Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
H guys,

I am trying to show the "name" value which is stored in the database, in the txtfield. But when I execute the program and click on the button nothing happens. Can me someone tell why??


btnStudent.addActionListener((ActionListener) new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {

            String newquery = "SELECT * FROM test";
            ps= (PreparedStatement) con.prepareStatement(newquery);
            rs = ps.executeQuery();

            if(rs.next()){
                //JOptionPane.showMessageDialog(null, rs.getString("name"));
                txtfldName= rs.getString("name");
                }


Thx all in advance.

What I have tried:

program the code and tried a lot to get the string in the txtfldName but nothing worked.
Posted
Updated 24-May-18 5:29am
Comments
Richard MacCutchan 23-May-18 10:29am    
Maybe your SQL statement did not return any data. Add some logging/debug code, or use your debugger to see exactly what is happening.
wseng 24-May-18 4:57am    
What System.out.println(txtfldName) return ?
Member 13814158 24-May-18 8:16am    
@wseg , just null.
wseng 24-May-18 11:20am    
why you need to cast conn to PreparedStatement ?

1 solution

Follow this

String newquery = "SELECT * FROM test";
DatabaseConnection db = new DatabaseConnection();
Connection conn = db.getConnection();
Statement st = conn.createStatement(); // create the java statement
ResultSet rs = st.executeQuery(query); // execute the query, and get resultset

 while (rs.next()){   // iterate through the java resultset
       //JOptionPane.showMessageDialog(null, rs.getString("name"));
        txtfldName= rs.getString("name");
    }

 st.close();
 rs.close();
 conn.close();
 
Share this answer
 
Comments
Member 13814158 26-May-18 6:13am    
@wseng I followed your post but nothing happens. I get these message when I start it with the debugger: Source not found Event Dispatch Thread.. I really don't know what that means. I am really confused.
Member 13814158 26-May-18 6:28am    
and I get an type dismatch for "txtfldName and rs.getString("Name")
wseng 27-May-18 12:21pm    
we need to see more code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900