java.lang.NullPointerException ..用于JDBC SQLite连接

时间:2018-10-03 15:12:55

标签: jdbc nullpointerexception

将JDBC用于本地sqlite数据库。每次我单击登录时,都会收到java.lang.NullPointerException。

我尝试过的事情:

1。验证数据库的路径。 (由于运行后会弹出连接成功窗口)

2。还要验证表名。

3。尝试移动数据库并更新路径。

4。还尝试了其他方法,例如(username ='“ + username.getText()”')

登录代码:

import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javax.swing.JOptionPane;


public class FXMLDocumentController implements Initializable {
Connection connection=null;
@FXML
private Label label;

@FXML
private TextField username;

@FXML
private PasswordField password;
@FXML
private Button login;
@FXML
private Button signup;

@FXML
private Button close;



public FXMLDocumentController()
{
     connection=SQLconnection.dbConnector();
}
public void close(ActionEvent event)
{
   System.exit(0);
}
public void login(ActionEvent event)
{
     try
    {


       String query="select * from user where username=? and password=?";
        PreparedStatement pst=connection.prepareStatement(query);
        String uname=username.getText();
        String pass=password.getText();
        pst.setString(1, uname);
        pst.setString(2, pass);   
        ResultSet rs=pst.executeQuery();
        int count=0;
        while(rs.next())
        {
            count++;
        }
        if(count==1)
        {
            System.out.print("Username password correct");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Incorrect Username and 
            password");
        }
        rs.close();
        pst.close();
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
    }  
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

}

连接:

import java.sql.*;
import javax.swing.*;
public class SQLconnection {

 Connection conn=null;
public static Connection dbConnector()
{
  try
  {
      Class.forName("org.sqlite.JDBC");
      Connection conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Pushkar\\Documents\\NetBeansProjects\\Traveler\\traveler.db");
      JOptionPane.showMessageDialog(null, "Connection Sucessfull");
      return conn;
  }catch(Exception e)
  {
      JOptionPane.showMessageDialog(null, e);
      return null;
  }
}
}

以下是屏幕截图: SS

0 个答案:

没有答案