JDBC的一些API
生活随笔
收集整理的這篇文章主要介紹了
JDBC的一些API
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- JDBCUtils
- 數據庫連接池
- Apache—DBUtils
- BasicDAO
??JDBC就是Java做了一套關于數據庫操作的接口,交給由不同的數據庫產商去具體實現。這樣,無論是那種類型的數據庫,我們導入相應的 jar包,只操作這一套接口就可以實現功能。
??下面我做了一張JDBC常用API的思維導圖:
------- 2023.1.15加更
JDBCUtils
??以上是JDBC的一些常用API,但是連接數據庫依然很繁瑣。連接數據庫和關閉資源是必須的操作,我們可以寫一個JDBCUtils,封裝連接資源和關閉資源。
public class JDBCutils {public static String user;public static String password;public static String url;public static String driver;static {try {Properties properties = new Properties();properties.load(new FileInputStream("D:\\code\\JDBC\\src\\mysql.properties"));user = properties.getProperty("user");password = properties.getProperty("password");url = properties.getProperty("url");driver = properties.getProperty("driver");} catch (IOException e) {throw new RuntimeException(e);}}public static Connection getConnection(){try {return DriverManager.getConnection(url,user,password);} catch (SQLException e) {throw new RuntimeException(e);}}public static void close(ResultSet set, Statement statement,Connection connection){try {if (set != null){set.close();}if (statement != null){statement.close();}if (connection != null){connection.close();}} catch (SQLException e) {throw new RuntimeException(e);}} }??這樣獲取連接和關閉資源我們就不用每次都寫一大推重復的代碼了,調用這個工具類傳參數就可以了。
??批處理
??需要處理大量的sql語句時,我們可以采取批處理的方式,即,把sql語句打個包,發到數據庫執行時讓其一次性執行多條。而不是收一條處理一條,再收一條再處理一條。這樣提高了效率:
數據庫連接池
??頻繁連接、斷開數據庫性能較低,而且訪問量突增的情況下會使得數據庫癱瘓,因此引入了數據庫連接池技術。連接池作為一個緩沖,既提高了效率,又保護了數據庫安全。德魯伊連接池目前性能最好,應用廣泛。
public class JDBCUtilsByDruid {private static DataSource ds;static {Properties properties = new Properties();try {properties.load(new FileInputStream("D:\\code\\JDBC\\src\\druid.properties"));ds = DruidDataSourceFactory.createDataSource(properties);} catch (Exception e) {throw new RuntimeException(e);}}public static Connection getConnection() throws SQLException {return ds.getConnection();}public static void close(ResultSet resultSet, Statement statement, Connection connection) {try {if (resultSet !=null){resultSet.close();}if (statement != null){statement.close();}if (connection != null){connection.close();}} catch (SQLException e) {throw new RuntimeException(e);}} }Apache—DBUtils
??關閉connection連接后,結果集resultSet也不能再使用了。但是,一直不關連接會浪費資源,因此需要把結果集封裝到集合中去。
public ArrayList<Actor> testArraylist(){Connection connection = null;String aql = "select * from actor where id >= ?"; // String aql = "select *from actor";PreparedStatement preparedStatement = null;ResultSet resultSet = null;ArrayList<Actor> list = new ArrayList<>();try {connection = JDBCUtilsByDruid.getConnection();preparedStatement = connection.prepareStatement(aql);preparedStatement.setInt(1,1);resultSet = preparedStatement.executeQuery();while (resultSet.next()) {int id =resultSet.getInt("id");String name = resultSet.getString("name");String sex = resultSet.getString("sex");String brithday = resultSet.getString("brithday");String phone = resultSet.getString("phone");list.add(new Actor(id,name,sex,brithday,phone));}System.out.println("list = " + list);} catch (SQLException e) {throw new RuntimeException(e);} finally {JDBCUtilsByDruid.close(resultSet,preparedStatement,connection);}return list;}??以上是韓老師教我們自己動手封裝的辦法,相當于是說一下原理和思路。而Apache—DBUtils則提供了一系列辦法,我們導入jar包直接調用即可。簡化了JDBC開發。開發中我們使用druid和commons.dbutils進行JDBC開發。
BasicDAO
??在進一步的使用中,我們封裝了BasicDAO類,引入泛型和可變參數進一步提高了通用性和效率。實例見在下的另外一篇文章,滿漢樓項目。
public class BasicDAO<T> {private QueryRunner qr = new QueryRunner();public int update(String sql, Object... parameters) {Connection connection = null;try {connection = JDBCUtilsByDruid.getConnection();int update = qr.update(connection, sql, parameters);return update;} catch (SQLException e) {throw new RuntimeException(e);} finally {JDBCUtilsByDruid.close(null,null,connection);}}public List<T> quertMutli(String sql,Class<T> clazz,Object... parameters){return null;}public T quertSingle(String sql,Class<T> clazz,Object... parameters){Connection connection = null;try {connection = JDBCUtilsByDruid.getConnection();return qr.query(connection,sql,new BeanHandler<T>(clazz),parameters);} catch (SQLException e) {throw new RuntimeException(e);} finally {JDBCUtilsByDruid.close(null,null,connection);}} }總結
以上是生活随笔為你收集整理的JDBC的一些API的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年1~12月语音合成和语音识别论
- 下一篇: 女子举重问题