dao层
package dao;
import java.sql.*;
import Shiti.Userti;
public class UserDao {
// 加载数据库驱动 com.mysql.jdbc.Driver
private final String driver = "com.mysql.cj.jdbc.Driver";
// 获取mysql连接地址
private final String url = "jdbc:mysql://localhost:3306/test?&useSSL=false&serverTimezone=UTC";
// 数据名称
private final String username = "root";
// 数据库密码
private final String password = "root";
//添加
public boolean add(Userti userti){
Connection conn = null;
ResultSet rs=null;
PreparedStatement statement=null;
// 获取连接的一个状态
try{
Class.forName(driver);
//getConnection()方法,连接MySQL数据库!
conn=DriverManager.getConnection(url,username,password);
//创建statement类对象,用来执行SQL语句!
//要执行的SQL语句
String sql ="insert into user values (?,?,? )";
statement=conn.prepareStatement(sql);
statement.setInt(1,userti.getAcc());
statement.setString(2,userti.getPsw());
statement.setString(3,userti.getName());
statement.execute();
return true;
}
catch(ClassNotFoundException e){
//数据库驱动类异常处理
e.printStackTrace();
return false;
}
catch(SQLException e1){
//数据库连接失败异常处理
e1.printStackTrace();
return false;
}
catch(Exception e2){
e2.printStackTrace();
return false;
}
finally {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
service层
package service;
import Shiti.Userti;
import dao.DingdanDao;
import dao.UserDao;
public class Userservice {
UserDao userDao=new UserDao();
//注册
public boolean zhuce(Userti userti){
boolean a=userDao.add(userti);
return a;
}
}
servle层
package Servlet;
import Shiti.Userti;
import service.Userservice;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
public class AddUserservlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
int acc= Integer.parseInt(request.getParameter("acc1"));
String psw = request.getParameter("psw1");
String name =request.getParameter("name1");
Userti userti=new Userti(acc,psw,name);
Userservice userservice=new Userservice();
boolean a=userservice.zhuce(userti);
if (a) {
out.println("注册成功");
}else {
out.println("注册失败");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
jsp层
<%@ page contentType="text/html;charset=UTF-8" language="java"
pageEncoding="utf-8" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>111</title>
</head>
<body>
<form action="Servlet/AddUserservlet">
acc:<input type="text" name="acc1">
psw:<input type="text" name="psw1">
name:<input type="text" name="name1">
<input type="submit" value="注册">
</form>
</body>
</html>