By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,805 Members | 1,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,805 IT Pros & Developers. It's quick & easy.

Unable to display data from database on a jtable

100+
P: 126
I dont know what is wrong with my code but i'm not able to display the data extracted from database on a jtable:(
here is my code

Expand|Select|Wrap|Line Numbers
  1.  Connection con = null;
  2.         try {
  3.             Class.forName("com.mysql.jdbc.Driver");
  4.             String conURL = "jdbc:mysql://localhost/dbname";
  5.             con = DriverManager.getConnection(conURL, "user", "pswrd");
  6.             Statement st = con.createStatement();
  7.             String query1 = "select name, date, login, logout from " + jTextField1.getText() + " where id = '" + jTextField2.getText() + "'";
  8.             ResultSet rs = st.executeQuery(query1);
  9.             while (rs.next()) {
  10. jTable1.setModel(DbUtils.resultSetToTableModel(rs));
  11.             }
  12.  
  13.             System.out.println(query1);
  14.         } catch (ClassNotFoundException | SQLException e) {
  15.              JOptionPane.showMessageDialog(null, e);
  16.         }
where jTextField1 gives table name and jTextField2 gives id value.
May 8 '13 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
1.)What happens when you run the code?
2.)Don't create SQL by adding string together. Use a PreparedStatement to supply the values as parameters.
May 9 '13 #2

Rabbit
Expert Mod 10K+
P: 12,422
I don't believe they will be able to use a prepared statement since the table name is one of the variables.

Having a variable table name is odd though. It makes me think your data is structured incorrectly and is not normalized.
May 9 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.