2012-11-23 116 views
0

我試圖用MySql數據填充JTable。事情是當我運行登錄類,我收到消息「java.SQLException:沒有爲參數2指定的值」,當我運行JTable類時,我不斷收到java.lang.NullPointerException錯誤。我有三類:1連接到數據庫:java.lang.NullPointerException錯誤從MySQL填充JTable

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

我想圖出了什麼問題,但找不到解決方案。連接工作。我真的很感謝你幫助解決這個錯誤。

回答

5

您的PreparedStatment中的第二個參數未設置。你應該

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

編輯取代

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

注意JPassword.getText()棄用這意味着你不應該再使用它。 getPassword()在其位置提供:

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

由於Reimeus一部分是解決 –

+0

還有其他部分嗎? – Reimeus

0

你可以在JTable1類添加一個檢查代碼

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

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

您可能需要解釋這將有助於爲什麼... – Craig

+0

由於用戶正在講述空指針異常和其他部分的答案 –

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