AbstractTableModel为什么JTable没有列名啊?我也重写的getColumnName方法啊,求解,在线等,代码如下
importjava.sql.*;importjava.util.*;importjavax.swing.table.*;;publicclassStudentModel...
import java.sql.*;
import java.util.*;
import javax.swing.table.*;;
public class StudentModel extends AbstractTableModel{
//用来定义数据库数据操作
Vector rowData,columnNames;
PreparedStatement ps = null;
Connection ct = null;
ResultSet rs = null;
public StudentModel(){
columnNames = new Vector();
columnNames.add("学号");
columnNames.add("名字");
columnNames.add("性别");
columnNames.add("年龄");
columnNames.add("籍贯");
columnNames.add("系别");
rowData = new Vector();
try {
//1.加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=company","sa","07071818w");
ps = ct.prepareStatement("select * from students");
rs = ps.executeQuery();
while(rs.next()){
Vector hang = new Vector();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getInt(4));
hang.add(rs.getString(5));
hang.add(rs.getString(6));
//加入到rowData中
rowData.add(hang);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
}
if(ct!=null){
ct.close();
}
if(ps!=null){
ps.close();
}
}catch(Exception err){
err.printStackTrace();
}
}
}
@Override
public String getColumnName(int column) {
// TODO Auto-generated method stub
return (String)this.columnNames.get(column);
}
//用来得到行数
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return this.rowData.size();
}
//用来得到列数
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return this.columnNames.size();
}
//用来得到某行某列的数据
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return ((Vector)this.rowData.get(rowIndex)).get(columnIndex);
}
} 展开
import java.util.*;
import javax.swing.table.*;;
public class StudentModel extends AbstractTableModel{
//用来定义数据库数据操作
Vector rowData,columnNames;
PreparedStatement ps = null;
Connection ct = null;
ResultSet rs = null;
public StudentModel(){
columnNames = new Vector();
columnNames.add("学号");
columnNames.add("名字");
columnNames.add("性别");
columnNames.add("年龄");
columnNames.add("籍贯");
columnNames.add("系别");
rowData = new Vector();
try {
//1.加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=company","sa","07071818w");
ps = ct.prepareStatement("select * from students");
rs = ps.executeQuery();
while(rs.next()){
Vector hang = new Vector();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getInt(4));
hang.add(rs.getString(5));
hang.add(rs.getString(6));
//加入到rowData中
rowData.add(hang);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
}
if(ct!=null){
ct.close();
}
if(ps!=null){
ps.close();
}
}catch(Exception err){
err.printStackTrace();
}
}
}
@Override
public String getColumnName(int column) {
// TODO Auto-generated method stub
return (String)this.columnNames.get(column);
}
//用来得到行数
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return this.rowData.size();
}
//用来得到列数
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return this.columnNames.size();
}
//用来得到某行某列的数据
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return ((Vector)this.rowData.get(rowIndex)).get(columnIndex);
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询