这是结构图jdbc连接数据库package jdbc;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import com.mysql.jdbc.Connection;public class jdbc {public static Connecti...
这是结构图
jdbc连接数据库
package jdbc;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.Connection;
public class jdbc {
public static Connection getConnection() throws SQLException,
ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/test";
String username="root";
String password="123456";
Connection con=(Connection) DriverManager.getConnection(url,username,password);
return con;
}
}
user存储用户
package user;
public class user {
private String Name;
private String Password;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
}
userdao是登录注册的操作
package userdao;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import jdbc.jdbc;
import user.user;
public class userdao{
public boolean select(user u) throws ClassNotFoundException, SQLException{
int x=0;
jdbc j=new jdbc();
Connection con=j.getConnection();
Statement stmt=(Statement) con.createStatement();
String sql="select Name,Password from t1";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(u.getName()+rs.getString(1));
if(u.getName().equals(rs.getString(1))){
System.out.println("能比较用户名了");
if(u.getPassword().equals(rs.getString(2))){
System.out.println("匹配成功了");
x=1;
break;
}
}
}
if(x==1)
return true;
else
return false;
}
public boolean insert(user u) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
jdbc j=new jdbc();
Connection con=j.getConnection();
Statement stmt=(Statement) con.createStatement();
System.out.println("得到连接");
String sql="insert into t1 values('"+u.getName()+"','"+u.getPassword()+"')";
int s=stmt.executeUpdate(sql);
if(s>0)
return true;
return false;
}
}
RegistServlet 是注册功能的servlet
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import user.user;
import userdao.userdao;
/**
* Servlet implementation class RegistServlet
*/
public class RegistServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("name");
String password=request.getParameter("password");
user u=new user();
u.setName(username);
u.setPassword(password);
userdao us=new userdao();
boolean t = false;
try {
t=us.insert(u);
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(t==true){
response.sendRedirect("/Servlet2/form.html");
}else{
response.sendRedirect("/Servlet2/regist.html");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
LoginServlet是登录功能的servlet
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import user.user;
import userdao.userdao;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
user u=new user();
String username=request.getParameter("username");
String password=request.getParameter("password");
u.setName(username);
u.setPassword(password);
userdao us=new userdao();
boolean x=false;
try {
x=us.select(u);
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(x==true){
response.sendRedirect("/Servlet2/welcome.html");
}else{
response.sendRedirect("/Servlet2/form.html");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
toregist是跳转页面的servlet
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class toregist
*/
public class toregist extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.sendRedirect("/Servlet2/regist.html");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
mysql数据库结构如图
沃梦达教程
本文标题为:servlet+html+mysql实现登录注册功能
基础教程推荐
猜你喜欢
- ajaxFileupload实现多文件上传功能 2023-02-14
- 关于 html:CSS – 调整浏览器窗口大小时修复边 2022-09-21
- AJAX跨域问题解决方案详解 2023-02-24
- 前端JS,删除JSON数据(JSON数组)中的指定元素方式 2023-07-10
- AJax 把拿到的后台数据在页面中渲染的实例 2023-02-23
- Ajax 入门之 GET 与 POST 的不同处详解 2023-01-31
- 关于 sencha touch:ExtJS、SenchaTouch – FormPanel 无 2022-09-15
- 【Layui】当Layui数据表格和Layui下拉框组合时发生的问题 2022-12-14
- Ajax客户端异步调用服务端的实现方法(js调用cs文件) 2023-02-15
- 一文掌握ajax、fetch和axios的区别对比 2023-02-24