Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:account","sa","123456");
Statement sta=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=sta.executeQuery("select * from serverIDINFO");
ResultSetMetaData rsmd=rs.getMetaData();
//先得到总共有多少列
int cols=rsmd.getColumnCount();
//再得到总共有多少条数据
int all=0;
while(rs.next()){
all++;
}
rs.beforeFirst();
for(int i=0;i<cols;i++){
System.out.print("----------------");
}
System.out.println();
for(int i=0;i<cols;i++){
String name=rsmd.getColumnName(i+1);
if(name.length()<8)
System.out.print("|\t"+name+"\t");
else
System.out.print("|"+name+"\t");
if(i==cols-1){
System.out.println("|");
}
}
for(int i=0;i<cols;i++){
System.out.print("----------------");
}
System.out.println();
for(int i=0;i<all;i++){
rs.next();
for(int j=0;j<cols;j++){
System.out.print("|\t"+rs.getString(j+1).trim()+"\t");
if(j==cols-1)
System.out.println("|");
}
for(int k=0;k<cols;k++){
System.out.print("----------------");
}
System.out.println();
}
试试这个方法先