连接数据库实现增删改查
数据库内容
IDEA内Bean内容
package com.media.bean;
public class UserBean {
private String username;
private String password;
private String gender;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Index 注册页面
<%--
Created by IntelliJ IDEA.
User: 了
Date: 2020/9/23
Time: 9:00
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<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>
Register页面使用Statement方法实现用户增加
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Driver" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %><%--
Created by IntelliJ IDEA.
User: 了
Date: 2020/9/23
Time: 9:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<jsp:useBean id="user" class="com.media.bean.UserBean" scope="application"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
//UserBean user1=new UserBean();
//user.setUsername(request.getParameter("username"));
//out.print(user.getUsername());
//加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//建立数据库连接
String url = "jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";
Connection connection = DriverManager.getConnection(url);//获得链接对象
String sql = "INSERT INTO user (username,password,gender,age) VALUES('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";
Statement stmt = connection.createStatement();
int count = stmt.executeUpdate(sql);
if(count == 0){
out.print("注册失败");
}else{
out.print("注册成功");
}
%>
<jsp:getProperty name="user" property="username"/>
</body>
</html>
使用PreparedStatement执行sql语句
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Driver" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="javax.swing.plaf.nimbus.State" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="com.mysql.jdbc.PreparedStatement" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="com.name.user"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
//数据库操作
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");//报错,说明驱动包没有完成
//建立数据库连接 18.3.22.1
String url ="jdbc:mysql://127.0.0.1:3306/book";
Connection connection = DriverManager.getConnection(url,"book","book");
String sql = "insert into user(username,password,gender,age) values (?,?,?,?)";
PreparedStatement temt = connection.prepareStatement(sql);
temt.setString(1,request.getParameter("username"));
temt.setString(2,request.getParameter("password"));
temt.setString(3,request.getParameter("gender"));
temt.setInt(4,request.getParameter("age"));
ResultSet rs = temt.executeQuery();
截图如下
使用Statement进行删除操作
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
//数据库操作
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");//报错,说明驱动包没有完成
//建立数据库连接 18.3.22.1
String url ="jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";
Connection connection = DriverManager.getConnection(url);
//3创建Statement对象 执行sql语句 alt+enter 自动引入缺失的类
Statement stmt = connection.createStatement();
//删除
String sql="delete from user where id = 4 ";
int count = stmt.executeUpdate(sql);
if(count == 0){
out.print("删除失败");
}else{
out.print("删除成功");
}
%>
使用PreparedStatement进行删除操作
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//建立数据库连接
String url = "jdbc:mysql://localhost:3306/book";
Connection connection = DriverManager.getConnection(url,"root","root");
//预定义对象
String sql = "delete from user where id = ? ";//使用问号代替参数
PreparedStatement pstm = connection.prepareStatement(sql);
pstm.setInt(4);
截图如下
未进行操作前
进行操作
数据库内变化
使用Statement对象修改
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Driver" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %><%--
Created by IntelliJ IDEA.
User: 了
Date: 2020/9/23
Time: 9:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<jsp:useBean id="user" class="com.media.bean.UserBean" scope="application"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
//UserBean user1=new UserBean();
//user.setUsername(request.getParameter("username"));
//out.print(user.getUsername());
//加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//建立数据库连接
String url = "jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";
Connection connection = DriverManager.getConnection(url);//获得链接对象
//String sql = "INSERT INTO user (username,password,gender,age) VALUES('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";
Statement stmt = connection.createStatement();
//使用Statement 对象执行sql语句 增删改的数据操作使用executeUpdate
String sql = "update user set stadius = 3 where id= 3";
int count = stmt.executeUpdate(sql);
if(count == 0){
out.print("修改失败");
}else{
out.print("修改成功");
}
%>
<jsp:getProperty name="user" property="username"/>
</body>
</html>
使用PreparedStatement对象修改
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Driver" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="javax.swing.plaf.nimbus.State" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="com.mysql.jdbc.PreparedStatement" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="com.name.user"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
//数据库操作
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");//报错,说明驱动包没有完成
//建立数据库连接 18.3.22.1
String url ="jdbc:mysql://127.0.0.1:3306/book";
Connection connection = DriverManager.getConnection(url,"book","book");
String sql="update user set statius=? where statius=?";
PreparedStatement pstm=connection.prepareStatement(sql);
pstm.setString(1,3);
截图如下
未修改前
修改
数据库变化如下
使用Statement查询
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Driver" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %><%--
Created by IntelliJ IDEA.
User: 了
Date: 2020/9/23
Time: 9:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<jsp:useBean id="user" class="com.media.bean.UserBean" scope="application"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
//UserBean user1=new UserBean();
//user.setUsername(request.getParameter("username"));
//out.print(user.getUsername());
//加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//建立数据库连接
String url = "jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";
Connection connection = DriverManager.getConnection(url);//获得链接对象
//String sql = "INSERT INTO user (username,password,gender,age) VALUES('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";
Statement stmt = connection.createStatement();
//用户名username为123
String sql = "select * from user where username = '"+request.getParameter("username")+"'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
out.print(rs.getString("username")+"-"+rs.getString(1)+"<br>");
}
%>
<jsp:getProperty name="user" property="username"/>
</body>
</html>
使用PreparedStatement查找
//预定义对象
String sql = "select * from user where username = ? and password = ? ";//使用问号代替参数
PreparedStatement temt = connection.prepareStatement(sql);
temt.setString(1,request.getParameter("username"));
temt.setString(2,request.getParameter("password"));
ResultSet rs = temt.executeQuery();
//结果数据
while (rs.next())
{
out.print(rs.getString("username")+"-"+rs.getString(2)+"<br>");
}
截图如下
数据库内容
所有用户
查找用户 123