search
HomeJavaJavagetting StartedWhat are the 5 steps to connect to the database using jdbc

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.

What are the 5 steps to connect to the database using jdbc

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

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.