【问题标题】:how to display all the data from the database to jtable?如何将数据库中的所有数据显示到jtable?
【发布时间】:2011-12-31 05:30:01
【问题描述】:

我们在数据库中有一个名为 data 的表,我们希望使用 NetBeans IDE 检索并显示 JTable 组件中的所有数据。数据库中的每一行都应该显示在 jtable 中。

【问题讨论】:

    标签: java swing netbeans jdbc jtable


    【解决方案1】:

    在没有任何关于此细节的信息的情况下,我建议您从JDBC tutorial 开始,了解如何从数据库中提取数据。然后JTable tutorial 将帮助您显示此内容。

    【讨论】:

      【解决方案2】:

      另外,请记住,MVC 是游戏的名称。扩展 AbstractTableModel 以创建将数据提供给 JTable 的模型。

      您将需要实现以下方法:

      public int getRowCount()
      public int getColumnCount() and 
      public Object getValueAt(int rowIndex, int columnIndex)
      

      最后将您的模型设置为 JTable:

          myJTable.setModel(new MyModel());
      

      类似这样的:

      public class MyModel extends AbstractTableModel {
      
          private Connection con;
          private Statement stmt;
          private ResultSet rs;
      
          public MyModel () {
      
              try {
                  Class.forName("com.mysql.jdbc.Driver");
                  try {
                      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename?useUnicode=true&characterEncoding=UTF-8",
                              "root", "password");
                      stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
                      rs = stmt.executeQuery("select * from tablename");
      
                  } catch (SQLException e) {
                      String msg = "MyModel(): Error Connecting to Database:\n"
                              + e.getMessage();
                      System.out.println(msg);
                  }
              } catch (ClassNotFoundException e) {
                  String msg = "The com.mysql.jdbc.Driver is missing\n"
                          + "install and rerun the application";
                  System.out.println(msg);
                  System.exit(1);
              } finally {
      
              }
          }
      
          public int getRowCount() {
      // count your rows and return 
              return count;
          }
      
          public int getColumnCount() {
      // how many cols will you need goes here
              return 3;
          }
      
          public Object getValueAt(int rowIndex, int columnIndex) {
              try {
                  rs.absolute(rowIndex + 1);
                  switch (columnIndex) {
                      case 0: 
                          return rs.getInt(1);
                      case 1:
                          return rs.getString(2);
                      case 2:
                          return rs.getString(6);
                      default:
                          return null;
                  }
              } catch (SQLException ex) {
                  System.out.println(ex.getMessage());
              }
              return null;
          }
      }
      

      【讨论】:

        【解决方案3】:

        您可以导入 DbUtils 并使用它将您的数据库导入 JTable,如下所示:

        Class.forName("com.mysql.jdbc.Driver");
        Connection connectionToDB = (Connection)DriverManager.getConnection(hostname, user,pw);
        PreparedStatement preparedStatement = connectionToDB.prepareStatement("SELECT * FROM Table;");
        ResultSet resultSetForFullTable = preparedStatement.executeQuery();
        dbTable.setModel(DbUtils.resultSetToTableModel(resultSetForFullTable));
        DbUtils 的导入语句是
        import net.proteanit.sql.DbUtils;

        【讨论】:

          猜你喜欢
          • 2015-07-20
          • 1970-01-01
          • 1970-01-01
          • 2011-09-16
          • 2011-07-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多