The five steps for jdbc to connect to the database are: 1. Load the driver; 2. Connect to the database and determine whether the connection is successful; 3. Create a Statement object; 4. Execute the sql statement; 5. Print the results and close resources and databases.

The steps are as follows:
1. Load the driver; 2. Connect to the database and determine whether the connection is successful; 3. Create a Statement object; 4. , execute the sql statement; 5. Print the results, and close the resources and database.
(Video tutorial recommendation: java course)
Code implementation:
1 import java.sql.Connection;
2 import java.sql.DriverManager;
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5 import java.sql.Statement;
6
7 public class connectionMysql {
8
9 public static void main(String[] args) {
10
11 String driver="com.mysql.jdbc.Driver";//驱动路径
12 String url="jdbc:mysql://localhost:3306/eshop";//数据库地址
13 String user="root";//访问数据库的用户名
14 String password="123456";//用户密码
15 try {
16 //1、加载驱动
17 Class.forName(driver);
18 //2、连接数据库
19 Connection con = DriverManager.getConnection(url, user, password);
20 if(!con.isClosed()){//判断数据库是否链接成功
21 System.out.println("已成功链接数据库!");
22 //3、创建Statement对象
23 Statement st = con.createStatement();
24 //4、执行sql语句
25 String sql="select *from user";//查询user表的所有信息
26 ResultSet rs = st.executeQuery(sql);//查询之后返回结果集
27 //5、打印出结果
28 while(rs.next()){
29 System.out.println(rs.getString("Id")+"\t"+rs.getString("name")+"\t"+rs.getString("password"));
}
}
31 rs.close();//关闭资源
32 con.close();//关闭数据库
33 }
34
35 } catch (Exception e) {
36 // TODO Auto-generated catch block
37 e.printStackTrace();
38 }
39 }
40 }Related recommendations: java entry
The above is the detailed content of What are the 5 steps to connect to the database using jdbc. 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.





