How to connect to the database in the Eclipse development environment and test the connection success
1) There is no integrated MySQL driver in the Eclipse development environment, you need to download the connection driver Mysql-connector-java-xx-xx-xx.zip from the following address:
Http://dev.mysql.com/downloads/connector/j
2) Extract, just take the file Mysql-connector-java-xx.xx.xx-bin.jar, reference it to the project you need to connect to the MySQL database, Such as: I built a test database connection in the Eclipsel project Conmysql. Here's how:
Right-click on the project Conmysql
Properties->java Build path->libraries
Click Add External JARS ...
Select the Mysql-connector-java-xx.xx.xx-bin.jar after decompression
2) The driver has been imported, let's write a program to verify
Packagetest1;ImportJava.sql.*; Public classMYSQLJDBC { Public Static voidMain (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", "Aa-12345"); //connection URL is jdbc:mysql//server address/database name, the following 2 parameters are login username and password, respectivelySystem.out.println ("Success Connect Mysql server!"); Statement stmt=connect.createstatement (); ResultSet RS= Stmt.executequery ("Select * from Stu"); //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 (); } }}
Click Run Program: The following code appears to indicate a successful connection
Success Loading Mysql driver!
Success Connect Mysql server!
NPU (what you write in MySQL)
Esclipse connecting MySQL Database