0

我试图用 MySql 数据填充 JTable。事情是当我运行登录类时,我收到消息“java.SQLException:没有为参数 2 指定值”,当我运行 JTable 类时,我不断收到 java.lang.NullPointerException 错误。我有三个类: 1-连接到数据库:

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

我试图找出问题所在,但找不到解决方案。连接有效。非常感谢您在修复此错误方面的帮助。

4

3 回答 3

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()));
于 2012-11-23T01:34:28.813 回答
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));
于 2012-11-23T03:10:52.527 回答
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);
          }
于 2013-08-27T09:13:47.010 回答