Home >Database >Mysql Tutorial >esclipse connects to mysql database
How to connect to the database in the eclipse development environment and test whether the connection is successful
1) eclipse There is nodriver integrated with mysql in the 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/j2) Unzip, take only the file mysql-connector-java-XX.XX.XX-bin.jar, andQuote to the project where you need to connect to the mysql database, for example: I built a project ConMysql in eclipsel to test the database connection. The specific operations are as follows:
Right-click on the project ConMysql properties->Java Build Path->Libraries Click Add External JARS... Select the decompressed mysql-connector-java-XX.XX.XX-bin.jar2) The driver has been imported, let’s follow Let’s write a program to verify it
##
package test1;import java.sql.*;public class MysqlJdbc { public static void main(String args[]) { try { Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序 //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"); //连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码 System.out.println("Success connect Mysql server!"); Statement stmt = connect.createStatement(); ResultSet rs = stmt.executeQuery("select * from stu"); //user 为你表的名称while (rs.next()) { System.out.println(rs.getString("name")); } } catch (Exception e) { System.out.print("get data error!"); e.printStackTrace(); } } }
Click to run the program: if the following code appears, the connection is successful
Success loading Mysql Driver!
Success connect Mysql server!npu(content written by myself in mysql)
The above is the detailed content of esclipse connects to mysql database. For more information, please follow other related articles on the PHP Chinese website!