-
环境准备 1.创建表 create database newdb3 character set utf8;
use newdb3;
create table user2(
id int primary key auto_increment,
username varchar(20),
password varchar(20)
)charset=utf8;
- 数据库链接文件
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/newdb3?useUnicode=true&characterEncoding=UTF-8
username=root
password=
DBUtils.java package cn.tedu.servlet;
import org.apache.commons.dbcp.BasicDataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtils {
private static BasicDataSource ds;
static {
InputStream is = DBUtils.class.getClassLoader()
.getResourceAsStream("jdbc.properties");
Properties p = new Properties();
try {
p.load(is);
} catch (IOException e) {
e.printStackTrace();
}
String driver = p.getProperty("driver");
String url = p.getProperty("url");
String username = p.getProperty("username");
String password = p.getProperty("password");
ds = new BasicDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
ds.setInitialSize(3);
ds.setMaxActive(5);
ds.setMaxIdle(3);
}
public static Connection getConn() throws SQLException {
Connection conn = ds.getConnection();
return conn;
}
}
-
注册、登录、修改密码功能
<body>
<form action="RegServlet" method="post">
账号:<input type="text" name="username" placeholder="请输入用户名"><br>
密码:<input type="password" name="password" placeholder="请输入密码"><br>
<input type="submit" value="注册">
</form>
</body>
public class RegServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username+":"+password);
try(Connection conn = DBUtils.getConn()){
String sql="insert into user2 values(null,?,?)";
PreparedStatement st = conn.prepareStatement(sql);
st.setString(1,username);
st.setString(2,password);
int num = st.executeUpdate();
if (num>0){
System.out.println("注册成功");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("注册成功");
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
<body>
<form action="LoginServlet" method="post">
账号:<input type="text" name="username" placeholder="请输入账号"><br>
密码:<input type="password" name="password" placeholder="请输入密码"><br>
<input type="submit" value="登录">
</form>
</body>
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username+":"+password);
try(Connection conn = DBUtils.getConn()){
String sql = "select count(*) from user2 where username=? and password=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
ResultSet rs = ps.executeQuery();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
while (rs.next()){
int count = rs.getInt(1);
if (count>0){
System.out.println("console:登录成功");
out.print("登录成功");
}else {
System.out.println("console:登录失败");
out.print("登录失败");
}
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
删除
<body>
<form action="UpdateServlet" method="post">
账号:<input type="text" name="username" placeholder="请输入用户名"><br>
原密码:<input type="password" name="oldpwd" placeholder="请输入密码"><br>
新密码:<input type="password" name="newpwd" placeholder="请输入密码"><br>
<input type="submit" value="修改">
</form>
</body>
public class UpdateServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String newpwd = request.getParameter("newpwd");
String oldpwd = request.getParameter("oldpwd");
System.out.println(username+","+newpwd+","+oldpwd);
try(Connection conn = DBUtils.getConn()){
String sql = "update user2 set password=? where username=? and password=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,newpwd);
ps.setString(2,username);
ps.setString(3,oldpwd);
int num = ps.executeUpdate();
System.out.println(num);
if (num>0){
System.out.println("修改成功");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("修改成功");
out.close();
}else{
System.out.println("修改失败");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("修改失败");
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
|