首先準備數(shù)據(jù)庫數(shù)據(jù)和登錄頁面
創(chuàng)建user數(shù)據(jù)表并且插入幾條數(shù)據(jù)
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`password` varchar(16) DEFAULT NULL,
`email` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('1', 'zhangsan', '123456', '1111@qq.com');
INSERT INTO `user` VALUES ('2', 'lisi', '123321', '2222@qq.com');
INSERT INTO `user` VALUES ('3', 'wangwu', '111222', '3333@qq.com');
登錄頁面index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<h1>歡迎登陸</h1>
<form action="login" method="post">
用戶名<input type="text" placeholder="請輸入用戶名" name="user"><br />
密碼<input type="password" placeholder="請輸入密碼" name="pwd"> <input
type="submit" value="登陸">
</form>
</body>
</html>
JCDB工具類
注意先導入必要的jar包
DBUtil.java
package com.wangc.utils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
public class DBUtil {
public static final String DRIVERNAME = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/javadata";//注意數(shù)據(jù)庫名
public static final String USERNAME = "root";//使用自己的用戶名和密碼
public static final String PASSWORD = "****";//使用自己的用戶名和密碼
public static BasicDataSource bSource = null;
static{
bSource = new BasicDataSource();
bSource.setDriverClassName(DRIVERNAME);
bSource.setUrl(URL);
bSource.setUsername(USERNAME);
bSource.setPassword(PASSWORD);
}
public static Connection getConn(){
try {
return bSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void release(Connection conn, Statement stat, ResultSet rs){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stat != null) {
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
LoginServlet.java
package com.wangc.control;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wangc.utils.DBUtil;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
try {
conn = DBUtil.getConn();
String sql = "select * from user where name=? and password=?";
ps = conn.prepareStatement(sql);
ps.setString(1, request.getParameter("user"));
ps.setString(2, request.getParameter("pwd"));
rs = ps.executeQuery();
if (rs.next()) {
response.getWriter().write("登陸成功瑟捣!");
}else {
response.getWriter().write("登陸失敯木臁!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.release(conn, ps, rs);
}
}
}
測試一下
輸入
zhangsan
123456
點擊登錄
輸入
zhangsan
1234