I added sqlite-jdbc-3.8.11.1.jar to my class path in the project. It worked in my class having a static main function like this below. This method is in Utilities class.
public static boolean getAuthResultV2(String userName, String pswd){
boolean authRes = false;
if(userName.length() > 0){
CustAuth cust = new CustAuth();
cust = cust.getCustAuthByUsrName(userName);
if(cust != null){
String usrPass = cust.getPassword();
if(usrPass.equals(pswd)){
authRes = true;
}
}
}
return authRes;
}
public static void main(String[] args){
if (getAuthResultV2("userx", "9999")){
System.out.println("YES");
}
else{
System.out.println("NO");
}
In CustAuth class, I get info from DB like that
public CustAuth getCustAuthByUsrName(String name){
CustAuth tmpCustAuth = null;
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:admin.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM CUST_AUTH WHERE USER_NAME = '" + name + "';" );
while ( rs.next() ) {
tmpCustAuth = new CustAuth();
String pswd = rs.getString("PASSWORD");
int custId = rs.getInt("OWN_CUST_ID");
Date effT = rs.getDate("EFF_TIME");
Date expT = rs.getDate("EXP_TIME");
tmpCustAuth.userName = name;
tmpCustAuth.password = pswd;
tmpCustAuth.effTime = effT;
tmpCustAuth.expTime = expT;
System.out.println( "PASSWORD = " + pswd );
System.out.println();
}
rs.close();
stmt.close();
c.close();
return tmpCustAuth;
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
//System.exit(0);
return null;
}
}
This works fine when I run the application Utilities. After that I remove my main function in Utilities and then want to use Utilities class in my servlet as shown below.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("userName");
String pswd = request.getParameter("pswd");
//Utilities ut = new Utilities();
boolean authResult = Utilities.getAuthResultV2(userName, pswd);
if(authResult){
request.getSession().setAttribute("userName", userName);
request.getSession().setAttribute("pswd", pswd);
response.sendRedirect("loginSuccessPage.html");
}
else{
response.sendRedirect("login.html");
}
}
But when I run the application on local server I get java.lang.ClassNotFoundException: org.sqlite.JDBC error. It works fine in desktop application as I described above but it doesn't work in my web application. Could you please help me with it ? Thanks
Aucun commentaire:
Enregistrer un commentaire