1. 程式人生 > >簡單的JDBC的增刪改查操作->抽取了基類,附源碼

簡單的JDBC的增刪改查操作->抽取了基類,附源碼

exceptio tac catch main trace 工具 nload null user

1.主程序

 1 package com.xyyz.jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 
 8 import com.xyyz.utils.JDBCUtils;
 9 
10 public class JDBCDemo {
11 
12     public static void main(String[] args) throws Exception {
13 query(); 14 insert(); 15 query(); 16 update(); 17 query(); 18 delete(); 19 query(); 20 } 21 22 /** 23 * 查詢代碼 24 * 25 * @throws Exception 26 */ 27 public static void query() throws Exception { 28 Connection connection = JDBCUtils.getConnection();
29 // 獲取數據庫連接 30 String sql = "select * from jdbctestdata"; 31 // 獲取預編譯對象 32 PreparedStatement prepareStatement = connection.prepareStatement(sql); 33 // 執行sql語句 34 ResultSet resultSet = prepareStatement.executeQuery(); 35 // 遍歷打印sql語句 36 while (resultSet.next()) {
37 System.out.println(resultSet.getInt(1) + "--" + resultSet.getString(2)); 38 } 39 // 關閉釋放資源 40 JDBCUtils.closeAll(resultSet, prepareStatement, connection); 41 } 42 43 /** 44 * 插入數據 45 */ 46 public static void insert() throws Exception { 47 // 獲取數據庫連接 48 Connection connection = JDBCUtils.getConnection(); 49 String sql = "insert into jdbctestdata value(?,?,?)"; 50 // 獲取預編譯對象 51 PreparedStatement prepare = connection.prepareStatement(sql); 52 prepare.setString(1, "6"); 53 prepare.setString(2, "小白"); 54 prepare.setString(3, "30"); 55 int i = prepare.executeUpdate(); 56 System.out.println("i=" + i); 57 // 關閉釋放資源 58 JDBCUtils.closeAll(null, prepare, connection); 59 } 60 61 /** 62 * 更新數據 63 */ 64 public static void update() throws Exception { 65 66 // 獲取數據庫連接 67 Connection connection = JDBCUtils.getConnection(); 68 String sql = "update jdbctestdata set name=? , age=? where id=?"; 69 // 獲取預編譯對象 70 PreparedStatement prepare = connection.prepareStatement(sql); 71 prepare.setString(1, "小紅"); 72 prepare.setString(2, "20"); 73 prepare.setString(3, "6"); 74 int i = prepare.executeUpdate(); 75 System.out.println("i=" + i); 76 // 執行sql語句 77 JDBCUtils.closeAll(null, prepare, connection); 78 } 79 80 /** 81 * 刪除數據 82 */ 83 public static void delete() throws Exception { 84 // 獲取數據庫連接 85 Connection connection = JDBCUtils.getConnection(); 86 String sql = "delete from jdbctestdata where id = ?"; 87 // 獲取預編譯對象 88 PreparedStatement prepare = connection.prepareStatement(sql); 89 prepare.setString(1, "6"); 90 int i = prepare.executeUpdate(); 91 System.out.println("i=" + i); 92 // 執行sql語句 93 JDBCUtils.closeAll(null, prepare, connection); 94 } 95 96 }

2.工具類

 1 package com.xyyz.utils;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 import java.sql.Statement;
 9 import java.util.Objects;
10 
11 public class JDBCUtils {
12     // 連接數據需要的參數
13     private static String url = "jdbc:mysql:///jdbctestdata";
14     private static String user = "root";
15     private static String password = "root";
16 
17     static {
18         // 註冊驅動
19         try {
20             Class.forName("com.mysql.jdbc.Driver");
21         } catch (ClassNotFoundException e) {
22             System.out.println("沒有加masql的jar包");
23         }
24 
25     }
26 
27     /**
28      * 獲取connection
29      */
30     public static Connection getConnection() throws SQLException {
31         return DriverManager.getConnection(url, user, password);
32     }
33 
34     // 關閉資源的方法
35     public static void closeAll(ResultSet resultSet, PreparedStatement prepareStatement, Connection connection) {
36         // 關閉資源
37         if (resultSet != null) {
38             try {
39                 resultSet.close();
40             } catch (Exception e) {
41                 e.printStackTrace();
42             }
43             resultSet = null;
44         }
45         if (prepareStatement != null) {
46             try {
47                 prepareStatement.close();
48             } catch (Exception e) {
49                 e.printStackTrace();
50             }
51             prepareStatement = null;
52         }
53         if (connection != null) {
54             try {
55                 connection.close();
56             } catch (Exception e) {
57                 e.printStackTrace();
58             }
59             connection = null;
60         }
61     }
62 }

http://download.csdn.net/detail/u010798073/9888920

簡單的JDBC的增刪改查操作->抽取了基類,附源碼