【发布时间】:2018-06-11 08:52:17
【问题描述】:
我有一个通过 JDBC 连接到我的数据库的表单,我创建了一个方法,我打算打印出我的表中的行数,如下所示:
public static void getRowCount(String sql, PrintWriter printThis)
{
try
{
Connection c = // Another function called from a different class that handles the connection
PreparedStatement p = (PreparedStatement) connect.prepareStatement(sql);
ResultSet r = p.executeQuery();
while (r.next())
{
printThis.println(r.toString());
}
c.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
如果我传递一个 sql 字符串 "Select COUNT(*) FROM MyTable;" 作为参数,它应该打印出我的表的行数。例如,如果我当前在 MyTable 中有 4 行,它应该打印 4 但它打印出来的只是一个带有随机数字和字母的对象:
com.mysql.jdbc.JDBC42ResultSet@7e11fc40
将我的 sql 查询的结果传递给我的函数后如何显示它?
【问题讨论】:
标签: java mysql database count resultset