There are 3 JTextfields that are supposed to display info in each one after you click List Info and brings you to a new GUI and you automatically see the info in the JTextFields, but it's not displaying anything. Also I'm using Eclipse and SQLite Manager if that helps. Iogin, but it just won't display the info. There are 3 projects linked together and two tables: Student(Name, GradeLevel, ReadingLevel) andLogin(Username, Password) The student is suppose to login, click the button and see his info in the 2nd GUI.
My connection:

import java.sql.*;
import javax.swing.*;


public class SqliteConnection {
    Connection conn=null;
    public static Connection dbConnector()
    {
        try{
            Class.forName("org.sqlite.JDBC");
            String path=SqliteConnection.class.getResource("Books.sqlite").getPath();
            Connection conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Butch\\Desktop\\Database\\Books.sqlite");

            JOptionPane.showMessageDialog(null, "Connection Succesful...");
            return conn;

        } catch ( Exception e)
        {

            JOptionPane.showMessageDialog(null,e);
            return null ;

        }

    }

My Login

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import Resources.student;

public class LoginC {

    private JFrame frame;       //Frame name frame

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

    Connection connection=null;
    private JTextField textField1;
    private JPasswordField passwordField;
    /**
     * Create the application.
     */

    public LoginC() {
        initialize();
        connection=SqliteConnection.dbConnector();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblUserName = new JLabel("User Name");
        lblUserName.setFont(new Font("Times New Roman", Font.BOLD, 15));
        lblUserName.setBounds(134, 82, 84, 23);
        frame.getContentPane().add(lblUserName);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setFont(new Font("Times New Roman", Font.BOLD, 15));
        lblPassword.setBounds(134, 125, 84, 14);
        frame.getContentPane().add(lblPassword);

        textField1 = new JTextField();
        textField1.setBounds(244, 82, 147, 23);
        frame.getContentPane().add(textField1);
        textField1.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(244, 121, 147, 23);
        frame.getContentPane().add(passwordField);

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try{
                    String query

                    = " select * from Login where USERNAME=? and PASSWORD=?";

                    PreparedStatement pst = connection.prepareStatement(query);
                    pst.setString(1,textField1.getText() );
                    pst.setString(2,passwordField.getText() );

                    ResultSet rs=pst.executeQuery();    
                    int count =0;
                    while (rs.next())
                    {
                        count=count+1;
                    }
                    if (count ==1)
                    {
                        JOptionPane.showMessageDialog(null, "USERNAME and PASSWORD is correct");
                        frame.dispose ();                       
                        student ts = new student();
                        ts.setVisible(true);

                    }
                    else  if (count>1)
                    {
                        JOptionPane.showMessageDialog(null, "Duplicated USERNAME and PASSWORD is invalid");
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null, "USERNAME and PASSWORD is not correct Try again...");
                    }

                    rs.close();         // close connectors 
                    pst.close();

                } catch (Exception e)

                {
                    JOptionPane.showMessageDialog(null, e);
                }


            }
        });
        btnLogin.setFont(new Font("Times New Roman", Font.BOLD, 17));
        btnLogin.setBounds(198, 168, 89, 23);
        frame.getContentPane().add(btnLogin);
    }
}

My Student Gui

package Resources;


import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class student extends JFrame {

    private JPanel contentPane;
    private JPanel contentPane1;
    private JTextField textField;
    private JTextField textField_3;
    private JTextField textField_2;


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

    /**
     * Create the frame.
     */
    public student() {
        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);

        JLabel lblstudent = new JLabel("Student");
        lblstudent.setFont(new Font("Times New Roman", Font.BOLD, 18));
        lblstudent.setBounds(142, 10, 150, 43);
        contentPane.add(lblstudent);

        JButton btnNewButton = new JButton("List Info");
        btnNewButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Info();
                setVisible(true);
            }
        });
        btnNewButton.setFont(new Font("Times New Roman", Font.BOLD, 14));
        btnNewButton.setBounds(10, 129, 147, 50);
        contentPane.add(btnNewButton);



protected void Info() {
    // TODO Auto-generated method stub
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 585, 387);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("Name");
    lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
    lblNewLabel.setBounds(97, 11, 46, 14);
    contentPane.add(lblNewLabel);

    textField = new JTextField();
    textField.setBounds(20, 30, 199, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    textField_3 = new JTextField();
    textField_3.setBounds(242, 30, 86, 20);
    contentPane.add(textField_3);
    textField_3.setColumns(10);

    textField_2 = new JTextField();
    textField_2.setBounds(338, 30, 86, 20);
    contentPane.add(textField_2);
    textField_2.setColumns(10);

    JTextField textField_4 = new JTextField();
    textField_4.setBounds(459, 30, 86, 20);
    contentPane.add(textField_4);
    textField_4.setColumns(10);

    JLabel lblNewLabel_1 = new JLabel("Grade");
    lblNewLabel_1.setFont(new Font("Times New Roman", Font.BOLD, 14));
    lblNewLabel_1.setBounds(259, 11, 46, 14);
    contentPane.add(lblNewLabel_1);

    JLabel lblNewLabel_2 = new JLabel("Reading Level");
    lblNewLabel_2.setFont(new Font("Times New Roman", Font.BOLD, 14));
    lblNewLabel_2.setBounds(338, 11, 93, 14);
    contentPane.add(lblNewLabel_2);



    JLabel lblNewLabel_4 = new JLabel("Read Books");
    lblNewLabel_4.setFont(new Font("Times New Roman", Font.BOLD, 14));
    lblNewLabel_4.setBounds(158, 61, 331, 14);
    contentPane.add(lblNewLabel_4);

    ****try{
        Class.forName("org.sqlite.JDBC");
        Connection connection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Butch\\Desktop\\Database\\Books.sqlite");
        PreparedStatement statement=connection.prepareStatement(("select LastName,GradeLevel,ReadingLevel from Student where LastName=? AND GradeLevel=? AND ReadingLevel=?"));

        statement.setString(1,textField.getText());
        String firstNo=textField_3.getText().trim();
         String seconNo=textField_2.getText().trim();
         statement.setInt(2,Integer.parseInt((firstNo.length()>0)?firstNo:"0"));
         statement.setInt(3,Integer.parseInt((seconNo.length()>0)?seconNo:"0"));
        ResultSet resultset=statement.executeQuery();
        while(resultset.next())
        {

        textField.setText(resultset.getString(1));
        textField_3.setText(String.valueOf(resultset.getInt(2)));
        textField_2.setText(String.valueOf(resultset.getInt(3)));

        }
        connection.close();
        resultset.close();
    }
    catch(Exception e){JOptionPane.showMessageDialog(null, e);
        }****

}

private static void addPopup(Component component, final JPopupMenu popup) {
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }
        private void showMenu(MouseEvent e) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    });
}
}

The code in bold is what I used to try to return the values from the database into JTextfield

Recommended Answers

All 2 Replies

Line 144, are you sure the result set is not empty?

That may have been the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.