窗体上有一JTable,如何让数据库里的数据在表格里显示出来

找了点代码,学习中

import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableColumn;

public class tableUI extends JPanel

{
public tableUI(String databaseName, String tableName, String[] colNames,
int[] colWidth, int totalWidth) {
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException e) {
System.out.println(e);
}
tableHandler = new TableHandler(databaseName, tableName, colNames);
tableHandler.updateTable();
TableColumn column = null;
table = new JTable(tableHandler);
table
.setPreferredScrollableViewportSize(new Dimension(totalWidth,
210));
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);

for (int i = 0; i < colWidth.length; i++) {

column = table.getColumnModel().getColumn(i);

column.setPreferredWidth(colWidth[i] * 100);

}

JScrollPane s = new JScrollPane(table);
this.add(s, BorderLayout.CENTER);
this.setSize(this.getPreferredSize());
setVisible(true);
}

JTable table;

JScrollPane scrollPane;

public TableHandler tableHandler;

}


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.table.AbstractTableModel;

public class TableHandler extends AbstractTableModel {
   public TableHandler(String databaseName, String tableName, String[] colNames) {
       super();
       this.tableName = tableName;
       this.databaseName = databaseName;
       colNum = colNames.length;
       this.colNames = new String[colNum];
       System.arraycopy(colNames, 0, this.colNames, 0, colNum);
   }

   public void updateTable() {
       try {
           Connection conn;

           conn = DriverManager.getConnection("jdbc:mysql://localhost/"
                   + databaseName + "?user=root&password=");

           Statement myStatement = conn.createStatement();
           ResultSet rt = myStatement.executeQuery("SELECT count(*) FROM "
                   + tableName);
           rt.next();
           rowNum = rt.getInt(1);
           list = new String[rowNum][colNum];
           ResultSet rs = myStatement.executeQuery("SELECT * FROM "
                   + tableName);

           int row = 0;
           while (rs.next()) {
               for (int i = 0; i < colNum; i++) {
                   list[row][i] = rs.getString(colNames[i]);
               }
               row++;
           }
           conn.close();        
           fireTableDataChanged();
       } catch (SQLException e) {
           System.out.println(e);
       }
   }

   public int getColumnCount() {
       return colNum;
   }

   public int getRowCount() {
       return rowNum;
   }

   public String getColumnName(int col) {
       return colNames[col];
   }

   public Object getValueAt(int row, int column) {
       return list[row][column];
   }
   String[][] list;
   String[] colNames;
   String tableName;
   String databaseName;
   int rowNum;
   int colNum;
}

代码2:

import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;

/**
* TableQueryFrame extends JInternalFrame to create a display which builds SQL
* CREATE statements
*/

class TableQueryFrame extends JInternalFrame
{
JTable table;
JScrollPane tableScroller;
DatabaseUtilities dbUtils;

String tableName = null;
String colNames[] = null;
String dataTypes[] = null;
String SQLQuery = null;

public TableQueryFrame(String tableName, DatabaseUtilities dbUtils)
{
setSize(450,300);
setClosable(true);
setMaximizable(true);
setIconifiable(true);
setResizable(true);
getContentPane().setLayout(new BorderLayout());
this.tableName=tableName;
this.dbUtils=dbUtils;

colNames = dbUtils.getColumnNames(tableName);
dataTypes = dbUtils.getDataTypes(tableName);
SQLQuery = "SELECT TOP 5 * FROM "+tableName;
Vector dataSet = dbUtils.executeQuery(SQLQuery);
table = createTable(colNames,dataSet);
tableScroller = new JScrollPane(table);
getContentPane().add(tableScroller,BorderLayout.CENTER);

setVisible(true);
}

JTable createTable(String[] colNames,Vector dataSet)
{
int nRows = dataSet.size();
String[][] rowData = new String[nRows][colNames.length];
for(int i=0;i<nRows;i++)
{
Vector row = (Vector)dataSet.elementAt(i);
for(int j=0;j<row.size();j++)
rowData[i][j]=((Object)row.elementAt(j)).toString();
}
JTable table = new JTable(rowData,colNames);
return table;
}
}

你可能感兴趣的:(代码)