JDBC工具類連線資料庫測試
阿新 • • 發佈:2018-12-26
測試程式碼
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import com.jdbcutils.JDBCUtils; public class JDBCDemo2 { public void select(){ Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try{ // 獲取連線 conn = JDBCUtils.getConnection(); // 編寫SQL: String sql = "select * from user"; // 預編譯SQL: pstmt = conn.prepareStatement(sql); // 獲取結果集 rs = pstmt.executeQuery(); // 遍歷 while(rs.next()){ System.out.println(rs.getInt("id")+" "+rs.getString("username")+" "+rs.getString("password")+" "+rs.getString("nickname")); } }catch(Exception e){ e.printStackTrace(); }finally{ // 關閉資源 JDBCUtils.release(rs, pstmt, conn); } } }
package com.jdbcutils; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Properties; public class JDBCUtils { private static final String driverClassName; private static final String url; private static final String username; private static final String password; static{ Properties Properties = new Properties(); try { Properties.load(new FileInputStream("src/db.properties")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } driverClassName = Properties.getProperty("driverClassName"); url = Properties.getProperty("url"); username = Properties.getProperty("username"); password = Properties.getProperty("password"); } //載入驅動 public static void loadDriver(){ try { Class.forName(driverClassName); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //獲取連線 public static Connection getConnection(){ Connection conn = null; try { loadDriver(); conn = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void release(Statement stmt,Connection conn){ if(stmt != null){ try { stmt.close(); } catch (Exception e) { e.printStackTrace(); } stmt = null; } if(conn != null){ try { conn.close(); } catch (Exception e) { e.printStackTrace(); } conn = null; } } public static void release(ResultSet rs,Statement stmt,Connection conn){ if(rs != null){ try { rs.close(); } catch (Exception e) { e.printStackTrace(); } rs = null; } if(stmt != null){ try { stmt.close(); } catch (Exception e) { e.printStackTrace(); } stmt = null; } if(conn != null){ try { conn.close(); } catch (Exception e) { e.printStackTrace(); } conn = null; } } }
db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///web_test3
username=root
password=123
SQL建表語句
create database web_test3; use web_test3; create table user( id int primary key auto_increment, username varchar(20), password varchar(20), nickname varchar(20), age int ); insert into user values (null,'aaa','123','小麗',34); insert into user values (null,'bbb','123','大王',32); insert into user values (null,'ccc','123','小明',28); insert into user values (null,'ddd','123','大黃',21);