7.4 JDBC数据库操作
1.核心知识点
2.任务1--查询数据库
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP连接数据库实例,查询记录</title>
</head>
<body>
<%
//1.加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.通过DriverManger获取数据库连接
String url="jdbc:mysql://localhost:3306/jdbc";
String userName="root";
String passWord="root";
Connection conn=DriverManager.getConnection(url,userName,passWord);
//3.通过Connection对象获取Statement
Statement stmt=conn.createStatement();
//4.使用Statement执行SQL语句
String sql="select * from users";
ResultSet rs=stmt.executeQuery(sql);
//5.操作ResultSet结果集
%>
<table border="1">
<tr>
<td>id</td><td>name</td><td>password</td><td>email</td><td>birthday</td>
</tr>
<%
while(rs.next()){
int id=rs.getInt("id"); //通过列名获取指定的值
String name=rs.getString("name");
String password=rs.getString("password");
String email=rs.getString("email");
String birthday=rs.getString("birthday");
%>
<tr>
<td><%=id %></td><td><%=name %></td><td><%=password %></td><td><%=email %></td><td><%=birthday %></td>
</tr>
<%
}
//6.回收数据库资源
rs.close();
stmt.close();
conn.close();
%>
</table>
</body>
</html>
3.任务2--插入数据记录
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP连接数据库实例,插入记录</title>
</head>
<body>
<%
//1.加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.通过DriverManger获取数据库连接
String url="jdbc:mysql://localhost:3306/jdbc";
String userName="root";
String passWord="root";
Connection conn=DriverManager.getConnection(url,userName,passWord);
//3.使用PreparedStatement执行SQL语句
String sql="insert into users(name,password,email,birthday)values(?,?,?,?)";
//(1)创建执行SQL语句的PreparedStatement对象
PreparedStatement preStmt=conn.prepareStatement(sql);
//(2)为SQL语句中的参数赋值
preStmt.setString(1,"Tom");
preStmt.setString(2,"tom");
preStmt.setString(3,"4455566@qq.com");
preStmt.setString(4,"1998-01-23");
//(3)执行SQL
preStmt.executeUpdate();
//(4)回收数据库资源
preStmt.close();
conn.close();
%>
</body>
</html>
4.任务3--删除数据记录
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP连接数据库实例,删除记录</title>
</head>
<body>
<%
//1.加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.通过DriverManger获取数据库连接
String url="jdbc:mysql://localhost:3306/jdbc";
String userName="root";
String passWord="root";
Connection conn=DriverManager.getConnection(url,userName,passWord);
//3.使用PreparedStatement执行SQL语句
String sql="delete from users where id=?";
//(1)创建执行SQL语句的PreparedStatement对象
PreparedStatement preStmt=conn.prepareStatement(sql);
//(2)为SQL语句中的参数赋值
preStmt.setInt(1,4); //删除id=4的1条记录
//(3)执行SQL
preStmt.executeUpdate();
//4回收数据库资源
preStmt.close();
conn.close();
%>
</body>
</html>
5.任务4--更新数据记录
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP连接数据库实例,更新记录</title>
</head>
<body>
<%
//1.加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.通过DriverManger获取数据库连接
String url="jdbc:mysql://localhost:3306/jdbc";
String userName="root";
String passWord="root";
Connection conn=DriverManager.getConnection(url,userName,passWord);
//3.使用PreparedStatement执行SQL语句
String sql="update users set password=? where id=?";
//(1)创建执行SQL语句的PreparedStatement对象
PreparedStatement preStmt=conn.prepareStatement(sql);
//(2)为SQL语句中的参数赋值
preStmt.setString(1,"abc");
preStmt.setInt(2,5);
//(3)执行SQL
preStmt.executeUpdate();
//4.回收数据库资源
preStmt.close();
conn.close();
%>
</body>
</html>