我是Java的新手,我正在尝试做这个练习;但我只是坚持这个新问题。我整天都在google搜索这个错误--Java.lang.NullPointerException - 我尝试解决它,但我找不到它;有人请?
**我有一个BankDB_Final.db文件;它有一个名为Bank_001的表 **我已经安装了sqlite连接器(连接正常)和 rs2xml.jar文件位于名为“lib”的同一文件夹中。 **登录:Abbas ..密码:root。
以下是方法代码: //启动应用程序。
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Login_jFrame extends JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
private JPanel contentPane;
private JPasswordField txt_password;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login_jFrame frame = new Login_jFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Login_jFrame() {
conn = javaconnect.ConnectrDB();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Username");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblNewLabel.setBounds(177, 93, 70, 14);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Password");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblNewLabel_1.setBounds(177, 139, 70, 14);
contentPane.add(lblNewLabel_1);
txt_password = new JPasswordField();
txt_password.setBounds(253, 133, 100, 20);
contentPane.add(txt_password);
TextField txt_username = new TextField();
txt_username.setBounds(253, 85, 100, 22);
contentPane.add(txt_username);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(new TitledBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Login", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), "Login:", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(64, 64, 64)), "Login:", TitledBorder.LEADING, TitledBorder.TOP, null, Color.DARK_GRAY));
panel.setBounds(160, 50, 219, 160);
contentPane.add(panel);
panel.setLayout(null);
JButton cmdLogin = new JButton("Login");
cmdLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String sql = "select * from bank_001 where AccountName=? and Password=?";
try{
pst = conn.prepareStatement(sql);
pst.setString(1, txt_username.getText());
pst.setString(2, txt_password.getText());
rs= pst.executeQuery();
if(rs.next()) {
Employee_info s = new Employee_info();
s.setVisible(true);
JOptionPane.showMessageDialog(null, "Username and Password is correct!");
}else {
JOptionPane.showMessageDialog(null, "Username or Password is Not correct!");
}
}catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
});
cmdLogin.setBounds(90, 126, 89, 23);
panel.add(cmdLogin);
JLabel icon = new JLabel("");
icon.setIcon(new ImageIcon("C:\\Users\\User\\Downloads\\DB SQLITE\\Project123\\img\\icon.jpg"));
icon.setBounds(40, 56, 90, 90);
contentPane.add(icon);
}
}
JavaConnect类:
import java.sql.*;
import javax.swing.*;
public class javaconnect {
Connection conn = null;
public static Connection ConnectrDB() {
// 1- Get a connection to Database
try {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:c:\\users\\user\\downloads\\db sqlite\\bankdb_final.db");
//Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\User\\Downloads\\DB SQLITE\\BankDB_Final.db");
JOptionPane.showMessageDialog(null, "Connection Established");
return conn;
} catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
和最后(我认为这是错误)Employee_info类
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import javax.swing.*;
import net.proteanit.sql.DbUtils;
public class Employee_info extends JFrame {
private JPanel contentPane;
private JTable table_employee;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Employee_info frame = new Employee_info();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
/**
* Create the frame.
*/
public Employee_info() {
conn = javaconnect.ConnectrDB();
Update_Table();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(21, 11, 389, 230);
contentPane.add(scrollPane);
table_employee = new JTable();
scrollPane.setViewportView(table_employee);
table_employee.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
}
));
}
private void Update_Table() {
try {
String sql = "select * from Bank_001";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
table_employee.setModel(DbUtils.resultSetToTableModel(rs));
conn.close();
}catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
}
屏幕: