1. 程式人生 > >java jdbc 查詢與更新實現

java jdbc 查詢與更新實現

package jdbc.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Demo1 {

    private final static String username = "root";
    private final static String password = "tiger";
    private
final static String url = "jdbc:mysql://localhost/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC "; Connection conn;// 連線引用 PreparedStatement ps; public Connection getConnection() { try { Class.forName("com.mysql.cj.jdbc.Driver");// 載入jdbc驅動 try
{ if (conn == null) { // 載入完後可以通過DriverManager獲取資料庫連線 conn = DriverManager.getConnection(url, username, password); conn.setAutoCommit(false);// 關閉事務自動提交 } } catch (SQLException e) { System.out.println("連線不了資料庫,或連線格式寫錯"
); e.printStackTrace(); this.close(); return null; } } catch (ClassNotFoundException e) { System.out.println("找不到驅動"); e.printStackTrace(); return null; } return conn; } public ResultSet find(Connection conn, String sql) { ResultSet result = null; try { ps = conn.prepareStatement(sql); result = ps.executeQuery();// 執行查詢 } catch (SQLException e) { // 查詢一般沒有事務要求 this.close(); e.printStackTrace(); } return result; } public int updateInsertDelete(Connection conn, String sql) { int i = 0; try { ps = conn.prepareStatement(sql); i = ps.executeUpdate();// 執行表更新(插入,更新,刪除都是表的更新操作) } catch (SQLException e) { try { conn.rollback();// 回滾事務 } catch (SQLException e1) { System.out.println("回滾事務失敗"); e1.printStackTrace(); } this.close();// 關閉資源 e.printStackTrace(); } return i; } public void close() { try { if (!ps.isClosed()) { ps.close(); } } catch (SQLException e1) { e1.printStackTrace(); } try { if (!conn.isClosed()) { conn.close(); } } catch (SQLException e1) { e1.printStackTrace(); } } public static void main(String[] args) throws SQLException { Demo1 jdbc = new Demo1(); Connection conn = jdbc.getConnection(); jdbc.updateInsertDelete(conn, "INSERT INTO test (username,create_time) VALUES ('user3',NOW());"); conn.commit();// 更新操作要提交事務才能生效 ResultSet result = jdbc.find(conn, "select id,username,update_time,create_time from test"); int maxCol = result.getMetaData().getColumnCount(); System.out.println("查詢時指定列的數量,為*時則是表的列數maxCol: " + maxCol); while (result.next()) {// 通過result.next()遍歷結果 int row = result.getRow(); System.out.print("當前遍歷的行數: " + row + " "); // 第一列的列號為1,所以i最小為1 for (int i = 1; i <= maxCol; i++) { System.out.print(result.getString(i) + " "); } System.out.println(""); } jdbc.close(); } } // 查詢時指定列的數量,為*時則是表的列數maxCol: 4 // 當前遍歷的行數: 1 1 user1 2017-05-29 10:19:39 2017-05-29 10:19:39 // 當前遍歷的行數: 2 2 user2 2017-05-29 10:20:15 2017-05-29 10:20:15 // 當前遍歷的行數: 3 5 user2 2017-05-29 10:22:12 2017-05-29 10:22:12 // 當前遍歷的行數: 4 12 user3 2017-05-29 10:42:10 2017-05-29 10:42:10 // 當前遍歷的行數: 5 13 user3 2017-05-29 10:42:12 2017-05-29 10:42:12 // 當前遍歷的行數: 6 14 user3 2017-05-29 10:42:17 2017-05-29 10:42:17 // 當前遍歷的行數: 7 15 user3 2017-05-29 10:43:34 2017-05-29 10:43:34 // 當前遍歷的行數: 8 16 user3 2017-05-29 10:43:42 2017-05-29 10:43:42 // 當前遍歷的行數: 9 17 user3 2017-05-29 10:44:33 2017-05-29 10:44:33 // 當前遍歷的行數: 10 18 user3 2017-05-29 10:44:42 2017-05-29 10:44:42 // 當前遍歷的行數: 11 19 user3 2017-05-29 10:56:58 2017-05-29 10:56:58 // 當前遍歷的行數: 12 20 user3 2017-05-29 11:28:13 2017-05-29 11:28:13 // 當前遍歷的行數: 13 21 user3 2017-05-29 11:29:02 2017-05-29 11:29:02 /* * CREATE TABLE test ( id INT(11) NOT NULL AUTO_INCREMENT , username VARCHAR(50) * NOT NULL , update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE * CURRENT_TIMESTAMP, create_time DATETIME , PRIMARY KEY(id) )ENGINE=INNODB * DEFAULT CHARSET utf8mb4 COMMENT '測試表'; * * INSERT INTO test (username,create_time) VALUES ('user1',NOW()); * * * */