(Self-test, start learning Eclipse (my eclipse version of the jar package version of the 4.5.2,JDBC drive is 5.1.7, the test can be used) when connected to the database, I found a lot of experience on the Internet, but found that there are many errors on the Internet, so I 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
Download the above file is also very bumpy ah, so for your convenience, hereby share.
2: After downloading the JDBC drive above, you can begin to do the operation, first open Eclipse, create a project, my project name is called demo, then right click SRC, continue to find new, find Floder, and then as shown
Then under the Project SRC right click Next is to download a good JDBC drive paste into this demo under the project under the Lib, and then click the jar package just pasted, find build path continue to find add to build path, the results appear as shown above, You can then use Eclipse to connect my SQL database after the addition is complete
The connection database code is as follows (attention should be focused on connection connect=drivermanage.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "password") )
The above sentence inside the "password" is your database your own password; you need to modify it, the above "jdbc:mysql://localhost:3306/test" inside of the test is the use of their own MySQL created a table, is created by themselves, need additional attention; (Create my SQL statement will be found elsewhere in this blog, please pay attention)
package Com.ningmengxueyuan; import java.sql.*; public class mysqljdbc{public static V OID 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");
The connection URL is jdbc:mysql//server address/database name, the following 2 parameters are login username and password System.out.println ("Success connect MySQL server!");
Statement stmt = Connect.createstatement ();
ResultSet rs = stmt.executequery ("SELECT * from user");
User is 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 (); }
}
}
The above is a small set to introduce the Eclipse connection MySQL database operation summary, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!