package tools;
import java.sql.*;
public class DbConnection {
final static String driverName=”com.mysql.jdbc.Driver”;
final static String url=”jdbc:mysql://localhost:3306/test”;
static String username= “root”;
static String password=”root”;
public static Connection getConnection(){
Connection conn=null;
try {
Class.forName(driverName);
conn=DriverManager.getConnection(url, username, password);
return conn;
} catch (Exception e) {
System.out.println(“fail”);
e.printStackTrace();
return conn;
}
}
这是连接数据库的java文件
package dao;
import java.sql.*;
import tools.DbConnection;
public class AdminDao {
// 用户验证
public boolean isValidatedUser(String username, String password) throws Exception{
boolean result=false;
// 1.获取数据库连接
Connection conn = DbConnection.getConnection();
// 2.编写SQL语句
String querySQL = “select * FROM admin WHERE username=? AND password=?”;
// 3.创建PreparedStatement对象
PreparedStatement pstmt = conn.prepareStatement(querySQL);
// 4.设置问号的参数
pstmt.setString(1,username);
pstmt.setString(2,password);
// 5.执行查询SQL语句
ResultSet rs = pstmt.executeQuery();
if (rs.next())
result=true;
pstmt.close();
conn.close();
return result;
}
}
<?xml version=”1.0″ encoding=”GB18030″ ?>
<%@page import=”dao.*”%>
<%@page import=”tools.*”%>
<%@page import=” java.sql.*”%>
<%@ page language=”java” contentType=”text/html; charset=GB18030″
pageEncoding=”GB18030″%>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=GB18030″ />
</head>
<body>
<%
request.setCharacterEncoding(“UTF-8”);
String username = null; //用户名
String password = null; //密码
String autoLogin = “”;
final int oneWeek = 3600 * 24; //两个星期时间
//判断cookie中能否存在用户名和密码
username = CookieTools.getCookieValue(“name”, request);
password = CookieTools.getCookieValue(“password”, request);
AdminDao dao = new AdminDao();
//假如cookie中不存在用户名和密码,则认为是从页面登录
if (username == null && password == null) {
username = request.getParameter(“username”);
password = request.getParameter(“password”);
autoLogin = request.getParameter(“autoLogin”);
System.out.println(username + “;” + password +”;” + autoLogin);
}
boolean i=true;
try {
i=dao.isValidatedUser(username, password); //错在这里 会抛出java.lang.NullPointerException的错误..
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
username=e.toString();
}
if (i) {
//判断能否为选择了自动登录
if (autoLogin!=null && autoLogin.equals(“1”)) {
//将账号密码写入cookie
CookieTools.writeCookie(“name”, username, response);
CookieTools.writeCookie(“password”, password, response);
}
//将用户名写入sesssion,进行页面跳转
session.setAttribute(“user”, username);
response.sendRedirect(“/ch05/admin/main.jsp”);
} else {
request.setAttribute(“info”, “用户名或密码错误,请重新输入”);
//目标路径
String url=”/auto_login.jsp”;
//创建分发器对象
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
//页面跳转
dispatcher.forward(request, response);
}
%>
</body>
</html>
为了做这个作业看了一下午都看不出来错在哪 求帮助。
import java.sql.*;
public class DbConnection {
final static String driverName=”com.mysql.jdbc.Driver”;
final static String url=”jdbc:mysql://localhost:3306/test”;
static String username= “root”;
static String password=”root”;
public static Connection getConnection(){
Connection conn=null;
try {
Class.forName(driverName);
conn=DriverManager.getConnection(url, username, password);
return conn;
} catch (Exception e) {
System.out.println(“fail”);
e.printStackTrace();
return conn;
}
}
这是连接数据库的java文件
package dao;
import java.sql.*;
import tools.DbConnection;
public class AdminDao {
// 用户验证
public boolean isValidatedUser(String username, String password) throws Exception{
boolean result=false;
// 1.获取数据库连接
Connection conn = DbConnection.getConnection();
// 2.编写SQL语句
String querySQL = “select * FROM admin WHERE username=? AND password=?”;
// 3.创建PreparedStatement对象
PreparedStatement pstmt = conn.prepareStatement(querySQL);
// 4.设置问号的参数
pstmt.setString(1,username);
pstmt.setString(2,password);
// 5.执行查询SQL语句
ResultSet rs = pstmt.executeQuery();
if (rs.next())
result=true;
pstmt.close();
conn.close();
return result;
}
}
<?xml version=”1.0″ encoding=”GB18030″ ?>
<%@page import=”dao.*”%>
<%@page import=”tools.*”%>
<%@page import=” java.sql.*”%>
<%@ page language=”java” contentType=”text/html; charset=GB18030″
pageEncoding=”GB18030″%>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=GB18030″ />
</head>
<body>
<%
request.setCharacterEncoding(“UTF-8”);
String username = null; //用户名
String password = null; //密码
String autoLogin = “”;
final int oneWeek = 3600 * 24; //两个星期时间
//判断cookie中能否存在用户名和密码
username = CookieTools.getCookieValue(“name”, request);
password = CookieTools.getCookieValue(“password”, request);
AdminDao dao = new AdminDao();
//假如cookie中不存在用户名和密码,则认为是从页面登录
if (username == null && password == null) {
username = request.getParameter(“username”);
password = request.getParameter(“password”);
autoLogin = request.getParameter(“autoLogin”);
System.out.println(username + “;” + password +”;” + autoLogin);
}
boolean i=true;
try {
i=dao.isValidatedUser(username, password); //错在这里 会抛出java.lang.NullPointerException的错误..
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
username=e.toString();
}
if (i) {
//判断能否为选择了自动登录
if (autoLogin!=null && autoLogin.equals(“1”)) {
//将账号密码写入cookie
CookieTools.writeCookie(“name”, username, response);
CookieTools.writeCookie(“password”, password, response);
}
//将用户名写入sesssion,进行页面跳转
session.setAttribute(“user”, username);
response.sendRedirect(“/ch05/admin/main.jsp”);
} else {
request.setAttribute(“info”, “用户名或密码错误,请重新输入”);
//目标路径
String url=”/auto_login.jsp”;
//创建分发器对象
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
//页面跳转
dispatcher.forward(request, response);
}
%>
</body>
</html>
为了做这个作业看了一下午都看不出来错在哪 求帮助。
解决方案
100
假如还不行,就加本人Q:305679849.本人远程给你调。还治不了它了