Unable to display data from database on a jtable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PreethiGowri
    New Member
    • Oct 2012
    • 126

    Unable to display data from database on a jtable

    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

    Code:
     Connection con = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                String conURL = "jdbc:mysql://localhost/dbname";
                con = DriverManager.getConnection(conURL, "user", "pswrd");
                Statement st = con.createStatement();
                String query1 = "select name, date, login, logout from " + jTextField1.getText() + " where id = '" + jTextField2.getText() + "'";
                ResultSet rs = st.executeQuery(query1);
                while (rs.next()) {
    jTable1.setModel(DbUtils.resultSetToTableModel(rs));
                }
                
                System.out.println(query1);
            } catch (ClassNotFoundException | SQLException e) {
                 JOptionPane.showMessageDialog(null, e);
            }
    where jTextField1 gives table name and jTextField2 gives id value.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.)What happens when you run the code?
    2.)Don't create SQL by adding string together. Use a PreparedStateme nt to supply the values as parameters.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      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.

      Comment

      Working...