JavaWeb開發之JDBC的增刪改查


項目搭建的包命名和類命名:

 

首先導入數據庫的驅動架包,並添加依賴

 

 

1.Dao層是專門負責和數據庫交互,數據處理的代碼層

2.Dao是接口,DaoImpl是Dao接口的實現類

3.Java代碼連接數據庫的步驟:“賈璉欲執事”口訣

  (1)賈:加載數據庫注冊驅動,將數據庫驅動注冊到DriverManager中去。

    Class.forName("com.mysql.jdbc.Driver");

 

  (2)璉:創建連接數據庫的對象,通過DriverManager獲取

  Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "123456");

 

  (3)欲:創建預編譯或非預編譯 SQL語句對象

  String sql="update t_student set name = ?,age = ? where id = ?";

  PrepardStatment ps =conn.preparedStatment(sql);

  ps.setString(1,"小明");

  ps.setInt(2,20);

  ps.setInt(3,2);

 

  (4)執:執行SQL語句

  int row =ps.executeUpdate();

 

  (5)事:釋放資源(先創后放)

  conn.close();

  ps.close();

 

具體代碼:

	@Override
	public int insert(Student stu) {

		String sql = "insert into t_student(name,age)values(?,?)";
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			// 1.加載注冊驅動
			Class.forName("com.mysql.jdbc.Driver");

			// 2.獲取數據庫連接對象
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "root");
			// 3.創建預編譯語句對象
			ps = conn.prepareStatement(sql);

			// 3.1設置預編譯語句對象占位符對應的參數值
			ps.setString(1, stu.getName());
			ps.setInt(2, stu.getAge());
			// 4.執行SQL語句 (注意:方法不能帶sql參數)
			int row = ps.executeUpdate();
			return row;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (conn != null) {
						conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return 0;
	}

	@Override
	public int delete(Integer id) {
		String sql = "delete from t_student where id = ?";
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			// 1.加載注冊驅動
			Class.forName("com.mysql.jdbc.Driver");

			// 2.獲取數據庫連接對象
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "root");
			// 3.創建預編譯語句對象
			ps = conn.prepareStatement(sql);

			// 3.1設置預編譯語句對象占位符對應的參數值
			ps.setInt(1, id);
			// 4.執行SQL語句 (注意:方法不能帶sql參數)
			int row = ps.executeUpdate();
			return row;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (conn != null) {
						conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return 0;
	}

	@Override
	public int update(Student stu) {
		String sql = "update t_student set name = ?,age = ? where id = ?";
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			// 1.加載注冊驅動
			Class.forName("com.mysql.jdbc.Driver");

			// 2.獲取數據庫連接對象
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "root");
			// 3.創建預編譯語句對象
			ps = conn.prepareStatement(sql);

			// 3.1設置預編譯語句對象占位符對應的參數值
			ps.setString(1, stu.getName());
			ps.setInt(2, stu.getAge());
			ps.setInt(3, stu.getId());
			// 4.執行SQL語句 (注意:方法不能帶sql參數)
			int row = ps.executeUpdate();
			return row;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (conn != null) {
						conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return 0;
	}

	@Override
	public Student selectByPrimaryKey(Integer id) {

		String sql = "select * from t_student where id = ?";

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// 1.加載注冊驅動
			Class.forName("com.mysql.jdbc.Driver");
			// 2.獲取數據庫連接對象
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "root");
			// 3.創建預編譯語句對象
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			// 4.執行sql語句
			rs = ps.executeQuery();

			while (rs.next()) {
				String name = rs.getString("name");
				int age = rs.getInt("age");

				// 封裝Student對象
				Student stu = new Student(id, name, age);
				//返回Student對象
				return stu;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (ps != null) {
						ps.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					try {
						if (conn != null) {
							conn.close();
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return null;
	}

	@Override
	public List<Student> selectList() {

		List<Student> students = new ArrayList<>();

		String sql = "select * from t_student";

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// 1.加載注冊驅動
			Class.forName("com.mysql.jdbc.Driver");
			// 2.獲取數據庫連接對象
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "root");
			// 3.創建預編譯語句對象
			ps = conn.prepareStatement(sql);

			// 4.執行sql語句
			rs = ps.executeQuery();

			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				int age = rs.getInt("age");

				// 封裝Student對象
				Student stu = new Student(id, name, age);

				// 學生對象添加到集合中
				students.add(stu);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (ps != null) {
						ps.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					try {
						if (conn != null) {
							conn.close();
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}

		return students;
	}

  

  

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM