How to connect java to the database
Tools: eclipse, MySQL, MySQL connection driver: mysql-connector-java -5.1.45.jar
This is the connection method officially provided by MySQL (i.e. MySQL connection driver). After decompression, you will get the jar library file, which needs to be imported into the project.
This is the decompressed file: (Recommended tutorial: java tutorial)
1. Create a new java project and then Create a new folder - libs (used to store various external packages)
Right-click the project name (mine is sqldemo1) - Create New - Folder —Folder name is libs—Finish.
2. Add the package to connect to the mysql database in the package
That is, the mysql-connector-java-5.1.45-bin.jar## obtained after decompression.
#Then copy and paste it under the libs folder of our java project (note that it is the physical address); At this time, in eclipse - right-click the libs folder - Refresh, and it will be downloaded Put the JDBC in the folder, as shown in the following figure:3. The jar package
on the build path is in In eclipsea: Click on the project Project - select the properties Properties b: AddAfter opening the properties Properties, click java Build path (Java Build Path)Click Add jar (Add JARs...), select the jar package under your project, then confirm, and finally add it4. Connect the mysql database in the java project
Create two new Class class files in the java project package, which I named DBHelper and Demo A class is used to open or close the database: DBHelper.javapackage sqldemo1; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class DBHelper { public static final String url = "jdbc:mysql://127.0.0.1/student"; public static final String name = "com.mysql.jdbc.Driver"; public static final String user = "root"; public static final String password = ""; public Connection conn = null; public PreparedStatement pst = null; public DBHelper(String sql) { try { Class.forName(name);//指定连接类型 conn = DriverManager.getConnection(url,user,password);//获取连接 pst = conn.prepareStatement(sql);//准备执行语句 } catch (Exception e) { e.printStackTrace(); } } public void close() { try { this.conn.close(); this.pst.close(); } catch (SQLException e) { e.printStackTrace(); } } }
package sqldemo1; import java.sql.ResultSet; import java.sql.SQLException; public class Demo { static String sql = null; static DBHelper db1 = null; static ResultSet ret = null; public static void main(String[] args) { sql = "select *from stuinfo";//SQL语句 db1 = new DBHelper(sql);//创建DBHelper对象 try { ret = db1.pst.executeQuery();//执行语句,得到结果集 while (ret.next()) { String uid = ret.getString(1); String ufname = ret.getString(2); String ulname = ret.getString(3); String udate = ret.getString(4); System.out.println(uid + "\t" + ufname + "\t" + ulname + "\t" + udate ); }//显示数据 ret.close(); db1.close();//关闭连接 } catch (SQLException e) { e.printStackTrace(); } } }
##5. Analysis of the reasons for failure to connect to the mysql database:
1. Whether the database service is open
2. Whether the corresponding database is created
3. Whether the jar package is added
4. Whether the driver is loaded
5. Whether the connection string, login name and password are correct
If the connection fails, check whether these factors occur
The above is the detailed content of How to connect to database in java. For more information, please follow other related articles on the PHP Chinese website!