1.创建注册表单
<%--
Created by IntelliJ IDEA.
User: 发
Date: 2020/10/2
Time: 9:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form method="post" action="register.jsp">
用户名:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
性别:<input type="text" name="gender"><br>
年龄:<input type="text" name="age"><br>
<button type="submit">注册</button>
</form>
</body>
</html>
2.创建register.jsp(插入注册数据)
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="com.media.bean.UserBean" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
UserBean user = new UserBean();
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://10.3.22.1/jhy28?user=root&password=root";
Connection connection = DriverManager.getConnection(url);
Statement stmt = connection.createStatement();
String sql = "insert into user (username,password,gender,age) values('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";
int count = stmt.executeUpdate(sql);
if(count == 0 ){
out.print("注册失败");
}else{
out.print("注册成功");
}
%>
3.利用register删除数据
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="com.media.bean.UserBean" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
//user对象实例化
UserBean user = new UserBean();
//给对象赋值
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
//数据库操作
//1 加载驱动程序
Class.forName("com.mysql.jdbc.Driver"); //如果执行到此位置报错 标识驱动的jar包没有加载成功
//2 建立数据库连接 10.3.22.1
String url="jdbc:mysql://10.3.22.1/jhy28?user=root&password=root";//book 为数据库名
Connection connection = DriverManager.getConnection(url);
Statement stmt = connection.createStatement();
String sql = "delete from user where id = 11";
int count = stmt.executeUpdate(sql);
if(count > 0 ){
out.print("失败");
}else{
out.print("成功");
}
%>
4
.修改数据
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="com.media.bean.UserBean" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
//user对象实例化
UserBean user = new UserBean();
//给对象赋值
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
//数据库操作
//1 加载驱动程序
Class.forName("com.mysql.jdbc.Driver"); //如果执行到此位置报错 标识驱动的jar包没有加载成功
//2 建立数据库连接 10.3.22.1
String url="jdbc:mysql://10.3.22.1/book?user=root&password=root";//book 为数据库名
Connection connection = DriverManager.getConnection(url);//获得连接对象 //alt + 回车 自动导入类 如果报此行有问题说明 数据库的连接地址或用户名和密码有问题
//3 创建Statement对象 执行sql语句 alt+enter 自动引入缺失的类
Statement stmt = connection.createStatement();
// sql 插入语句值是字符串 必须使用单引号
//String sql = "insert into user (username,password,gender,age) values('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";
String sql = "update user set status = 3 where id = 11";
// String sql = "delete from user where id = 1"; // 执行sql语句时id 保证数据库中存在该数据
//使用Statement 对象执行sql语句 增删改的数据库操作使用executeUpdate
int count = stmt.executeUpdate(sql); //如果报此行有错误 数码上一行的sql语句是有问题的 请检查数据库
if(count == 0 ){
out.print("失败"); //如果能执行到此位置 表示数据库操作是成功 数据库的数据是没有发生变化 否则的肯定有异常
}else{
out.print("成功");
}
%>
5.查看数据
<%@ page import="java.sql.*" %><%--
Created by IntelliJ IDEA.
User: 发
Date: 2020/10/6
Time: 14:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//建立数据库链接
String url="jdbc:mysql://10.3.22.1/jhy28?user=root&password=root";
Connection connection = DriverManager.getConnection(url,"root","root");
// String sql="select * from user where username= ? and password=?";//使用?代替参数
PreparedStatement stmt =connection.prepareStatement(sql);
// pstm.setString(1,request.getParameter("username"));
// pstm.setString(2,request.getParameter("paswword"));
// ResultSet rs =stmt.executeQuery();
//遍历结果集数据
// while (rs.next())
// {
// out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");
// }
//创建statement对象
Statement stmt=connection.createStatement();
String sql="select * from user where username='"+request.getParameter("username")+"'";
//select *from user where username='admin3' or '1'='1' //数据库注入
//执行查询 返回结果集
ResultSet rs =stmt.executeQuery(sql);
//遍历结果集数据
while (rs.next())
{
out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");
}
%>
</body>
</html>