DBUtil.java
package a;
import java.sql.*;
import javax.servlet.http.HttpSession;
public class DBUtil {
boolean bInited = false;
private HttpSession request;
// 加载驱动
public void initJDBC() throws ClassNotFoundException {
// 加载MYSQL JDBC驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
bInited = true;
System.out.println("Success loading sql Driver!");
}
public Connection getConnection() throws ClassNotFoundException,
SQLException {
if (!bInited) {
initJDBC();
}
// 连接URL为 jdbc:mysql//服务器地址/数据库名
// 后面的2个参数分别是登陆用户名和密码
String url = "jdbc:sqlserver://localhost:1433;databaseName=classcircle";
String userName = "sa";
String password = "123456789123456";
Connection conn = DriverManager.getConnection(url, userName, password);
return conn;
}
public boolean loginSuccess(String userName, String password) {
boolean returnValue = false;
String sql = "SELECT * FROM headteacher";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String userNameInDB = rs.getString("username");
String passwordInDB = rs.getString("password");
if (userNameInDB.equals(userName)
&& passwordInDB.equals(password)) {
returnValue = true;
String name = rs.getString("name");
request.setAttribute("name", name);//**我就是加了这段代码**
System.out.println(name);
break;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return returnValue;
}
public boolean loginSuccess2(String userName, String password) {
boolean returnValue = false;
String sql = "SELECT * FROM teacher";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String userNameInDB = rs.getString("username");
String passwordInDB = rs.getString("password");
if (userNameInDB.equals(userName)
&& passwordInDB.equals(password)) {
returnValue = true;
break;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return returnValue;
}
public boolean loginSuccess3(String userName, String password) {
boolean returnValue = false;
String sql = "SELECT * FROM parents";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String userNameInDB = rs.getString("username");
String passwordInDB = rs.getString("password");
if (userNameInDB.equals(userName)
&& passwordInDB.equals(password)) {
returnValue = true;
break;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return returnValue;
}
public boolean loginSuccess4(String userName, String password) {
boolean returnValue = false;
String sql = "SELECT * FROM student";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String userNameInDB = rs.getString("username");
String passwordInDB = rs.getString("password");
if (userNameInDB.equals(userName)
&& passwordInDB.equals(password)) {
returnValue = true;
break;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return returnValue;
}
}
servelet
package a;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
public class loginServlet implements javax.servlet.Servlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter("username");// 取得用户名
String password = request.getParameter("password");// 取得密码
String radio = request.getParameter("radio");//单选框
System.out.println(radio);
DBUtil db = new DBUtil();// 构建数据库对象
if ("1".equals(radio)) {
boolean canLogin = db.loginSuccess(userName, password);
if (canLogin) {// 根据登陆情况,跳转页面
System.out.println("登陆成功!");
response.sendRedirect("zhuyeheader.jsp");
} else {
JOptionPane.showMessageDialog(null, "账号或密码错误");
response.sendRedirect("denglu.jsp");
}
}
else if ("2".equals(radio)) {
boolean canLogin = db.loginSuccess2(userName, password);
if (canLogin) {// 根据登陆情况,跳转页面
System.out.println("登陆成功!");
response.sendRedirect("zhuyeteacher.jsp");
} else {
JOptionPane.showMessageDialog(null, "账号或密码错误");
response.sendRedirect("denglu.jsp");
}
}
else if ("3".equals(radio)) {
boolean canLogin = db.loginSuccess3(userName, password);
if (canLogin) {// 根据登陆情况,跳转页面
System.out.println("登陆成功!");
response.sendRedirect("zhuyepparents.jsp");
} else {
JOptionPane.showMessageDialog(null, "账号或密码错误");
response.sendRedirect("denglu.jsp");
}
}
else{
boolean canLogin = db.loginSuccess4(userName, password);
if (canLogin) {// 根据登陆情况,跳转页面
System.out.println("登陆成功!");
response.sendRedirect("zhuyestudent.jsp");
} else {
JOptionPane.showMessageDialog(null, "账号或密码错误");
response.sendRedirect("denglu.jsp");
}
}
}
public void destroy() {
}
public ServletConfig getServletConfig() {
return null;
}
public String getServletInfo() {
return null;
}
public void init(ServletConfig arg0) throws ServletException {
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
HttpServletRequest rq = (HttpServletRequest) request;
HttpServletResponse rs = (HttpServletResponse) response;
doPost(rq, rs);
}![图片说明](https://img-ask.csdn.net/upload/201705/10/1494411561_422889.png)
}