手把手封裝數據層之DButil數據庫連接的封裝
阿新 • • 發佈:2017-08-25
增刪 log ret n) ont pass 通過 void nag
最近這段時間一直在用SSM框架做增刪改查,突然想把以前還不會用框架的時候,綜合百度和各種資料結合API文檔抄襲而來的數據層的封裝分享給大家。這邊先封裝一個DButil。
我這個封裝就是爛大街的那種,沒什麽特別。
//DButil.java
package com.yck.database; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class DButil { private static String username;private static String password; private static String url; private static String driver; static { getDatabaseInfo("jdbc.properties"); try { Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch blocke.printStackTrace(); } } public static void getDatabaseInfo(String path) { InputStream in = DButil.class.getClassLoader().getResourceAsStream(path); Properties p = new Properties(); try { p.load(in); username= p.getProperty("jdbc.username"); password = p.getProperty("jdbc.password"); url = p.getProperty("jdbc.url"); driver = p.getProperty("jdbc.driver"); } catch (IOException e) { e.printStackTrace(); } } public static Connection getConnection() { Connection conn = null; try { conn = DriverManager.getConnection(url,username,password); }catch (SQLException e) { e.printStackTrace(); } return conn; } public static void closeConnection(Connection connection) { try { if(connection != null) { connection.close(); connection = null; } } catch (SQLException e) { e.printStackTrace(); } } public static PreparedStatement prepareStatement(Connection connection,String sql) { PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return preparedStatement; } public static void closePreparedStatement(PreparedStatement preparedStatement) { try { if(preparedStatement != null) { preparedStatement.close(); preparedStatement = null; } } catch (SQLException e) { e.printStackTrace(); } } public static Statement createStatement(Connection connection) { Statement statement = null; try { statement = connection.createStatement(); } catch (SQLException e) { e.printStackTrace(); } return statement; } public static void closeStatement(Statement statement) { try { if(statement != null) { statement.close(); statement = null; } } catch (SQLException e) { e.printStackTrace(); } } public static ResultSet getResultSet(Statement statement,String sql) { ResultSet resultSet = null; try { resultSet = statement.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return resultSet; } public static void closeResultSet(ResultSet resultSet) { try { if(resultSet != null) { resultSet.close(); resultSet = null; } } catch (SQLException e) { e.printStackTrace(); } } }
這裏是數據庫連接需要的jdbc.properties,我們是通過DButil類的讀取properties文件的辦法來將DButil實例化的。
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=true jdbc.username=root jdbc.password=8888888
數據庫寫了個很簡單的來測試
CREATE DATABASE test; USE test; CREATE TABLE t_user ( id INT AUTO_INCREMENT NOT NULL, name VARCHAR(20), age INT, PRIMARY KEY(id) )ENGINE=InnoDB DEFAULT CHARSET=utf8
手動插了兩條數據,數據庫的內容如下
下面我們來測試一下,新建一個Test類
package com.yck.database; import java.sql.*; public class Test { public static void main(String[] args) { Connection conn = DButil.getConnection(); Statement stam =DButil.createStatement(conn); String sql = "select * from t_user"; ResultSet rs = null; try { rs = stam.executeQuery(sql); while(rs.next()) { System.out.println(rs.getString("name")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DButil.closeResultSet(rs); DButil.closeStatement(stam); DButil.closeResultSet(rs); } }
運行結果如下,說明我們封裝的數據庫是能夠連接上的
手把手封裝數據層之DButil數據庫連接的封裝