(I personally test it and started learning Eclipse (my Eclipse version is 4.5.2, and the JDBC drive jar package version is 5.1.7, which can be used for testing). When connecting to the database, I found that I had a lot of experience on the Internet, but found that there were many errors on the Internet, so I posted this blog, hoping it will be helpful to everyone)
1: First, you need to download the Jdbc drive (mysql-connector-java-5.1.7-bin.jar) file
It is also very difficult to download the above file, so for your convenience, I would like to share it here.
2: After downloading the above Jdbc drive, you can start to operate it. First, open Eclipse and create a Project. The name of my project is demo. Then right-click src, continue to find new, find Floder, and then as shown in the figure
Then right-click on the src below the project. Next, paste the downloaded Jdbc drive into the lib below the demo project. Then click on the jar package you just pasted, find the build path and continue to find the add to build path. The result appears as shown in the figure above. Only after the addition is completed can you use Eclipse to connect to the My sql database using Eclipse.
The connection database code is as follows (the important thing to note is Connection connect=DriverManage.getConnection("jdbc:mysql://localhost:3306/test","root","password"))
The "password" in the above sentence is your own password; you need to modify it yourself. The test in "jdbc:mysql://localhost:3306/test" above is a table created by myself using mysql, which is created by myself, and requires extra attention; (The my sql statement will be found elsewhere in this blog, please pay attention)
package com.ningmengxueyuan;import java.sql.*;public class MysqlJdbc{ public static void main(String args[]) { try { Class.forName("com.mysql.jdbc.Driver"); //Load the 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 two parameters are the 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 the summary of the Eclipse connection Mysql database operation introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!