Tools:
eclipse, MySQL, MySQL connection driver: mysql-connector-java-5.1.45.jar
1, Create a new java project and then create a new folder - libs (used to store various external packages)
2. Add a connection to the mysql database in the package The package
is mysql-connector-java-5.1.45-bin.jar, then copy and paste it under the libs folder of our java project (note that it is in the physical address); at this time, in In eclipse—right-click the libs folder—Refresh, and put the downloaded JDBC into the folder, as shown in the following figure:
##3, Jar package on the build path
a. Click on the project - select Properties
4. 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
package 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(); } } }Write another Demo.java to execute related queries OperationThe code is as follows:
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
a. Is the database service open?b. Whether the corresponding database has been created; c. Whether the jar package has been added; d. Whether the driver has been loaded; e. Connection string, Are the login name and password correct? Recommended tutorial:The above is the detailed content of Steps to connect java to database. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software