我正在尝试获取已创建并填充的表中的所有行。这是我目前正在尝试执行的功能,显然它不起作用。什么都不放出来。
public static void getTable(String[] table) throws ClassNotFoundException, SQLException{
DatabaseMetaData metadata = null;
Class.forName(driver);
conn = DriverManager.getConnection(connectionURL);
metadata = conn.getMetaData();
ResultSet res = metadata.getTables(null, null, null, table);
while(res.next()){
System.out.println(res.toString());
}
res.close();
conn.close();
}
任何想法我做错了什么?
哇,在玩了几个小时的组合之后。我有一些东西。这会让我很生气,但对于那些可能有这种问题的人来说,这就是我想出的从整个表格中获取数据的方法。
编辑:现在返回一个 HashMap,其中行号和值由“,”分隔。
/**
* Gets all rows of a table. Returns a HashMap.
*
* Returns a HashMap with each table row value separated by ','.
* Each row in a new count in the hashmap. Example:
*
* Returns:
*
* <1, "FirstName,LastName,Age">
*
*
* @param table
* @return HashMap<Int, String>
* @throws ClassNotFoundException
* @throws SQLException
*/
@SuppressWarnings("null")
public static HashMap<Integer, String> getTable(String table) throws ClassNotFoundException, SQLException{
DatabaseMetaData metadata = null;
Class.forName(driver);
conn = DriverManager.getConnection(connectionURL);
String sql = "select * from " + table; // use actual name of the table
PreparedStatement statement = conn.prepareStatement(sql);
ResultSet res = statement.executeQuery();
ResultSetMetaData md = res.getMetaData();
HashMap<Integer,String> hash = new HashMap();
int count = md.getColumnCount();
String done;
int y = 1;
while(res.next()){
for(int x = 1; x < md.getColumnCount(); x = x+md.getColumnCount()){
done = res.getObject(x).toString() + "," + res.getObject(x+1).toString();
}
hash.put(y, done);
y++;
}
res.close();
conn.close();
if(!hash.isEmpty())
return hash;
else
return null;
}