JDBC连接数据库并执行语句入门案例

Posted yxfyg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC连接数据库并执行语句入门案例相关的知识,希望对你有一定的参考价值。

 1 package com.yxfyg.test;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 
 9 import com.yxfyg.util.JDBCUtil;
10 
11 public class MainTest {
12     
13     public static void main(String[] args) {
14         
15         Connection conn = null;
16         Statement st = null;
17         ResultSet rs = null;
18         try {
19             //1.注册驱动
20             DriverManager.registerDriver(new com.mysql.jdbc.Driver());
21             //2.获取连接
22             conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb?user=root&password=root");
23             //3.创建statement,跟数据库打交道,一定需要这个对象
24             st = conn.createStatement();
25             //4.执行查询,得到结果集
26             String sql = "select * from student";
27             rs = st.executeQuery(sql);
28             //5.遍历查询每一条记录
29             while(rs.next()) {
30                 int sno = rs.getInt("sno");
31                 String sname = rs.getString("sname");
32                 String sex = rs.getString("ssex");
33                 int sage = rs.getInt("sage");
34                 String sdept = rs.getString("sdept");
35                 System.out.println("sno="+ sno +",sname="+ sname +",sex="+ sex +",age="+ sage +",sdept=" + sdept);
36             }
37         } catch (SQLException e) {
38             e.printStackTrace();
39         }finally {
40             //6.释放资源
41             JDBCUtil.release(rs, st, conn);
42         }
43     }
44 }
 1 package com.yxfyg.util;
 2 
 3 import java.sql.Connection;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 import java.sql.Statement;
 7 
 8 public class JDBCUtil {
 9     
10     public static void release(ResultSet rs,Statement st,Connection conn) {
11         closeRs(rs);
12         closeSt(st);
13         closeConn(conn);
14     }
15     
16     private static void closeRs(ResultSet rs) {
17         try {
18             if(rs != null) {
19                 rs.close();
20             }
21         }catch(SQLException e) {
22             e.printStackTrace();
23         }finally {
24             rs = null;
25         }
26     }
27     
28     private static void closeSt(Statement st) {
29         try {
30             if(st != null) {
31                 st.close();
32             }
33         }catch(SQLException e) {
34             e.printStackTrace();
35         }finally {
36             st = null;
37         }
38     }
39     
40     private static void closeConn(Connection conn) {
41         try {
42             if(conn != null) {
43                 conn.close();
44             }
45         }catch(SQLException e) {
46             e.printStackTrace();
47         }finally {
48             conn = null;
49         }
50     }
51     
52 }

 

以上是关于JDBC连接数据库并执行语句入门案例的主要内容,如果未能解决你的问题,请参考以下文章

21jdbc入门1

jdbc java数据库连接 1)jdbc入门

Hibernate入门配置案例

Mybatis 入门案例分享

Hibernate介绍和入门案例

JDBC详细全解(示例超多)