【问题标题】:Most simple code to populate JTable from ResultSet从 ResultSet 填充 JTable 的最简单代码
【发布时间】:2012-05-24 03:04:53
【问题描述】:

我用谷歌搜索了一整天,没有运气。按下 OK 按钮后,我调用 getnPrintAllData() 方法。所以代码是:

public class DatabaseSQLiteConnection {
    Connection conn = null;
    PreparedStatement statement = null;
    ResultSet res = null;

    public DatabaseSQLiteConnection(){
        try{
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:test.sqlite");
            statement = conn.prepareStatement("SELECT * from product_info;");
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }   

    public void getnPrintAllData(){
        String name, supplier, id;
        DefaultTableModel dtm = new DefaultTableModel();
        Window gui = new Window(); //My JPanel class        
        try{
            res = statement.executeQuery();
            testResultSet(res);
            ResultSetMetaData meta = res.getMetaData();
            int numberOfColumns = meta.getColumnCount();
            while (res.next())
            {
                Object [] rowData = new Object[numberOfColumns];
                for (int i = 0; i < rowData.length; ++i)
                {
                    rowData[i] = res.getObject(i+1);
                }
                dtm.addRow(rowData);
            }
            gui.jTable1.setModel(dtm);
            dtm.fireTableDataChanged();
            //////////////////////////

        }
        catch(Exception e){
            System.err.println(e);
            e.printStackTrace();
        }
        finally{
            try{
                res.close();
                statement.close();
                conn.close();
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    }    

    public void testResultSet(ResultSet res){
        try{
            while(res.next()){
                System.out.println("Product ID: "+ res.getInt("product_id"));
                System.out.println("Product name: "+ res.getString("product_name"));
                System.out.println("Supplier: "+ res.getString("supplier"));
            }        
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}

我的testResultSet() 方法工作正常。 现在,如何更改我的代码以使其正常工作,或者从 ResultSet 制作 DefaultTableModel 的最简单代码是什么? 提前致谢。

编辑:我收到java.lang.IllegalStateException: SQLite JDBC: inconsistent internal state 错误。

【问题讨论】:

  • 是的,从 ResultSet 填充 DefaultTableModel。
  • 不是推荐的方法(不使用 SwingWorker)但应该给你一个想法java2s.com/Code/Java/Swing-JFC/…
  • 从哪一行抛出异常?
  • @mprabhat - 看起来不错 (how-to-fill-data-in-a-jtable-with-database) 但您能否在此处编辑该代码并发表评论,以便我可以在我的项目中使用它?我实际上没有得到那个代码。

标签: java swing sqlite jtable defaulttablemodel


【解决方案1】:

这是我的方法:

String[] columnNames = {"id", "Nome", "Sobrenome","Email"};

List<Student> students = _repo.getAll();

Object[][] data = new Object[students.size()][4];

int index = 0;
for(Student s : students) {
    data[index][0] = s.getId();
    data[index][1] = s.getFirstName();
    data[index][2] = s.getLastName();
    data[index][3] = s.getEmail();
    index++;
}

DefaultTableModel model = new DefaultTableModel(data, columnNames);

table = new JTable(model);

【讨论】:

    【解决方案2】:

    考虑到所有因素,关于代码架构,使用模块化编程将工作得很好,并且代码更简单。写一个简单的

    getData() function @returns a 2D array of the data from the database.
    

    将此函数传递给 JTabel() 构造函数的构造函数,即

    JTabel myTable = new JTable(getData(),columnsArray);
    

    在这种情况下,第二个参数:columnsArray 是具有列名的一维数组

    String[] columns = {{"ID","DataOfBirth","Age","Grade","Marks","RegNumber"}};
    

    将 JTable 对象传递给 JScrollPane 然后你就完成了,在将 ScrollPane 添加到容器之后就完成了

    JScrollPane scrollPane = new JScrollPane(myTable);
    
    JFrame myFrame = new JFrame();
    myFrame.add(scrollPane);
    

    这是一个示例函数 getData(),它查询数据库以获取稍后传递给 JTable 的数据

    public String[][] getRecords() {
        try (Connection conect = DriverManager.getConnection("jdbc:mysql://localhost:3306/online_students_registration", "root", "")) {
            Statement stm = conect.createStatement();
            String SELECT_QUERY = "SELECT COUNT(*) FROM medicalrecords ;";
            ResultSet cursor = stm.executeQuery(SELECT_QUERY);
            while (cursor.next()) {
                rows = cursor.getInt("COUNT(*)");
                System.out.println("Table will have " + rows + " Rows");
            }
            System.err.println("Contacts row count is obtained!!");
            if (rows < 1) {
                JOptionPane.showMessageDialog(null, "There is NO DATA");
                //contactsRowsCount = 1;
                //System.out.println("Table rows succefully reset to  " + contactsRowsCount + " Rows");
                dataValues = new String[1][8];
                //dataValues[1][5] = "No Values";
    
                for (int i = 0; i < 1; i++) {
                    for (int j = 0; j < 8; j++) {
                        if (j == 0) {
                            dataValues[i][j] = "No Details Available";
                            System.out.println("" + dataValues);
                        } else {
                            dataValues[i][j] = "...";
                            System.out.println("Contacts" + dataValues[i][j]);
                        }
                    }
                }
    
                System.out.println("Return statement is being executed on 0 rows ");
                //return doctoredDataValues;
            } else if (rows > 0) {
    
                System.out.println("obtain contacts code is being run under  " + rows + " Rows");
                dataValues = new String[rows][8];    //declare array for contacts table data
                System.out.println("[ Line 1584 ]The dataValues object for the JTable succefully set");
    
                String SELECT_QUERY_CONTACT = "SELECT * FROM medicalrecords; ";
    
                //OBTAIN CONTACTS FROM DB WITH REGARD TO CONTACT CATEGORY SPECIFIED
                ResultSet contactsTableCursor = stm.executeQuery(SELECT_QUERY_CONTACT);
                //use iterator-algorithm to insert values into the JTable
                for (int i = 0; contactsTableCursor.next() && i < rows; i++) {
                    for (int j = 0; j < 8; j++) {
                        dataValues[i][j] = contactsTableCursor.getString(j + 1);
                        System.out.println("Contacts" + dataValues[i][j]);
                    }
                }
            }
            JOptionPane.showMessageDialog(null, "Medical Details Added Succefully!!");
        } catch (SQLException e) {
    
            JOptionPane.showMessageDialog(null, "Unable to Obtain contacts:Server is Offline(LINE 1568)" + e.getMessage());
        }
    
        return dataValues;
    
    }
    

    【讨论】:

      【解决方案3】:

      我确信这是从 ResultSet 填充 JTable 的最简单方法,无需任何外部库。 我在这个方法中包含了 cmets。

      public void resultSetToTableModel(ResultSet rs, JTable table) throws SQLException{
              //Create new table model
              DefaultTableModel tableModel = new DefaultTableModel();
      
              //Retrieve meta data from ResultSet
              ResultSetMetaData metaData = rs.getMetaData();
      
              //Get number of columns from meta data
              int columnCount = metaData.getColumnCount();
      
              //Get all column names from meta data and add columns to table model
              for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++){
                  tableModel.addColumn(metaData.getColumnLabel(columnIndex));
              }
      
              //Create array of Objects with size of column count from meta data
              Object[] row = new Object[columnCount];
      
              //Scroll through result set
              while (rs.next()){
                  //Get object from column with specific index of result set to array of objects
                  for (int i = 0; i < columnCount; i++){
                      row[i] = rs.getObject(i+1);
                  }
                  //Now add row to table model with that array of objects as an argument
                  tableModel.addRow(row);
              }
      
              //Now add that table model to your table and you are done :D
              table.setModel(tableModel);
          }
      

      【讨论】:

        【解决方案4】:

        您必须通过单击此链接rs2xml.jar 下载名为 rs2xml 的 Jar,然后将其添加到您的项目库中,然后导入

        import net.proteanit.sql.DbUtils;
        

        我只准备了一个方法作为示例

        private void Update_table(){
            try{
                // fetch a connection
                connection = your_connection_class_name_here.getInstance().getConnection();
                if (connection != null) {
                String sql="select name,description  from sell_mode";
                pst=connection.prepareStatement(sql);
                rs=pst.executeQuery();
                 your_table_name_for_populating_your_Data.setModel(DbUtils.resultSetToTableModel(rs));
                }
            }
            catch(IOException | SQLException | PropertyVetoException e){
            JOptionPane.showMessageDialog(null,e);
            }
            finally {
                  if (rs!= null) try { rs.close(); } catch (SQLException e) {}
                  if (pst != null) try { pst.close(); } catch (SQLException e) {}
                  if (connection != null) try { connection.close(); } catch (SQLException e) {}
              }
        }
        

        希望这会有所帮助

        【讨论】:

          【解决方案5】:

          我认为这是使用 ResultSet 填充/建模表的最简单方法。 下载 rs2xml.jar Get rs2xml.jar 并将其包含在您的库中..

          import net.proteanit.sql.DbUtils;
          
              try
              {
              CreateConnection();
              PreparedStatement st =conn.prepareStatement("Select * from ABC;");
              ResultSet rs = st.executeQuery();
              tblToBeFilled.setModel(DbUtils.resultSetToTableModel(rs));
              conn.close();
              }
              catch(Exception ex)
              {
              JOptionPane.showMessageDialog(null, ex.toString());
              }
          

          【讨论】:

            【解决方案6】:

            我认为从ResultSet 的实例构建模型的最简单方法如下。

            public static void main(String[] args) throws Exception {
                // The Connection is obtained
            
                ResultSet rs = stmt.executeQuery("select * from product_info");
            
                // It creates and displays the table
                JTable table = new JTable(buildTableModel(rs));
            
                // Closes the Connection
            
                JOptionPane.showMessageDialog(null, new JScrollPane(table));
            
            }
            

            方法buildTableModel

            public static DefaultTableModel buildTableModel(ResultSet rs)
                    throws SQLException {
            
                ResultSetMetaData metaData = rs.getMetaData();
            
                // names of columns
                Vector<String> columnNames = new Vector<String>();
                int columnCount = metaData.getColumnCount();
                for (int column = 1; column <= columnCount; column++) {
                    columnNames.add(metaData.getColumnName(column));
                }
            
                // data of the table
                Vector<Vector<Object>> data = new Vector<Vector<Object>>();
                while (rs.next()) {
                    Vector<Object> vector = new Vector<Object>();
                    for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                        vector.add(rs.getObject(columnIndex));
                    }
                    data.add(vector);
                }
            
                return new DefaultTableModel(data, columnNames);
            
            }
            

            更新

            你喜欢用javax.swing.SwingWorker吗?你喜欢使用try-with-resources 语句吗?

            public class GUI extends JFrame {
            
                public static void main(String[] args) {
                    EventQueue.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            new GUI().setVisible(true);
                        }
                    });
                }
            
                private final JButton button;
                private final JTable table;
                private final DefaultTableModel tableModel = new DefaultTableModel();
            
                public GUI() throws HeadlessException {
            
                    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            
                    table = new JTable(tableModel);
                    add(new JScrollPane(table), BorderLayout.CENTER);
            
                    button = new JButton("Load Data");
                    button.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            new SwingWorker<Void, Void>() {
                                @Override
                                protected Void doInBackground() throws Exception {
                                    loadData();
                                    return null;
                                }
                            }.execute();
                        }
                    });
                    add(button, BorderLayout.PAGE_START);
            
                    setSize(640, 480);
                }
            
                private void loadData() {
                    LOG.info("START loadData method");
            
                    button.setEnabled(false);
            
                    try (Connection conn = DriverManager.getConnection(url, usr, pwd);
                            Statement stmt = conn.createStatement()) {
            
                        ResultSet rs = stmt.executeQuery("select * from customer");
                        ResultSetMetaData metaData = rs.getMetaData();
            
                        // Names of columns
                        Vector<String> columnNames = new Vector<String>();
                        int columnCount = metaData.getColumnCount();
                        for (int i = 1; i <= columnCount; i++) {
                            columnNames.add(metaData.getColumnName(i));
                        }
            
                        // Data of the table
                        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
                        while (rs.next()) {
                            Vector<Object> vector = new Vector<Object>();
                            for (int i = 1; i <= columnCount; i++) {
                                vector.add(rs.getObject(i));
                            }
                            data.add(vector);
                        }
            
                        tableModel.setDataVector(data, columnNames);
                    } catch (Exception e) {
                        LOG.log(Level.SEVERE, "Exception in Load Data", e);
                    }
                    button.setEnabled(true);
            
                    LOG.info("END loadData method");
                }
            
            }
            

            【讨论】:

            • @ Paul Vargas - 嘿兄弟,它对我有用。如果我有一个以前设计的名为productInfoTable 的JTable 怎么办?我尝试使用productInfoTable.setModel(buildTableModel(res)),但它不起作用。有什么建议吗?
            • 我添加了代码,以便在您的应用程序已经启动时更新表格数据。
            • LOG.info 是做什么的?我运行它时出错。它是我的 netbeans 没有的库的一部分吗?
            • @Mariel 在 NetBeans 中,使用 Alt + Insert,然后使用 Logger...。 :)
            【解决方案7】:

            我认为这是使用 ResultSet 之类的方法填充表的最简单方法

            FillTable(MyTable, "select * Customers;");
            

            还有一个很简单的方法可以做成

            public void FillTable(JTable table, String Query)
            {
                try
                {
                    CreateConnection();
                    Statement stat = conn.createStatement();
                    ResultSet rs = stat.executeQuery(Query);
            
                    //To remove previously added rows
                    while(table.getRowCount() > 0) 
                    {
                        ((DefaultTableModel) table.getModel()).removeRow(0);
                    }
                    int columns = rs.getMetaData().getColumnCount();
                    while(rs.next())
                    {  
                        Object[] row = new Object[columns];
                        for (int i = 1; i <= columns; i++)
                        {  
                            row[i - 1] = rs.getObject(i);
                        }
                        ((DefaultTableModel) table.getModel()).insertRow(rs.getRow()-1,row);
                    }
            
                    rs.close();
                    stat.close();
                    conn.close();
                }
                catch(InstantiationException | IllegalAccessException | SQLException e)
                {
                }
            }
            

            【讨论】:

            • 传递查询可能会导致更改结构。而不是removeRow()insertRow(),使用setDataVector() 可能会更好。它一次替换数据和列列表。
            • 表格呈现时结果集中的列名丢失?
            【解决方案8】:

            JTable 构造函数接受两个参数,二维对象数组作为数据,字符串数组作为列名。

            例如:

            import java.awt.BorderLayout;
            import java.awt.Color;
            import javax.swing.JFrame;
            import javax.swing.JPanel;
            import javax.swing.JScrollPane;
            import javax.swing.JTable;
            
            public class Test6 extends JFrame {
            
                public Test6(){     
                    this.setSize(300,300);
                    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    Mpanel m = new Mpanel();
                    this.add(m,BorderLayout.CENTER);        
                }
            
            
                class Mpanel extends JPanel {       
            
                    JTable mTable;
                    private Object[][] cells = {{"Vivek",10.00},{"Vishal",20.00}};
                    private String[] columnNames = { "Planet", "Radius" };
                    JScrollPane mScroll;
            
                    public Mpanel(){
                        this.setSize(150,150);
                        this.setComponent();
                    }
            
                    public void setComponent(){
                        mTable = new JTable(cells,columnNames);
                        mTable.setAutoCreateRowSorter(true);
                        mScroll = new JScrollPane(mTable);
            
                        this.add(mScroll);
                    }
                }
            
                public static void main(String[] args){     
                    new Test6().setVisible(true);
                }
            }
            

            【讨论】:

              【解决方案9】:

              去这里java tips weblog

              然后,放入您的项目:listtabelmodel.java 和 rowtablemodel.java 使用此代码添加另一个类:

                  enter code here
              /*
               * To change this license header, choose License Headers in Project Properties.
               * To change this template file, choose Tools | Templates
               * and open the template in the editor.
               */
              package comp;
              
              import java.awt.*;
              import java.sql.*;
              import java.util.*;
              import javax.swing.*;
              import static javax.swing.JFrame.EXIT_ON_CLOSE;
              import javax.swing.table.*;
              
              public class TableFromDatabase extends JPanel {
              
                  private Connection conexao = null;
              
                  public TableFromDatabase() {
                      Vector columnNames = new Vector();
                      Vector data = new Vector();
              
                      try {
                          //  Connect to an Access Database
                          conexao = DriverManager.getConnection("jdbc:mysql://" + "localhost"
                                  + ":3306/yourdatabase", "root", "password");
              
                          //  Read data from a table
                          String sql = "select * from tb_something";
                          Statement stmt = conexao.createStatement();
                          ResultSet rs = stmt.executeQuery(sql);
                          ResultSetMetaData md = rs.getMetaData();
                          int columns = md.getColumnCount();
              
                          //  Get column names
                          for (int i = 1; i <= columns; i++) {
                              columnNames.addElement(md.getColumnName(i));
                          }
              
                          //  Get row data
                          while (rs.next()) {
                              Vector row = new Vector(columns);
              
                              for (int i = 1; i <= columns; i++) {
                                  row.addElement(rs.getObject(i));
                              }
              
                              data.addElement(row);
                          }
              
                          rs.close();
                          stmt.close();
                          conexao.close();
                      } catch (Exception e) {
                          System.out.println(e);
                      }
              
                      //  Create table with database data
                      JTable table = new JTable(data, columnNames) {
                          public Class getColumnClass(int column) {
                              for (int row = 0; row < getRowCount(); row++) {
                                  Object o = getValueAt(row, column);
              
                                  if (o != null) {
                                      return o.getClass();
                                  }
                              }
              
                              return Object.class;
                          }
                      };
              
                      JScrollPane scrollPane = new JScrollPane(table);
                      add(scrollPane);
              
                      JPanel buttonPanel = new JPanel();
                      add(buttonPanel, BorderLayout.SOUTH);
                  }
              
                  public static void main(String[] args) {
              
                      javax.swing.SwingUtilities.invokeLater(new Runnable() {
                          public void run() {
                              JFrame frame = new JFrame("any");
                              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              
                              //Create and set up the content pane.
                              TableFromDatabase newContentPane = new TableFromDatabase();
                              newContentPane.setOpaque(true); //content panes must be opaque
                              frame.setContentPane(newContentPane);
              
                              //Display the window.
                              frame.pack();
                              frame.setVisible(true);
                          }
                      });
              
                  }
              }
              

              然后把这个类拖到你的jframe中,就完成了

              它已被弃用,但它可以工作............

              【讨论】:

                【解决方案10】:

                Row 类将处理数据库中的一行。

                负责填充 UI 的 UpdateTask 的完整实现。

                import java.sql.ResultSet;
                import java.sql.SQLException;
                import java.util.ArrayList;
                import java.util.List;
                
                import javax.swing.JTable;
                import javax.swing.SwingWorker;
                
                public class JTableUpdateTask extends SwingWorker<JTable, Row> {
                
                    JTable      table       = null;
                
                    ResultSet   resultSet   = null;
                
                    public JTableUpdateTask(JTable table, ResultSet rs) {
                        this.table = table;
                        this.resultSet = rs;
                    }
                
                    @Override
                    protected JTable doInBackground() throws Exception {
                        List<Row> rows = new ArrayList<Row>();
                        Object[] values = new Object[6];
                        while (resultSet.next()) {
                            values = new Object[6];
                            values[0] = resultSet.getString("id");
                            values[1] = resultSet.getString("student_name");
                            values[2] = resultSet.getString("street");
                            values[3] = resultSet.getString("city");
                            values[4] = resultSet.getString("state");
                            values[5] = resultSet.getString("zipcode");
                            Row row = new Row(values);
                            rows.add(row);
                        }
                        process(rows); 
                        return this.table;
                    }
                
                    protected void process(List<Row> chunks) {
                        ResultSetTableModel tableModel = (this.table.getModel() instanceof ResultSetTableModel ? (ResultSetTableModel) this.table.getModel() : null);
                        if (tableModel == null) {
                            try {
                                tableModel = new ResultSetTableModel(this.resultSet.getMetaData(), chunks);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                            this.table.setModel(tableModel);
                        } else {
                            tableModel.getRows().addAll(chunks);
                        }
                        tableModel.fireTableDataChanged();
                    }
                }
                

                表模型:

                import java.sql.ResultSetMetaData;
                import java.sql.SQLException;
                import java.util.ArrayList;
                import java.util.List;
                
                import javax.swing.table.AbstractTableModel;
                
                /**
                 * Simple wrapper around Object[] representing a row from the ResultSet.
                 */
                class Row {
                    private final Object[]  values;
                
                    public Row(Object[] values) {
                        this.values = values;
                    }
                
                    public int getSize() {
                        return values.length;
                    }
                
                    public Object getValue(int i) {
                        return values[i];
                    }
                }
                
                // TableModel implementation that will be populated by SwingWorker.
                public class ResultSetTableModel extends AbstractTableModel {
                    private final ResultSetMetaData rsmd;
                
                    private List<Row>               rows;
                
                    public ResultSetTableModel(ResultSetMetaData rsmd, List<Row> rows) {
                        this.rsmd = rsmd;
                        if (rows != null) {
                            this.rows = rows;
                        } else {
                            this.rows = new ArrayList<Row>();
                        }
                
                    }
                
                    public int getRowCount() {
                        return rows.size();
                    }
                
                    public int getColumnCount() {
                        try {
                            return rsmd.getColumnCount();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        return 0;
                    }
                
                    public Object getValue(int row, int column) {
                        return rows.get(row).getValue(column);
                    }
                
                    public String getColumnName(int col) {
                        try {
                            return rsmd.getColumnName(col + 1);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        return "";
                    }
                
                    public Class<?> getColumnClass(int col) {
                        String className = "";
                        try {
                            className = rsmd.getColumnClassName(col);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        return className.getClass();
                    }
                
                    @Override
                    public Object getValueAt(int rowIndex, int columnIndex) {
                        if(rowIndex > rows.size()){
                            return null;
                        }
                        return rows.get(rowIndex).getValue(columnIndex);
                    }
                
                    public List<Row> getRows() {
                        return this.rows;
                    }
                
                    public void setRows(List<Row> rows) {
                        this.rows = rows;
                    }
                }
                

                构建 UI 并进行数据库连接的主应用程序

                import java.awt.BorderLayout;
                import java.awt.Dimension;
                import java.awt.event.ActionEvent;
                import java.awt.event.ActionListener;
                import java.sql.Connection;
                import java.sql.DriverManager;
                import java.sql.PreparedStatement;
                import java.sql.ResultSet;
                
                import javax.swing.JButton;
                import javax.swing.JFrame;
                import javax.swing.JTable;
                
                public class MainApp {
                    static Connection conn = null;
                    static void init(final ResultSet rs) {
                        JFrame frame = new JFrame();
                        frame.setLayout(new BorderLayout());
                        final JTable table = new JTable();
                        table.setPreferredSize(new Dimension(300,300));
                        table.setMinimumSize(new Dimension(300,300));
                        table.setMaximumSize(new Dimension(300,300));
                        frame.add(table, BorderLayout.CENTER);
                        JButton button = new JButton("Start Loading");
                        button.setPreferredSize(new Dimension(30,30));
                        button.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                JTableUpdateTask jTableUpdateTask = new JTableUpdateTask(table, rs);
                                jTableUpdateTask.execute();
                
                            }
                        });
                        frame.add(button, BorderLayout.SOUTH);
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.pack();
                        frame.setVisible(true);
                    }
                
                    public static void main(String[] args) {
                        String url = "jdbc:mysql://localhost:3306/test";
                        String driver = "com.mysql.jdbc.Driver";
                        String userName = "root";
                        String password = "root";
                        try {
                            Class.forName(driver).newInstance();
                            conn = DriverManager.getConnection(url, userName, password);
                            PreparedStatement pstmt = conn.prepareStatement("Select id, student_name, street, city, state,zipcode from student");
                            ResultSet rs = pstmt.executeQuery();
                            init(rs);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                

                【讨论】:

                • 非常感谢兄弟,我会试试这个,让你知道结果。
                • 我刚刚编辑了我的代码,现在它有完整的工作解决方案,您可以更改选择中的列名并相应地更新任务
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2010-10-27
                • 2012-01-27
                • 1970-01-01
                • 1970-01-01
                • 2018-12-14
                • 1970-01-01
                • 2014-11-22
                相关资源
                最近更新 更多