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

王林
Release: 2020-10-26 15:32:27
Original
15282 people have browsed it

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 }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!