lundi 1 octobre 2018

Java web : Sqlite NullPoinerException error

I'm trying to build web application that with SQLite db. I already have desktop version of this app and db works just fine, but now i want to make web version also, but for some reason i cant connect to database although code is pretty much the same.

Sqlite connector:

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;
    }

}

}

first you need to log in in application and that when code breaks

servlet part:

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();}
}

}

and dao part:

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;

}

and console gives me:

bd NIJE povezana
1
java.lang.NullPointerExceptionlogin else

i tried removing and adding sqlite jar but it doesnt work




Aucun commentaire:

Enregistrer un commentaire