从mysql填充JTable的java.lang.NullPointerException错误

时间:2012-11-23 01:33:23

标签: java mysql swing jtable runtime-error

我试图用MySql数据填充JTable。事情是当我运行Login类时,我收到消息“java.SQLException:没有为参数2指定值”,当我运行JTable类时,我一直收到java.lang.NullPointerException错误。我有三个类:1-Connection to DB:

import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JOptionPane;



public class javaconnect {
Connection conn = null;

public static Connection ConnecrDB() {

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/mydb", 
                        "root", "root");
        JOptionPane.showMessageDialog(null, "Connected");
        return con;

    } catch (Exception e) {
        System.out.println("ERROR: " + e.getMessage());
    }
    return null;
}
}

第二个类是登录框:

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.*;

import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class JFrameLogIn extends JFrame {
Connection conn =null;
ResultSet rs = null;
PreparedStatement pst = null;

private JPanel contentPane;
private JPasswordField txt_Password;
private JTextField txt_Username;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                JFrameLogIn frame = new JFrameLogIn();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public JFrameLogIn() {

    conn=javaconnect.ConnecrDB();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 375, 214);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblUsername = new JLabel("Username:");
    lblUsername.setFont(new Font("Tahoma", Font.BOLD, 13));
    lblUsername.setBounds(126, 45, 80, 14);
    contentPane.add(lblUsername);

    JLabel lblPassword = new JLabel("Password:");
    lblPassword.setFont(new Font("Tahoma", Font.BOLD, 13));
    lblPassword.setBounds(126, 82, 69, 14);
    contentPane.add(lblPassword);

    txt_Password = new JPasswordField();
    txt_Password.setBounds(218, 82, 109, 20);
    contentPane.add(txt_Password);

    txt_Username = new JTextField();
    txt_Username.setBounds(216, 43, 111, 20);
    contentPane.add(txt_Username);
    txt_Username.setColumns(10);

    JButton cmd_Login = new JButton("Login");
    cmd_Login.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String sql = "Select * from clients where username=? and password=?";
            try{
                pst = conn.prepareStatement(sql);
                pst.setString(1, txt_Username.getText());
                pst.setString(1, txt_Password.getText());

                rs = pst.executeQuery();
                if(rs.next()){
                    JOptionPane.showMessageDialog(null, "Username and     Password are correct");

                }
                else {
                    JOptionPane.showMessageDialog(null, "Username and Password are not correct");
                }
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null, e);
            }
        }
    });
    cmd_Login.setBounds(218, 125, 80, 23);
    contentPane.add(cmd_Login);
}
}

,最后一个是JTable

import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.sql.*;
import net.proteanit.sql.DbUtils;

public class JTable1 extends JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;

private JPanel contentPane;
private JTable table_Admins;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                JTable1 frame = new JTable1();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public JTable1() {
    conn = javaconnect.ConnecrDB();
    UpdateTable ();


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

    table_Admins = new JTable();
    table_Admins.setBounds(30, 28, 378, 54);
    contentPane.add(table_Admins);
}

private void UpdateTable () {
    try { 
        String sql = "SELECT *FROM menu";
    pst = conn.prepareStatement(sql);
    rs = pst.executeQuery();
    table_Admins.setModel(DbUtils.resultSetToTableModel(rs));
}
    catch (Exception e){
    JOptionPane.showMessageDialog(null, e);
}
    }
}

我试图弄清楚问题是什么,但找不到解决方案。连接工作。我非常感谢您帮助修复此错误。

3 个答案:

答案 0 :(得分:5)

未设置PreparedStatment中的第二个参数。你应该替换

pst.setString(1, txt_Password.getText());

pst.setString(2, txt_Password.getText());

修改:请注意JPassword.getText() 已弃用,这意味着您不应再使用它。提供getPassword()代替:

pst.setString(2, new String(txt_Password.getPassword()));

答案 1 :(得分:0)

您可以在代码中添加支票

           rs = pst.executeQuery();
           if (rs!=null){
               if(rs.next()){
                  JOptionPane.showMessageDialog(null, "Username and     Password are correct");

              }
              else {
                JOptionPane.showMessageDialog(null, "Username and Password are not correct");
              }
          }

同样在你的JTable1类

 if (rs!=null)
 table_Admins.setModel(DbUtils.resultSetToTableModel(rs));

答案 2 :(得分:0)

   private void UpdateTable () {
   conn = javaconnect.ConnecrDB();// connection and query should be in same place
   try { 
       String sql = "SELECT *FROM menu";
       pst = conn.prepareStatement(sql);
       rs = pst.executeQuery();
        table_Admins.setModel(DbUtils.resultSetToTableModel(rs));
       }
    catch (Exception e){
     JOptionPane.showMessageDialog(null, e);
          }