关于servlet中类的调用
小弟是初学者,写了一个登陆程序,不知道怎么回事,在servlet里面直接调用自己写的一个类(UserBean),但是调用UserBean中的方法之后程序就不能继续运行了,在Eclipse中编写,没有报错。当我把servlet中所有有关UserBean的内容注释掉后能运行。请大家帮我看看我代码有没有写错,谢谢啦!从表单接收userId和password
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
try{
PrintWriter out=response.getWriter();
String userId=request.getParameter( "userId ");
String password=request.getParameter( "password ");
UserBean userBean=new UserBean();
if(userBean.login(userId,password))
out.print( "success ");
else
out.print( "failure ");
}catch(Exception e){System.out.print(e);}
}
//以下为UserBean
class UserBean {
private Connection con;
public UserBean(){ //构造方法
this.con=DataBaseConnection.getConnection();//连接字符串已经封装在DataBaseConnection类中
}
public boolean login(String userId,String password)throws Exception{//从数据库查询,用户名和密码是否匹配
Statement st=con.createStatement();
ResultSet rs=st.executeQuery( "select * from user_Info where userId= ' "+userId+ " 'and password= ' "+password+ " ' ");
if(rs.next())
return true;
else
return false;
}
}
}
//以下为DataBaseConnection类用于封装连接字符串
import java.sql.*;
public class DataBaseConnection {
public static Connection getConnection(){
Connection con=null;
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
con=DriverManager.getConnection "Jdbc:Odbc:ebusiness ", "sa ", " ");
}catch(Exception e){}
return con;
}
}
------解决方案--------------------
//以下为DataBaseConnection类用于封装连接字符串
package login;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.*;
public class DataBaseConnection {
public DataBaseConnection(){}
public static Connection getConnection(){
Connection con=null;
try {
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
} catch (ClassNotFoundException ex) {
System.out.println( "驱动加载失败! ");
}
try {
con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://127.0.0.1:7788;databaseName=ebusiness ",
"sa ", "admin ");
} catch (SQLException ex1) {
System.out.println( "数据库连接失败! ");
}
return con;
}
}
package login;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class TestServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK ";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String userId=request.getParameter( "userId ");
String password=request.getParameter( "password ");
useBean u=new useBean();
if(u.login(userId,password))
out.print( "success ");
else
out.print( "failure ");
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
package login;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.*;
public class useBean {
public useBean(){}
public boolean login(String userId,String password){
Connection con=null;
Statement st=null;
ResultSet rs=null;
boolean a=false;
con = DataBaseConnection.getConnection();
try {
st = con.createStatement();
rs = st.executeQuery( "select * from user_Info where userId= ' " +
userId + " ' and password= ' " + password + " ' ");
if(rs.next())
a=true;
else
a=false;
} catch (SQLException ex) {
ex.printStackTrace();
}
return a;
}
}