JDBC規(guī)范
持久化的概念
- 將程序中的數(shù)據(jù)保存到硬盤(pán)、U盤(pán)等可掉電式硬件中
為什么要使用JDBC規(guī)范?
- 在JDBC規(guī)范出來(lái)之前,各大數(shù)據(jù)庫(kù)廠(chǎng)商使用自己的操作數(shù)據(jù)庫(kù)的規(guī)范,使得廣大開(kāi)發(fā)者難以操作數(shù)據(jù)庫(kù),心力憔悴,開(kāi)發(fā)成本增大;直到Sun公司統(tǒng)一了各個(gè)操作數(shù)據(jù)庫(kù)的接口規(guī)范,迎來(lái)了開(kāi)發(fā)者的新生;而這套操作關(guān)系型數(shù)據(jù)庫(kù)的規(guī)范就是JDBC規(guī)范,極大地減少了開(kāi)發(fā)成本
JDBC的基本操作
- 加載mysql驅(qū)動(dòng)
Class.forName("com.mysql.jdbc.Driver"); - 獲取連接對(duì)象
Connection conn = DriverManager.getConnection( //url "jdbc:mysql://127.0.0.1/jdbc:3306?characterEncoding=utf-8&useSSL=false", //數(shù)據(jù)庫(kù)用戶(hù)名 "root", //數(shù)據(jù)庫(kù)密碼 "root" ); - 獲取語(yǔ)句對(duì)象
PreparedStatement pst = conn.prepareStatement(sql); - 執(zhí)行sql語(yǔ)句
//sql語(yǔ)句 String sql = "insert into t_students values (null, ?,?,?,?,?,?,?,?,?)"; //設(shè)置參數(shù)值 pst.setString(1, student.getUserName()); pst.setBoolean(2, student.getSex()); pst.setDate(3, new java.sql.Date(student.getBirthday().getTime())); pst.setBigDecimal(4, student.getSalary()); pst.setBigDecimal(5, student.getBalance()); pst.setString(6, student.getCollege()); pst.setString(7, student.getMajor()); pst.setString(8, student.getHobby()); pst.setInt(9, student.getClassRoom()); //執(zhí)行sql語(yǔ)句,返回?cái)?shù)據(jù)庫(kù)表中數(shù)據(jù)的影響條數(shù) int result = pst.executeUpdate(); - 釋放資源
pst.close(); conn.close();
DAO思想
概念
- 通過(guò)編寫(xiě)一套接口和對(duì)應(yīng)實(shí)現(xiàn)類(lèi)的組件來(lái)完成對(duì)數(shù)據(jù)庫(kù)的操作
DAO思想出現(xiàn)的意義
- 解耦,類(lèi)與類(lèi)之間的關(guān)聯(lián)更清晰,使程序更易維護(hù)
使用JDBC規(guī)范和DAO思想實(shí)現(xiàn)CRUD的步驟
- 按照規(guī)范定義包(domain、dao、dao/impl、util、test)
- 在domain中定義JavaBean對(duì)象,對(duì)象屬性和數(shù)據(jù)庫(kù)表中字段一一對(duì)應(yīng)
- 在dao包中按照IXxxDAO規(guī)范定義接口,接口中編寫(xiě)操作數(shù)據(jù)庫(kù)的抽象方法
- 在impl中按照XxxDAOImpl的規(guī)范定義dao包中接口的實(shí)現(xiàn)類(lèi)并編寫(xiě)操作數(shù)據(jù)庫(kù)的語(yǔ)句
- 在test包中按照規(guī)范定義測(cè)試impl包中類(lèi)的實(shí)現(xiàn)類(lèi),并使用@Test注解編寫(xiě)測(cè)試方法
提取工具類(lèi)
- 將impl實(shí)現(xiàn)類(lèi)中操作數(shù)據(jù)庫(kù)的重復(fù)語(yǔ)句提取出來(lái)并在util包中定義工具類(lèi)
- 將加載驅(qū)動(dòng)、獲取數(shù)據(jù)庫(kù)連接對(duì)象的語(yǔ)句編寫(xiě)在工具類(lèi)的static靜態(tài)代碼塊中
- 將釋放資源等重復(fù)語(yǔ)句編寫(xiě)在工具類(lèi)中定義工具方法
Jdbc工具類(lèi)
- jdbc.properties文件
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql:///mybatis?characterEncoding=utf-8&useSSL=false username=root password=root - JdbcUtils類(lèi)
public class JDBCUtils { private JDBCUtils() {} public static DataSource dataSource = null; public static Properties properties = new Properties(); /** * 加載jdbc.properties文件,讀取到properties對(duì)象中 */ static { InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); try { properties.load(in); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 使用Druid連接池工廠(chǎng)創(chuàng)建連接池對(duì)象 * @return 連接池對(duì)象 */ public static DataSource getDataSource() { try { if (dataSource == null) { dataSource = DruidDataSourceFactory.createDataSource(properties); } } catch (Exception e) { e.printStackTrace(); } return dataSource; } /** * 釋放資源 * @param conn * @param pst * @param rs */ public static void close(Connection conn, PreparedStatement pst, ResultSet rs) { try { if (rs != null) { rs.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (pst != null) { pst.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } /** * 釋放資源 * @param conn * @param pst */ public static void close(Connection conn, PreparedStatement pst) { close(conn, pst, null); } }