JDBC与数据库连接工具对比分析
生活随笔
收集整理的這篇文章主要介紹了
JDBC与数据库连接工具对比分析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JDBC
使用步驟:
- 注冊數據庫驅動
- 通過DriverManager獲取數據庫連接
- 通過Connection對象獲取Statement對象
- 使用Statement執行SQL語句
- 操作ResultSet結果集
- 回收數據庫資源
案例:
public class Example {public static void main(String[] args) throws SQLException {Statement stmt = null;ResultSet rs = null;Connection conn = null;try {Class.forName("com.mysql.jdbc.Driver");String url = "jdbc:mysql://localhost:3306/test1";String username = "root";String password = "mysql123";conn = DriverManager.getConnection(url,username,password);stmt = conn.createStatement();String sql = "select * from inf";rs = stmt.executeQuery(sql);System.out.println("id | name | age");while (rs.next()){int id = rs.getInt("id");String name = rs.getString("name");String age = rs.getString("age");System.out.println(id + "|" + name + "|" + age + "|");}}catch (ClassNotFoundException e){e.printStackTrace();}finally {if (rs != null){try {rs.close();}catch (SQLException e){e.printStackTrace();}rs = null;}if (stmt != null){try {stmt.close();}catch (SQLException e){e.printStackTrace();}stmt = null;}if (conn != null){try {conn.close();}catch (SQLException e){e.printStackTrace();}conn = null;}}}
}
數據庫連接池
使用步驟:
- 聲明靜態數據源成員變量
- 創建連接池對象
- 定義公有的得到數據源的方法
- 定義得到連接對象的方法
- 定義關閉資源的方法
- 具體案例代碼:
工具類:
/*** @author Damon Wang* @data 2020/9/15 08:47:26* 1. 聲明靜態數據源成員變量* 2. 創建連接池對象* 3. 定義公有的得到數據源的方法* 4. 定義得到連接對象的方法* 5. 定義關閉資源的方法*/public class DruidUtils {// Create a static variable ds;private static DataSource ds;// create connector object;static {InputStream is = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");Properties pp = new Properties();try {// create connector pool and properties's parameter;pp.load(is);ds = DruidDataSourceFactory.createDataSource(pp);}catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}// Create public the way of getting DataSource;public static DataSource getDataSource() {return ds;}// Create object the way of connector;public static Connection getConnection() throws SQLException {return ds.getConnection();}// Create thr way of turning of connector datasource;public static void close(Connection conn, Statement stmt, ResultSet rs){if (rs != null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (stmt != null){try{stmt.close();} catch (SQLException e) {e.printStackTrace();}}if (rs != null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}}}// Reload the way of turning of;public static void close(Connection conn,Statement stmt) {close(conn, stmt);}// Create the test Case;public static void main(String[] args)throws SQLException {System.out.println(ds.getConnection());}}
測試類:
@WebServlet(name = "RegisterServlet",urlPatterns = {"/RegisterServlet"})
public class RegisterServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html");request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");PrintWriter out = response.getWriter();String phone = request.getParameter("phone");String password = request.getParameter("password");String sql = "insert into user(phone,password)values(?,?)";PreparedStatement ps = null;ResultSet rs = null;Connection con = null;try {con = DruidUtils.getConnection();ps = con.prepareStatement(sql);ps.setString(1,phone);ps.setString(2,password);int i = ps.executeUpdate();if (i == 1) {String sqlOne = "select * from user where phone = " + phone;ps = con.prepareStatement(sqlOne);rs = ps.executeQuery();while (rs.next()) {String out_phone = rs.getString("phone");String out_password = rs.getString("password");out.println(out_phone);out.println(out_password);out.println("Register is success");}}else {out.println("Register is wrong.");}} catch (SQLException e) {e.printStackTrace();}finally {DruidUtils.close(con,ps);}}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}
}
對比分析:數據庫連接池相對JDBC操作具有簡潔性、高效性、便于后期維護。
- 數據庫連接池實現功能分離,數據庫連接池只負責分配、管理、和釋放數據庫連接,而CRUD基本操作可直接調用數據庫連接池工具,極大減少代碼量(簡潔性),同時也便于服務器后期維護。
- 對于JDBC每次創建和斷開都會消耗一定的時間和IO資源。這種頻繁操作Connection對象十分影響數據庫訪問效率。由于在大量的并發訪問時(例:十萬次),頻繁創建必然會影響數據庫訪問效率。而數據庫連接池采用資源分配的理念,用戶使用完Connection將其返回,以減少創建和斷開數據庫連接的次數,提高數據庫的訪問效率(高效性)。
總結
以上是生活随笔為你收集整理的JDBC与数据库连接工具对比分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Druid数据库连接池超时问题com.a
- 下一篇: Java中类、常量、变量、方法名等命名规