请教各位,如何用jTable显示一个表中的数据?
在线等!
谢谢!
在线等!
谢谢!
解决方案 »
- 新手求助~
- 这一段代码中的 << 是什么意思
- NETBEANS中数据四舍五入问题(非常奇怪)
- JAVA编写如下程序
- 我想问一下,为什么要用char[]呢?char数组究竟有什么好处,什么时候用呢?
- java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
- 关于Java的理论问题~!
- 马上要学java了,是学 Visual java++,还是学javaborland,我以前是用VB的!
- 有关在Jbuilder7中做Jsp的小问题?答的好给高分!!!
- 高分!!!急用!!简单的TOMCAT 4.0.1 的问题!!!
- --------创建包-----------
- moverToInsertRow到底把CONCUR定位到什么地方
AbstractTableModel tm; // 声明一个AbstractTableModel类对象tm
tm = new AbstractTableModel() { // 创建AbstractTableModel类对象tm
public int getColumnCount() { // 取得表格列数
return title.length;
}
public int getRowCount() { // 取得表格行数
return vector.size();
}
public Object getValueAt(int row, int column) { // 取得单元格中的属性值
if(!vector.isEmpty()) {
return ((Vector)vector.elementAt(row)).elementAt(column);
}
else {
return null;
}
}
public void setValueAt(Object value, int row, int column) { //看功能需要实现..
}
public String getColumnName(int column) { // 取得表格列名
return title[column];
}
public Class getColumnClass(int c) { // 取得列所属对象类
return getValueAt(0,c).getClass();
}
public boolean isCellEditable(int row, int column) { }//看功能需要实现..
};
用tm创建一个JTable对象..
//...............................JDBC方面大概是这样:
Connection connection = null; // 声明Connection接口对象connection
ResultSet rs = null; // 定义数据库查询的结果集
Statement statement = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // 实例化JDBC-ODBC桥的驱动
String url = "jdbc:odbc:TestDbStu"; // 设置连接字符串
connection = DriverManager.getConnection(url); // 连接数据库
// 创建Statement接口对象
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "select * from studentbase";
rs = statement.executeQuery(sql); // 执行查询语句
vector.removeAllElements(); // 初始化向量对象
tm.fireTableStructureChanged(); // 更新表格内容
while(rs.next()) {
Vector rec_vector = new Vector();
// 从结果集中取数据放入向量rec_vector中
rec_vector.addElement(String.valueOf(rs.getInt("学号")));
rec_vector.addElement(rs.getString("姓名"));
rec_vector.addElement(String.valueOf(rs.getInt("年龄")));
rec_vector.addElement(rs.getString("性别"));
rec_vector.addElement(rs.getString("系名"));
vector.addElement(rec_vector); // 向量rec_vector加入向量vector中
}
tm.fireTableStructureChanged(); // 更新表格,显示向量vector的内容
rs.close(); // 关闭结果集
}
catch(SQLException ex){ // 捕捉异常
System.out.println("\nERROR:----- SQLException -----\n");
while (ex != null) {
System.out.println("Message: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("ErrorCode: " + ex.getErrorCode());
ex = ex.getNextException();
}
}
catch(Exception ex ) {
ex.printStackTrace();
}
finally {
try {
if(statement != null) {
statement.close(); // 关闭Statement接口实例
}
if(connection != null) {
connection.close(); // 关闭Connection接口实例
}
}
catch (SQLException ex) {
System.out.println("\nERROR:----- SQLException -----\n");
System.out.println("Message: " + ex.getMessage( ));
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("ErrorCode: " + ex.getErrorCode());
}
}
刚学不久....抛砖引玉吧..