这是表的显示的文件 package examsystem;import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.JFrame;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class StudentFrame extends JFrame { private boolean DEBUG = true; public StudentFrame(Object[][] data) { super("SimpleTableDemo"); String[] columnNames = {"学号", "姓名", "科目1", "科目2", "科目3"}; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); if (DEBUG) { table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { printDebugData(table); } }); } //Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this window. getContentPane().add(scrollPane, BorderLayout.CENTER); } private void printDebugData(JTable table) { int numRows = table.getRowCount(); int numCols = table.getColumnCount(); javax.swing.table.TableModel model = table.getModel(); System.out.println("Value of data: "); for (int i=0; i < numRows; i++) { System.out.print(" row " + i + ":"); for (int j=0; j < numCols; j++) { System.out.print(" " + model.getValueAt(i, j)); } System.out.println(); } System.out.println("--------------------------"); } }
这是数据库的连接文件 把读出来的数据赋给Object[][] package examsystem;public class Loginconnect { private Object[][] dat; public Loginconnect() { String[] a ={"1","2"}; dat = new Object[][]{ {"Mary", "Campione", new Integer(5), new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Chasing toddlers", new Integer(2), new Boolean(false)}, }; } public Object[][] get_data(){ return dat; } }
public Class getColumnClass(int column) { int type; try{ type = rsmd.getColumnType(column+1); } catch (SQLException e) { return super.getColumnClass(column); } switch(type) { case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: return String.class; case Types.BIT: return Boolean.class; case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: return Integer.class; case Types.BIGINT: return Long.class; case Types.FLOAT: case Types.DOUBLE: return Double.class; case Types.DATE: return java.sql.Date.class; default: return Object.class; } }
public String getColumnName(int column) { if (columnNames[column] != null) { return columnNames[column]; } else { return ""; } }
public int getColumnCount(){ //return columnHeaders.size(); return columnNames.length; }
public int getRowCount(){ return tableData.size(); }
时间关系我简单说一下! try{ //获取字段的名称 ResultSetMetaData rsmd = rs.getMetaData(); for ( int i = 1; i <= rsmd.getColumnCount(); ++i ) System.out.print(rsmd.getColumnName( i )+" "); //获取记录集 } ....
package examsystem;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class StudentFrame extends JFrame {
private boolean DEBUG = true;
public StudentFrame(Object[][] data) {
super("SimpleTableDemo"); String[] columnNames = {"学号",
"姓名",
"科目1",
"科目2",
"科目3"}; final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70)); if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
} //Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this window.
getContentPane().add(scrollPane, BorderLayout.CENTER); }
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel(); System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
}
把读出来的数据赋给Object[][]
package examsystem;public class Loginconnect {
private Object[][] dat; public Loginconnect() {
String[] a ={"1","2"};
dat = new Object[][]{
{"Mary", "Campione",
new Integer(5), new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Chasing toddlers", new Integer(2), new Boolean(false)},
};
} public Object[][] get_data(){
return dat;
}
}
void login_actionPerformed(ActionEvent e) {
Loginconnect connect = new Loginconnect();
StudentFrame student = new StudentFrame(connect.get_data());
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = student.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
student.setLocation((screenSize.width - frameSize.width) /4, (screenSize.height - frameSize.height) / 3);
student.setVisible(true);
student.pack();
student.setVisible(true);
}
int count=rsmd.getColumnCount();//getColumnCount();
columnNames = new String[count];
//columnHeaders=new Vector(count);
tableData=new Vector();
/*for(int i=1;i<=count;i++){
columnHeaders.addElement(rsmd.getColumnName(i));
}*/
for(int column = 0; column < count; column++) {
columnNames[column] = rsmd.getColumnLabel(column+1);
}
while(rs.next()){
rowData=new Vector(count);
for(int i=1;i<=count;i++){
rowData.addElement(rs.getObject(i));
}
tableData.addElement(rowData);
}
}
public Class getColumnClass(int column) {
int type;
try{
type = rsmd.getColumnType(column+1);
}
catch (SQLException e) {
return super.getColumnClass(column);
} switch(type) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
return String.class; case Types.BIT:
return Boolean.class; case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
return Integer.class; case Types.BIGINT:
return Long.class; case Types.FLOAT:
case Types.DOUBLE:
return Double.class; case Types.DATE:
return java.sql.Date.class; default:
return Object.class;
}
}
public String getColumnName(int column) {
if (columnNames[column] != null) {
return columnNames[column];
} else {
return "";
}
}
public int getColumnCount(){
//return columnHeaders.size();
return columnNames.length;
}
public int getRowCount(){
return tableData.size();
}
public Object getValueAt(int row,int column){
Vector rowData=(Vector)(tableData.elementAt(row));
return rowData.elementAt(column);
//Vector row = (Vector)rows.elementAt(aRow);
//return row.elementAt(aColumn);
}
public boolean isCellEditable(int row, int column){
return false;
}
/*public String getColumnName(int column){
return (String)(columnHeaders.elementAt(column));
}*/
}
这些就是读取数值的代码
try{
//获取字段的名称
ResultSetMetaData rsmd = rs.getMetaData();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
System.out.print(rsmd.getColumnName( i )+" ");
//获取记录集
}
....