我写是一个登录。输入用户名密码点登录,然后就直接进入debug了。
而debug指向的都是DB的getStmt()的 Statement stmt = null; 
而把debug一路下一步,居然也能顺利登录(用户名密码正确的情况下),这到底是怎么回事啊。
问题到底出在哪一直弄不明白,请各位指点指点我吧~~~把我困死了~~~
我遇到的问题关联到三个文件,我一一把这三个文件列出来。DB.java   //链接数据库的文件,我用的数据库是mysqlpackage com.shopping.util;//封装数据库的操作
import java.sql.*;public class DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/shopping?user=root&password=123456");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}

return conn;
}

public static void closeConn(Connection conn) { // 关闭流
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
} public static Statement getStmt(Connection conn) {
Statement stmt = null; 
try {
if(conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt; } public static PreparedStatement getPStmt(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt; } public static void closeStmt(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
} public static ResultSet getRs(Statement stmt, String sql) { // 返回查询结果
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs; }

public static ResultSet getRs(Connection conn, String sql) { // 重载getRs()    返回查询结果
ResultSet rs = null;
try {
rs = conn.createStatement().executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs; } public static void closeRs(ResultSet rs) // 关闭流 流一般是最后打开的最先关,最先打开的最后关!
{
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}User.java   //与用户信息操作相关的类
package com.shopping.user;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;import com.shopping.util.DB;public class User {
private int id;
private String username;
private String password;
private String phone;
private String addr;
private Date rdate; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public String getAddr() {
return addr;
} public void setAddr(String addr) {
this.addr = addr;
} public Date getRdate() {
return rdate;
} public void setRdate(Date rdate) {
this.rdate = rdate;
} public User() { }
public static User validate(String username, String password)
throws UserNotFoundException, PasswordNotCorrectException {
// 验证用户名密码是否正确 Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
User u = null; try {
conn = DB.getConn();
                        stmt = DB.getStmt(conn);
String sql = "select * from user where username='" + username
+ "' and password='" + password + "'";
System.out.println(sql);
rs = stmt.executeQuery(sql); // 查询是否有此用户
if (!rs.next()) {
throw new UserNotFoundException();
} else if (!rs.getString("password").equals(password)) {
throw new PasswordNotCorrectException();
} else { // 存储用户的信息到U里
u = new User();
u.setId(rs.getInt("id"));
u.setUsername(rs.getString("username"));
u.setPassword(rs.getString("password"));
u.setPhone(rs.getString("phone"));
u.setAddr(rs.getString("addr"));
u.setRdate(rs.getTimestamp("rdate"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.closeRs(rs);
DB.closeStmt(stmt);
DB.closeConn(conn);
} return u; }}
login.jsp<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="com.shopping.user.*"%>
<%
String action = request.getParameter("action");
if(action != null&&action.equals("login")){
  String username = request.getParameter("username");
  String password = request.getParameter("password");
  User u = null;
  try{
     u = User.validate(username,password);
  }catch(UserNotFoundException e){
      out.println("找不到用户");
         return;
  }catch(PasswordNotCorrectException e1){
      out.println("密码不正确!");
         return;
  }
  session.setAttribute("user",u);
  response.sendRedirect("selfservice.jsp");
  }
 %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>管理员登录界面</title>
</head>
<body>
<form action="login.jsp" method="post">
<table border=1>
<tr>&nbsp;imp用户登录</tr>
<tr>
<td>
用户名
</td>
<td>
<input type="text" name="username" size=16>
<input type="hidden" name="action" value="login">
</td>  
</tr>

<tr>
<td>
密 码
</td>
<td>
<input type="password" name="password" size=16>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit1" value="登录">
</td>
<td>
<input type="reset" name="submit2" value="重置">
</td>
</tr>
</table> </form>
</body>
</html>