public String DBMessage(long id){
String dbdriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://localhost:3333;databaseName=aidb";
String us="sa";
String pw="sa";
String sql="select * from ColumnItem where id="+id+"";
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
StringBuffer sb= new StringBuffer();
try {
Class.forName(dbdriver);
conn=DriverManager.getConnection(url, us, pw);
stat=conn.createStatement();
rs=stat.executeQuery(sql);
while(rs.next()){
ResultSetMetaData rsmd = rs.getMetaData();
int counts = rsmd.getColumnCount();
for (int col = 0; col < counts; col++) {
String colname = rsmd.getColumnName(col+1);
// String colclass=(String) rs.getObject(colname);
sb.append(colname);
sb.append(",");
// sb.append(colclass);
sb.append("\r\n");
System.out.println(colname);
}
// str += rs.getInt("colId");
// str += ",";
// str += rs.getInt("sid");
// str += ",";
// str += rs.getInt("id");
// str += ",";
// str += rs.getString("colCode");
// str += ",";
// str += rs.getString("colName");
// str += ",";
// str += rs.getInt("colPid");
// str += ",";
// str += rs.getInt("colLevel");
// str += ",";
// str += rs.getInt("colType");
// str += ",";
// str += rs.getInt("colLenght");
// str += ",";
// str += rs.getString("colDate");
// str += ",";
// str += rs.getInt("com_id");
// str += ",";
// str += rs.getInt("display");
// str += ",";
// str += rs.getString("formula");
// str += ",";
// str += rs.getInt("isText");
// str += "\r\n";
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
for里面应该怎么写才能自动获取到表列和表列的值?现表列已经获取到了,表列的值怎么获取?
------解决方案--------------------
rs.getInt(index);
或者
rs.getInt("colname");
不就行了么。
------解决方案--------------------
- Java code
public class Test{ public String DBMessage(long id){ String dbdriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String url="jdbc:sqlserver://localhost:3333;databaseName=aidb"; String us="sa"; String pw="sa"; String sql="select * from ColumnItem where id="+id+""; Connection conn=null; Statement stat=null; ResultSet rs=null; StringBuffer sb= new StringBuffer(); try { Class.forName(dbdriver); conn=DriverManager.getConnection(url, us, pw); stat=conn.createStatement(); rs=stat.executeQuery(sql); while(rs.next()){ ResultSetMetaData rsmd = rs.getMetaData(); int counts = rsmd.getColumnCount(); for (int col = 0; col < counts; col++) { String colname = rsmd.getColumnName(col+1); sb.append(rsmd.getColumnName(col+1)+","); System.out.println(colname); } sb.setLength(sb.length()-1); sb.append("\r\n"); while(rs.next()){ sb.append(rs.getInt("colId")+","); sb.append(rs.getInt("sid")+","); //....自己写 sb.setLength(sb.length()-1); sb.append("\r\n"); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return sb.toString(); } }//楼主这问题还在纠结啊?排版很简单啊,难得是取数据