我正在尝试使用SQLite db构建Web应用程序。我已经有此应用程序的桌面版本,并且db可以正常工作,但是现在我也想制作网络版本,但是由于某些原因,尽管代码几乎相同,但由于某些原因我无法连接到数据库。
Sqlite连接器:
package DAO;
import java.sql.*;
public class SQLite {
Connection conn = null;
public static Connection dbConnector() {
try {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:podaci/baza_new");
System.out.println("db connected");
return conn;
}catch(Exception e){
//JOptionPane.showMessageDialog(null, e);
System.out.println("db NOT connected");
return null;
}
}
}
首先,您需要登录应用程序,然后在代码中断时登录
servlet部分:
package obrada;
import java.io.IOException;
import java.util.Map;
import modeli.Osoba;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DAO.LoginDAO;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String userName = request.getParameter("username");
String passWord = request.getParameter("password");
String type = request.getParameter("tip");
ServletContext context = getServletContext();
if (LoginDAO.validate(userName, passWord, type)) {
System.out.println("asd");
}else {
RequestDispatcher rd=request.getRequestDispatcher("Login.html");
rd.include(request,response);
System.out.println("login else");
}
try {
}catch(Exception e) {e.printStackTrace();}
}
}
和道部分:
package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class LoginDAO {
static Connection con = null;
public static boolean validate(String username, String password) {
boolean status = false;
try {
con = SQLite.dbConnector();
System.out.println("1");
String query = "select * from kupci where korisnicko=? and lozinka=?";
PreparedStatement pst = con.prepareStatement(query);
//pst.setString(1, type);
pst.setString(1, username);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
status= rs.next();
con.close();
}catch(Exception e) {System.out.print(e);}
return status;
}
并且控制台给了我
bd NIJE povezana
1
java.lang.NullPointerExceptionlogin else
我尝试删除并添加sqlite jar,但这没用