5555急死我了,大虾帮忙解决下
我是新手,才学习JAVA WEB 今天做练习遇到了怪事,以下是全部代码:
连接数据库的类
//////////////////////////////////////////////////////////////
package Shop.dll.JavaClass;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class GetConn {
/**
*连接数据库
*/
public static Connection GetCon(){
Connection con = null;
try {
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
con = DriverManager.getConnection( "jdbc:odbc:ShopDB ");
} catch (Exception ex) {
try {
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
String url= "jdbc:microsoft:sqlserver://localhost:1433;databaseName=Shop ";
con=DriverManager.getConnection(url, "sa ", " ");
} catch (Exception e) {
System.out.println( "连接数据库失败! ");
return null;
}
}
return con;
}
/**
* 关闭连接
*/
public static void CloseCon(Connection con,PreparedStatement pre){
try {
pre.close();
} catch (Exception ex) {}
try {
con.close();
} catch (Exception ex) {}
}
public static void CloseCon(Connection con,PreparedStatement pre,ResultSet re){
try {
re.close();
} catch (Exception ex) {}
try {
pre.close();
} catch (Exception ex) {}
try {
con.close();
} catch (Exception ex) {}
}
}
查看用户名密码是否正确的类.
////////////////////////////////////////////////////////
package Shop.dll.JavaClass;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class LoginInfo {
/**
* 查看是否是授权用户.
*/
public boolean SelectUserNameAndPwd(String User, String Pwd) {
boolean flage = false;
Connection con = null;
PreparedStatement pre = null;
ResultSet re = null;
try {
con = GetConn.GetCon();
if (con != null) {
String SQL =
"SELECT * FROM LOGININFO WHERE USERNAME = ? AND USERPWD = ? ";
pre = con.prepareStatement(SQL);
pre.setString(1, User);
pre.setString(2, Pwd);
re = pre.executeQuery();
if (re.next()) {
flage = true;
}
}
} catch (Exception ex) {
return flage;
} finally {
GetConn.CloseCon(con, pre, re);
}
return flage;
}
}
调用验证方法的servlet
////////////////////////////////////////////
package Shop.dll.Servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import Shop.dll.JavaClass.LoginInfo;
public class Loginservlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK ";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
//验证用户名密码是否正确的类.
LoginInfo login = new LoginInfo();
request.setCharacterEncoding( "GBK ");
String User = request.getParameter( "TxtUserName ");
String Pwd = request.getParameter( "LoginPwd ");
if(login.SelectUserNameAndPwd(User,Pwd)){
out.print( "成功 ");
}else{
out.print( "失败 ");
}
out.close();
}
//Clean up resources
public void destroy() {
}
}
后来我没在页面取值,直接把User和Pwd写成了数据库一样的数据也提示失败
我又另外建了一个测试的类,在里面用同样的方法,同样的参数调用SelectUserNameAndPwd方法,返回值却和在servlet里面调这个方法返回值不一样.
哪位大虾行行好,帮我看看啊!小弟我感激不尽.
[解决办法]
很久没有直接用jdbc访问数据库了,让hibernate和spring给我搞懒了