我正在使用Java和MySQL的Web应用程序工作。无法从ResultSet中获取值
我创建了一个方法,该方法应该根据数据库中的各个表返回各个列名的ArrayList。
但是,当我调试该方法时,我意识到while(rs.next())
会导致错误。我用这个site作为参考,因此我不确定哪里出了问题。
这是代码。谢谢。
// Returns the the all the columns in the table
public ArrayList getColumnName(String tableName) throws SQLException {
ResultSet rs = null;
List<String> columnName = new ArrayList<String>();
Statement st = null;
Connection con = null;
try {
// Get a connection from the connection factory
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/information_schema", "root", "xxxx");
// Create a Statement object so we can submit SQL statements to the driver
st = con.createStatement();
StringBuilder sql = new StringBuilder("SELECT column_name FROM information_schema.columns " +
"WHERE table_schema = 'testDB' AND table_name = '");
sql.append(tableName).append("'");
rs = st.executeQuery(sql.toString());
while (rs.next()) { // getting error..
columnName.add(rs.getString("column_name"));
}
} catch (SQLException ex) {
Logger.getLogger(ModificationPage.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (con != null || st != null) {
st.close();
con.close();
}
}
return (ArrayList) columnName;
}
你看到什么错误? –
没有堆栈跟踪,发生的事情是从while(rs.next())开始,它会逐步捕获(SQLException ex)。同时,在我的Web应用程序中,页面将刷新,而应该显示的表格组件不存在。 –