MySQL_(Java)【事物操做】使用JDBC模擬銀行轉帳向數據庫發起修改請求 傳送門html
MySQL_(Java)【鏈接池】使用DBCP簡單模擬銀行轉帳事物 傳送門java
Java應用程序訪問數據庫的過程:mysql
1、裝載數據庫驅動程序sql
2、經過jdbc創建數據庫鏈接數據庫
3、訪問數據庫,執行sql語句ide
4、斷開數據庫鏈接性能
數據庫鏈接池做用:負責分配、管理和釋放數據庫鏈接,它容許應用程序重複使用一個現有的數據庫鏈接,而不是再從新創建一個;釋放空閒時間超過最大空閒時間的數據庫鏈接來避免由於沒有釋放數據庫鏈接而引發的數據庫鏈接遺漏,這項技術能明顯提升對數據庫操做的性能。【百度百科】編碼
在JDBC中使用Arrary集合保存全部的連接url
private static ArrayList<Connection> conList = new ArrayList<Connection>();
添加靜態代碼塊,利用for循環一次建立五個連接spa
//靜態代碼塊:當整個程序執行的時候,優先加載靜態代碼塊 static { for(int i =0;i<5;i++) { Connection con = createConnection(); conList.add(con); } } private static Connection createConnection() { try { Class.forName("com.mysql.jdbc.Driver"); return DriverManager.getConnection(connectionURL, username, password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
建立連接時從靜態代碼塊中取出連接,當判斷靜態代碼塊中存在連接時就去取得第一個連接,取完後將該連接從conList集合中移除
當五個連接存取完後,可再次調用createConnection()方法再次建立五個連接
public static Connection getConnection() { if(conList.isEmpty()==false) { Connection con = conList.get(0); conList.remove(con); return con; }else { return createConnection(); } }
在con關閉後歸還鏈接池
private static void closeConnection(Connection con) { // try { // if(con!=null)con.close(); // } catch (SQLException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } conList.add(con); }
模擬銀行由a向b轉帳1000元操做,使用事物+鏈接池
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBC01 { public static void main(String[] args) throws SQLException { transferAccount("a","b",1000); } public static void selectAll() throws SQLException { //註冊驅動 使用驅動鏈接數據庫 Connection con = null; Statement stmt = null; ResultSet rs = null; try { //數據庫的鏈接 con = JDBCUtils.getConnection(); //數據庫的增刪改查 stmt = con.createStatement(); //返回一個結果集 rs =stmt.executeQuery("select * from garytb"); while(rs.next()) { //System.out.println(rs.getString(1)+","+rs.getString(2)+","+rs.getString(3)); System.out.println(rs.getString("id")+","+rs.getString("username")+","+rs.getString("password")); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { JDBCUtils.close(rs, stmt, con); } } //校驗用戶 public static boolean selectByUernamePassword(String username,String password) throws SQLException { Connection con=null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false"; con = DriverManager.getConnection(url,"root","123456"); stmt =con.createStatement(); String sql = "select * from garytb where username = '"+username+"' and password = '"+password+"'"; //System.out.println(sql); rs = stmt.executeQuery(sql); if(rs.next()) { return true; }else { return false; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(con!=null) con.close(); } return false; } public static boolean selectByUP2(String username,String password) throws SQLException{ Connection con=null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false"; con = DriverManager.getConnection(url,"root","123456"); String sql = "select * from garytb where username = ? and password = ?"; PreparedStatement pstmt = con.prepareStatement(sql); //添加參數 pstmt.setString(1, username); pstmt.setString(2, password); //進行查詢 rs = pstmt.executeQuery(); if(rs.next()) { return true; }else { return false; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(con!=null) con.close(); } return false; } //pageNumber是頁數,第幾頁,pageCount是每頁顯示多少個數據 public static void selectUserByPage(int pageNumber,int pageCount) throws SQLException { //註冊驅動 使用驅動鏈接數據庫 Connection con = null; PreparedStatement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); //String url ="jdbc:mysql://localhost:3306/garysql"; //指定編碼查詢數據庫 String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false"; String user = "root"; String password = "123456"; //創建和數據庫的鏈接 con = DriverManager.getConnection(url,user,password); stmt = con.prepareStatement("select * from garytb limit ?,?"); stmt.setInt(1, (pageNumber-1)*pageCount ); stmt.setInt(2, pageCount); rs = stmt.executeQuery(); while(rs.next()) { //System.out.println(rs.getString(1)+","+rs.getString(2)+","+rs.getString(3)); System.out.println(rs.getString("id")+","+rs.getString("username")+","+rs.getString("password")); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(con!=null) con.close(); } } //crud: create read update delete //插入語句 public static void insert(String username,String password) throws SQLException { //註冊驅動 使用驅動鏈接數據庫 Connection con = null; PreparedStatement stmt = null; ResultSet rs = null; try { con = JDBCUtils.getConnection(); String sql = "insert into garytb(username,password) values(?,?)"; stmt = con.prepareStatement(sql); stmt.setString(1, username); stmt.setString(2, password); int result =stmt.executeUpdate();// 返回值表明收到影響的行數 System.out.println("插入成功"+username); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { JDBCUtils.close(rs, stmt, con); } } //刪除語句 public static void delete(int id) throws SQLException { //註冊驅動 使用驅動鏈接數據庫 Connection con = null; PreparedStatement stmt = null; ResultSet rs = null; try { con = JDBCUtils.getConnection(); String sql = "delete from garytb where id = ?"; stmt = con.prepareStatement(sql); stmt.setInt(1, id); int result =stmt.executeUpdate();// 返回值表明收到影響的行數 if(result>0) { System.out.println("刪除成功"); }else { System.out.println("刪除失敗"); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.close(rs, stmt, con); } } //修改語句 public static void update(int id,String newPassword) throws SQLException { Connection con = null; PreparedStatement stmt = null; ResultSet rs = null; try { con = JDBCUtils.getConnection(); String sql = "update garytb set password = ? where id = ?"; stmt = con.prepareStatement(sql); stmt.setString(1, newPassword); stmt.setInt(2, id); int result =stmt.executeUpdate();// 返回值表明收到影響的行數 if(result>0) { System.out.println("修改爲功"); }else { System.out.println("修改失敗"); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.close(rs, stmt, con); } } //事物操做 //由username1向username2轉帳金額 public static void transferAccount(String username1,String username2,int money) { Connection con = null; PreparedStatement stmt = null; ResultSet rs = null; try { con = JDBCUtils.getConnection(); //開啓事物 是否自動提交 con.setAutoCommit(false); String sql = "update garytb set balance = balance - ? where username = ?"; stmt = con.prepareStatement(sql); stmt.setInt(1, money); stmt.setString(2, username1); stmt.executeUpdate();// 返回值表明收到影響的行數 //顯示異常throw new Exception("出現錯誤"); //隱示異常 空指針異常 //String s = null; //s.charAt(2); sql = "update garytb set balance = balance + ? where username = ?"; stmt = con.prepareStatement(sql); stmt.setInt(1, money); stmt.setString(2, username2); stmt.executeUpdate();// 返回值表明收到影響的行數 System.out.println("操做成功!!"); //提交事務 //當事物中全部事物都完成了纔會提交 con.commit(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.close(rs, stmt, con); } } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; public class JDBCUtils { private static final String connectionURL = "jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false"; private static final String username = "root"; private static final String password = "123"; private static ArrayList<Connection> conList = new ArrayList<Connection>(); //靜態代碼塊:當整個程序執行的時候,優先加載靜態代碼塊 static { for(int i =0;i<5;i++) { Connection con = createConnection(); conList.add(con); } } public static Connection getConnection() { if(conList.isEmpty()==false) { Connection con = conList.get(0); conList.remove(con); return con; }else { return createConnection(); } } private static Connection createConnection() { try { Class.forName("com.mysql.jdbc.Driver"); return DriverManager.getConnection(connectionURL, username, password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void close(ResultSet rs,Statement stmt,Connection con) { closeResultSet(rs); closeStatement(stmt); closeConnection(con); } public static void close(Statement stmt1,Statement stmt2,Connection con) { closeStatement(stmt1); closeStatement(stmt2); closeConnection(con); } private static void closeResultSet(ResultSet rs ) { try { if(rs!=null)rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void closeStatement(Statement stmt) { try { if(stmt!=null) stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void closeConnection(Connection con) { // try { // if(con!=null)con.close(); // } catch (SQLException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } conList.add(con); } }