0

Im trying to populate JTable with MySql data. Thing is when i run Login class, i receive message "java.SQLException: No value specified for parameter 2" and when i run JTable class I keep receiving java.lang.NullPointerException error. I have three classes : 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;
}
}

The second class is login frame:

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

and the last one is 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);
}
    }
}

I am trying to figure out what is the problem but cannot find the solution. Connection works. I would really appreciate your help in fixing this error.

3 Answers 3

5

The second parameter in your PreparedStatment is not set. You should replace

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

with

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

Edit: Note that JPassword.getText() is deprecated meaning that you should no longer use it. getPassword() is provided in its place:

pst.setString(2, new String(txt_Password.getPassword()));
Sign up to request clarification or add additional context in comments.

Comments

0

you can add a check to the code

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

And Also in your JTable1 class

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

1 Comment

As the user is telling about null pointer exception and the answer to other part
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);
          }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.