Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:47:34+00:00 2026-06-14T19:47:34+00:00

Im trying to populate JTable with MySql data. Thing is when i run Login

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:47:35+00:00Added an answer on June 14, 2026 at 7:47 pm

    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()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to populate data in my fields depending on the value selected
I am trying to populate an array with the text value of a json
I'm trying to populate a listview from another class, but I'm geting this error:
I am trying to populate the dropdown value with the selected value. I referred
I am trying to populate a dropdownlist with data pulled from a .resx file.
I am trying to populate a grid using a DirectStore. No data appears in
Im trying to populate a JSF Datatable with data that I am retrieving from
I am trying to populate my listbox and can only populate it with System.Data.Datarow
I am trying to populate a collection with data from a JSON file. I'm
I am trying to populate a sql variable with a value. It works when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.