首先创建一个简单的数据库:请看表结构:id就当作用户名,stu_id当作密码吧。数据库名叫db_jxgl,里面只有一张student表,具体请看下图:
使用过IDEA之后好像从农村来到了城市,太TM强大了!!! 先创建数据库访问类:
package com.jmdx.bingo.sql;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
public class MysqlDBConn {
private PreparedStatement ps;
private Connection conn;
ResultSet res;
public static final String url = "jdbc:mysql://localhost:3306/db_jxgl?useSSL=false";
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "123456";
public MysqlDBConn() {
ps = null;
res = null;
try {
Class.forName(name);
conn = DriverManager.getConnection(url,user,password);
}catch (Exception e) {
e.printStackTrace();
}
}
public ResultSet Query(String sql,Map<Integer,Integer> map) {
try {
ps = conn.prepareStatement(sql);
for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
ps.setInt(entry.getKey(),entry.getValue());
}
res = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
public String getResult(ResultSet result) {
String uName = "";
try {
while (result.next()) {
uName = result.getString("name");
// System.out.println("name:"+userName);
}
} catch (SQLException e) {
e.printStackTrace();
}
return uName;
}
public static void main(String[] args) {
String sql = "select name from student where id = ? and stu_id = ?";
MysqlDBConn conn = new MysqlDBConn();
Map<Integer,Integer> map = new HashMap<>();
map.put(1,1);
map.put(2,901);
ResultSet res = conn.Query(sql,map);
// System.out.println(res.toString());
String str = conn.getResult(res);
System.out.println(str);
}
}
然后是Servlet:
package com.jmdx.bingo.servlet;
import com.jmdx.bingo.sql.MysqlDBConn;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
public class Login extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
response.setHeader("Content-type","text/html;charset=UTF-8");
String name = request.getParameter("username");
String pwd = request.getParameter("password");
String user = login(name,pwd);
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
writer.write("<h1>你好, "+user+"</h1>");
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
doPost(request,response);
}
public String login (String id,String passwd) {
Integer uId = Integer.valueOf(id);
Integer uPasswd = Integer.valueOf(passwd);
String sql = "select name from student where id = ? and stu_id = ?";
Map<Integer,Integer> map = new HashMap<>();
map.put(1,uId);
map.put(2,uPasswd);
MysqlDBConn conn = new MysqlDBConn();
ResultSet res = conn.Query(sql,map);
String uName = conn.getResult(res);
return uName;
}
}
首页:(没有写样式,简单的用空格排版了。)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<h1>个人门户</h1>
<form method="post" action="/hello">
用户id:<input type="text" name="username"> <br><br>
密   码:<input type="password" name="password"><br><br>    
<input type="submit" value="登陆">    
<input type="reset" value="取消">
</form>
</body>
</html>
效果: