JDBCUtils工具類建立和使用
阿新 • • 發佈:2019-01-06
0. 準備工作
先要匯入mysql-connector的jar包,新增依賴關係。然後匯入jdbc.properties配置檔案
//jdbc 配置檔案內容
url=jdbc:mysql:///db1
user=root
password=root
driver=com.mysql.jdbc.Driver
1. JDBCUtils工具類程式碼實現
package util; import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.*; import java.util.Properties; /** * JDBC 工具類 */ public class JDBCUtils { private static String url; private static String user; private static String password; private static String driver; /** * 檔案讀取,只會執行一次,使用靜態程式碼塊 */ static { //讀取檔案,獲取值 try { //1.建立Properties集合類 Properties pro = new Properties(); //獲取src路徑下的檔案--->ClassLoader類載入器 ClassLoader classLoader = JDBCUtils.class.getClassLoader(); URL resource = classLoader.getResource("jdbc.properties");; String path = resource.getPath(); //2.載入檔案 pro.load(new FileReader(path)); //3獲取資料 url = pro.getProperty("url"); user = pro.getProperty("user"); password = pro.getProperty("password"); driver = pro.getProperty("driver"); //4.註冊驅動 Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 獲取連線 * @return 連線物件 */ public static Connection getConnection() throws SQLException { Connection conn = DriverManager.getConnection(url, user, password); return conn; } /** * 釋放資源 * @param rs * @param st * @param conn */ public static void close(ResultSet rs, Statement st,Connection conn){ if (rs != null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(st != null){ try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
2. 把資料裡面建立的表格當作一個物件
如下面這個表:
建立一個數據庫表的類,程式碼如下:
package com.zk.demo02jdbcutils; public class UserBean { private int id; private String username; private String password; public UserBean(int id, String username, String password){ this.id = id; this.username = username; this.password = password; } @Override public String toString() { return "UserBean{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
3.測試JDBCUtils工具類
package com.zk.demo02jdbcutils;
import util.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class Demo01JDBCUtils {
public static void main(String[] args) { List<UserBean> list = findAll(); System.out.println("list = " + list); } //定義一個方法,查詢user表的資料將其封裝為物件,然後裝載集合,返回。 public static List<UserBean> findAll(){ Connection conn = null; Statement st = null; ResultSet rs = null; List<UserBean> list = null; try { conn = JDBCUtils.getConnection(); //定義sql String sql = "SELECT * FROM user"; //獲取執行sql的物件 st = conn.createStatement(); //執行sql rs = st.executeQuery(sql); UserBean bean = null; list = new ArrayList<UserBean>(); while (rs.next()){ int id = rs.getInt("id"); String username = rs.getString("username"); String password = rs.getString("password"); bean = new UserBean(id,username,password); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); }finally { JDBCUtils.close(rs,st,conn); } return list; } }
執行結果如下圖