Actions that eclipse connects to my SQL database
(Self-test, start learning Eclipse (my eclipse version is the 4.5.2,JDBC drive Jar package version is 5.1.7, pro-test can be used) when connected to the database, found that there are a lot of experience online, but found that there are many errors on the Internet, so published this blog, Hope to be helpful to everyone)
1: Preferred you need to download the JDBC Drive (mysql-connector-java-5.1.7-bin.jar) This file (Baidu Cloud disk http://pan.baidu.com/s/1hs9DxS4, password c22r)
Download the above file is also very bumpy ah, so for your convenience, hereby share.
2: After downloading the above JDBC drive, you can start the operation, first open Eclipse, create a project, my project name is demo, then right click SRC, continue to find new, find Floder, and then
Then in the project below the SRC right click Next is to paste the download of the JDBC driver into this demo project under the Lib, and then click on the jar just pasted, find the build path continue to find the add to build path, The results appear as shown , and after the addition is complete, you can use Eclipse to connect to my SQL database
The connection database code is as follows (it is important to note that connection connect=drivermanage.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "password") )
The "password" in the above sentence is your database's own password, you need to modify it, the above "jdbc:mysql://localhost:3306/test" inside of the test is their own use of MySQL created a table, is created by themselves, need extra attention; (Create my SQL statement will be found elsewhere in this blog, stay tuned)
Package Com.ningmengxueyuan;
import java.sql.*;
Public class mysqljdbc{
Public static void Main (String args[]) {
try {
class.forname ("Com.mysql.jdbc.Driver"); Load MySQL JDBC driver
//class.forname ("Org.gjt.mm.mysql.Driver");
System.out.println ("Success loading Mysql driver!");
}catch (Exception e) {
System.out.print ("Error loading Mysql driver!");
E.printstacktrace ();
}
try{
Connection connect = drivermanager.getconnection (
"Jdbc:mysql://localhost:3306/test1", "root", "123456");
//Connection URL is jdbc:mysql//server address/database name, the following 2 parameters are login username and password, respectively
System.out.println ("Success connect Mysql server!");
Statement stmt = Connect.createstatement ();
ResultSet rs = stmt.executequery ("SELECT * from user");
//user the name of your table
While (Rs.next ()) {
System.out.println (rs.getstring ("name"));
}
}catch (Exception e) {
System.out.print ("Get Data error!");
E.printstacktrace ();
}
}
}
Summary of operations that eclipse connects to my SQL database