2021-11-04
2021/11/5 6:13:47
本文主要是介绍2021-11-04,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
用servlet连接数据库实现登陆验证
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
<table>
账号:<input type="text" name="sname" value="张三">
密码:<input type="password" name="spwd" value="123">
<input type="submit" value="登录">
</table>
</form>
</body>
</html>
public interface Dao {
Student login(String sname, String spwd);
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DaoImpl implements Dao{
@Override
public Student login(String sname, String spwd) {
Student student = null;
Util u = new Util();
Connection conn = u.getConn();
try {
PreparedStatement ps = conn.prepareStatement("select * from student where sname=? and spwd=?");
ps.setString(1,sname);
ps.setString(2, spwd);
ResultSet rs =ps.executeQuery();
while(rs.next()) {
int sid = rs.getInt("sid");
int cid = rs.getInt("cid");
student = new Student(sid, sname, spwd, cid);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return student;
}
}
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Servlet
*/
@WebServlet("/login")
public class Servlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String sname = request.getParameter("sname");
String spwd = request.getParameter("spwd");
System.out.println(sname+""+spwd);
Dao d = new DaoImpl();
Student s = d.login(sname,spwd);
if (s!=null) {
System.out.println("登陆成功");
}else {
System.out.println("登陆失败");
}
}
}
public class Student {
private int sid;
private String sname;
private String spwd;
private int cid;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSpwd() {
return spwd;
}
public void setSpwd(String spwd) {
this.spwd = spwd;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public Student(int sid, String sname, String spwd, int cid) {
this.sid = sid;
this.sname = sname;
this.spwd = spwd;
this.cid = cid;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Util {
private static String URL = "jdbc:mysql://localhost:3306/mydb";
private static String NAME = "root";
private static String PWD = "123456";
private static Connection conn;
public static Connection getConn(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL,NAME,PWD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
}
return conn;
}
public void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// public static void main(String[] args) {
// System.out.println(getConn());
// }
}
这篇关于2021-11-04的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南