JDBC更新+數據庫連接池--詳解--初學者必看


上一篇文章給大家寫了jdbc的基本的知識。本章會進行詳細講解JDBC的使用。

JDBC的使用增刪改查操作

JDBC工具類的封裝

下面展示一些 內聯代碼片

package cn.tedu.util;
/**
* @author  作者: bjzhangjian
* @version 創建時間:2020年5月7日 上午10:14:03
* @description 描述:jdbc的工具類
*/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtil {
	
	/**
	 * 數據庫的連接
	 */
	public static Connection getConnection() {
		//1、註冊數據庫的驅動
		try {
			Class.forName("com.mysql.jdbc.Driver");
			//2、獲取數據庫的連接
			String url = "jdbc:mysql:///dd_db?characterEncoding=utf-8";
			String user = "root";//用戶名和密碼是你數據庫連接的時候的用戶名和密碼
			String password = "root";
			Connection connection = DriverManager.getConnection(url, user, password);
			return connection;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 釋放資源
	 * @param connection 連接對象
	 * @param st 傳輸器對象
	 * @param rs 結果集對象
	 */
	public static void close(Connection connection, Statement st, ResultSet rs) {
		//6、釋放資源
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				rs = null;
			}
		}
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				st = null;
			}
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				connection = null;
			}
		}
	}
}

JDBC實現新增數據-插入-insert

需求:
新增一條數據:郭澤平 qwer123 [email protected] 13100006666

@Test
	public void testInsert() {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			//獲取數據庫的連接
			conn = JDBCUtil.getConnection();
			//獲取傳輸器對象
			st = conn.createStatement();
			//時間的轉化
//			Date date = new Date();
			int rows = st.executeUpdate("insert into user values(null,'郭澤平','qwer123','2020-5-7 10:34:00','[email protected]','13100006666')");
			//
			System.out.println("受影響的行數:" + rows);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, st, rs);
		}
	}

JDBC實現修改數據-更新–update

@Test
	public void testUpdate() {
		
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			//獲取數據庫的連接
			conn = JDBCUtil.getConnection();
			//獲取傳輸器對象
			st = conn.createStatement();
			int rows = st.executeUpdate("update user set password='asd123' where username='張老師';");
			//
			System.out.println("受影響的行數:" + rows);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, st, rs);
		}
		
	}

JDBC實現刪除數據-刪除—delete

參考以上代碼,可以進行SQL語句修改即可。

JDBC實現查詢數據-查詢—select

參考以上代碼,可以進行SQL語句修改即可。

select *from user where username='蒼老師';

案例:實現用戶登錄

  • 需求分析:
    1、提示用戶名和密碼的輸入
    2、根據輸入的用戶名和密碼,去到數據庫中訪問並且進行匹配,查詢出指定用戶名和密碼是否正確
    3、如果數據庫中能夠查詢到用戶,並且密碼也是正確的,那就可以進行登錄
    4、否則,登錄失敗。

代碼實現:

public class LoginUser {
	
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入用戶名:");
		String username = sc.nextLine();
		System.out.println("請輸入密碼:");
		String password = sc.nextLine();
		
		//模擬用戶登錄
		login(username, password);
		
		sc.close();
	}

	/**模擬用戶進行登錄的實現邏輯-----11:05*/
	private static void login(String username, String password) {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			//獲取數據庫的連接
			conn = JDBCUtil.getConnection();
			//獲取傳輸器對象
			st = conn.createStatement();

			rs = st.executeQuery("select * from user where username = '" + username + "' and password = '" + password + "'");
			if (rs.next()) {
				System.out.println("用戶登錄success");
			}else {
				System.out.println("登錄失敗。用戶名或者密碼錯誤。");
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, st, rs);
		}
	}
	

運行結果:

在這裏插入圖片描述
在這裏插入圖片描述
但是注意會出現問題:

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
原因:產生了SQL注入攻擊。

案例:登錄更新----防止SQL注入攻擊

  • 產生的原因:後臺在執行的時候,SQL語句是進行拼接的。

用戶名和密碼-----用戶提交的數據

  • SQL關鍵字:or and # – 等

  • 1、可以使用正則表達式進行校驗參數

  • 2、使用PreparedStatement對象替換Statement

先把SQL骨架發送給服務器。

只要SQL一旦被編譯,確定下來。

修改上面案例

/**模擬用戶進行登錄的實現邏輯----*/
	private static void login(String username, String password) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			//獲取數據庫的連接
			conn = JDBCUtil.getConnection();
			//獲取傳輸器對象
			String sql = "select * from user where username=? and password=?";
			ps = conn.prepareStatement(sql);
			//設置參數:
			ps.setString(1, username);
			ps.setString(2, password);
			
			rs = ps.executeQuery();//注意:不要傳SQL
			
			if (rs.next()) {
				System.out.println("用戶登錄success");
			}else {
				System.out.println("登錄失敗。用戶名或者密碼錯誤。");
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
	}
	

運行結果分析:

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

數據庫連接池—C3P0

直接上操作:

有不懂的地方可以留言哈,給解決。
在這裏插入圖片描述
新建文件:根目錄src下即可。
在這裏插入圖片描述

在這裏插入圖片描述
導入C3P0的jar包

/**模擬用戶進行登錄的實現邏輯-----11:05*/
	private static void login(String username, String password) {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		
		//連接池對象
		ComboPooledDataSource pool = new ComboPooledDataSource();
		
		try {
			//獲取數據庫的連接
			//conn = JDBCUtil.getConnection();
			conn = pool.getConnection();
			//獲取傳輸器對象
			st = conn.createStatement();

			rs = st.executeQuery("select * from user where username = '" + username + "' and password = '" + password + "'");
			if (rs.next()) {
				System.out.println("用戶登錄success");
			}else {
				System.out.println("登錄失敗。用戶名或者密碼錯誤。");
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, st, rs);
		}
	}
	

測試結果:

在這裏插入圖片描述
完美運行。

感謝查看。更多的請關注,謝謝。後續繼續更新。

老鐵,關注一波666

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章