数据库增删改查
程序员文章站
2022-03-27 12:56:27
编写一个学生注册的页面
编写一个学生注册的页面
<head>
<title>注册页面</title>
<link rel="stylesheet" href="css/bootstrap.css">
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/bootstrap.bundle.js"></script>
<style>
.container{
width: 20%;
height: 20%;
margin-top: 10%;
}
</style>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<form action="register.jsp" method="post">
用户名:<input type="text" name="username" class="form-control"><br>
密码:<input type="text" name="password" class="form-control"><br>
性别:<input type="text" name="gender" class="form-control"><br>
年龄:<input type="text" name="age" class="form-control"><br>
<button type="submit">注册</button>
</form>
</div>
</div>
</div>
</body>
注册成功提示页面
<jsp:useBean id="user" class="com.media.bean.userbean"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/book";
Connection connection = DriverManager.getConnection(url,"root","root");
Statement stmt = connection.createStatement();
String sql = "inser into user(username,password,gender,age) valuse("+user.getUsername()+","+user.getPassword()+",1,20)";
int count = stmt.executeUpdate(sql);
if(count >0){
out.print("注册成功");
}else{
out.print("注册失败");
}
%>
编写一个登录页面,建立一个登陆的表单
<html>
<head>
<title>登录页面</title>
<%--引入css文件和js文件--%>
<link rel="stylesheet" href="css/bootstrap.css">
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/bootstrap.bundle.js"></script>
<style>
.container{
width: 20%;
height: 20%;
margin-top: 10%;
}
</style>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<form role="form" method="post" action="index.jsp">
<h3 style="text-align: center"登录></h3>
<div class="form-group">
<label for="exampleInputEmail1">用户名:</label><input name="username" class="form-control" id="exampleInputEmail1" type="text" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">密码:</label><input name="password" class="form-control" id="exampleInputPassword1" type="password" />
</div>
<button class="btn btn-default" type="submit">提交</button>
</form>
</div>
</div>
</div>
</body>
</html>
连接book数据库
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立数据库连接
String url = "jdbc:mysql://localhost:3306/book";//数据库连接地址
Connection connection = DriverManager.getConnection(url, "root", "root");
String sql = "select * from user where username = ? and password = ?";
//使用PreparedStatement对象执行sql语句
PreparedStatement ps = connection.prepareStatement(sql);
删除数据库操作代码如下:
//stmt.executeUpdate()执行删改的驱动
String sql = "delete from user where id=?";
PreparedStatement ps = connection.prepareStatement(sql);
int id = Integer.parseInt(request.getParameter("id"));
ps.setInt(1,id);
int count = ps.executeUpdate();
if (count > 0){
out.print("删除成功");
}else{
out.print("删除失败");
}
编辑数据库操作页面的代码:
String sql = "select * from user where id=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1,id);
ResultSet rs = ps.executeQuery();
while (rs.next()){
out.print("<input type='hidden' name='id' value="+rs.getString("id")+">");
out.print("用户名:<input type='text' class='form-control' name='username' value="+rs.getString("username")+"><br>");
out.print("密码:<input type='text' class='form-control' name='password' value="+rs.getString("password")+"><br>");
out.print("性别:<input type='text' class='form-control' name='gender' value="+rs.getString("gender")+"><br>");
out.print("年龄:<input type='text' class='form-control' name='age' value="+rs.getString("age")+"><br>");
}
<button type="submit" style="width: 100%" class="btn btn-info">提交</button>
</form>
</div>
</div>
</div>
编辑成功提示页面
//stmt.executeUpdate()执行修改的驱动
String sql = "update user set username = ?, password = ?,gender = ?,age = ? where id = ?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,user.getUsername());
ps.setString(2,user.getUsername());
ps.setInt(3,user.getGender());
ps.setInt(4,user.getAge());
ps.setInt(5,user.getId());
int count = ps.executeUpdate();
if (count >0){
out.print("修改成功");
}else{
out.print("修改失败");
}
写一个添加用户的表单:
<div class="modal fade" id="modal-container-addUser" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">添加用户</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<form role="form" method="post" action="add.jsp">
<div class="modal-body">
<div class="form-group">
<label>用户名:</label><input type="text" name="username" class="form-control" />
</div>
<div class="form-group">
<label>密码:</label><input type="password" name="password" class="form-control" />
</div>
<div class="form-group">
<label>性别:</label><input type="text" name="gender" class="form-control" />
</div>
<div class="form-group">
<label>年龄:</label><input type="text" name="age" class="form-control" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="submit" class="btn btn-primary">添加</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
添加成功提示页面的代码:
String sql = "insert into user (username,password,gender,age) values (?,?,?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,user.getUsername());
ps.setString(2,user.getUsername());
ps.setInt(3,user.getGender());
ps.setInt(4,user.getAge());
int count = ps.executeUpdate();
if (count >0){
out.print("添加成功");
}else{
out.print("添加失败");
}
response.setHeader("refresh","3;url=home.jsp");
本文地址:https://blog.csdn.net/xiao_feiy/article/details/109238426
上一篇: Java对象的‘生老病死’
下一篇: jmu-枚举WeekDay(补全代码)