dimanche 17 avril 2016

How can you get data from SQLite Manager into JTextField?

I'm making a Java program where it logs you into a GUI screen after the Login prompt and takes you into the student frame and login. It connects to the SQLite Manager database where it pulls data for that user that logged in into certain JTextFields. I connects to the database (I made sure it was connecting), but it won't display the data from the database.

Here's my Login Code:

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=?";
                //String query="select * from LoginTb as L1,LoginTb as L2 where L1.USERNAME=L2.USERNAME and L1.PASSWORD=L2.PASSWORD;  ";
                PreparedStatement pst = connection.prepareStatement(query);
                pst.setString(1,textField1.getText() );
                pst.setString(2,passwordField.getText() );

                ResultSet rs=pst.executeQuery();    // variable object r s  to keep track of result
                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(); // import student resource
                    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);
}}

Here's the frame for the student:

public class student extends JFrame {

private JPanel contentPane;
private JPanel contentPane1;
private JTextField textField;
private JTextField textField_3;
private JTextField textField_2;
Connection connection=null;


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


protected void Info() {
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);

JLabel lblNewLabel_1 = new JLabel("Grade Level");
lblNewLabel_1.setFont(new Font("Times New Roman", Font.BOLD, 14));
lblNewLabel_1.setBounds(242, 11, 76, 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_3 = new JLabel("Current Book");
lblNewLabel_3.setFont(new Font("Times New Roman", Font.BOLD, 14));
lblNewLabel_3.setBounds(459, 11, 100, 14);
contentPane.add(lblNewLabel_3);

try{

    Class.forName("org.sqlite.JDBC");
    String path=student.class.getResource("Booklander.sqlite").getPath();
    Connection connection=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Gozie\\Desktop\\Database\\Booklander.sqlite");
    String SQL="select firstName,GradeLevel,ReadingLevel from Student,Login where firstName=? AND GradeLevel=? AND ReadingLevel=?";
    Statement statement=connection.createStatement();
    ResultSet rs=statement.executeQuery(SQL);

    while(rs.next())
    {
    String FN= rs.getString("firstName");
    int GL = Integer.parseInt("GradeLevel");
    int RL= Integer.parseInt("ReadingLevel");   

    textField.setText(FN);
    System.out.println(""+FN);
    textField_3.setText(String.valueOf(GL));
    System.out.println(GL);
    textField_2.setText(String.valueOf(RL));



    }
    connection.close();
    statement.close();

}
catch(Exception e){System.out.println("Error");
    }


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

Also here's some pics from the database in case you need it. http://ift.tt/1MBLytG

http://ift.tt/1STMXII

Should I return the 3 values using methods instead? Or did use the wrong code?

Aucun commentaire:

Enregistrer un commentaire