如何从java中读取MySQL表中的数据
2个问题,它输出的是如何从java中读取MySQL表中的数据,java,mysql,sql,jdbc,Java,Mysql,Sql,Jdbc,2个问题,它输出的是12345,而不是12345,我不知道为什么后面会出现成功,但不会从SQL表中打印数据 Statement st = null; ResultSet rs = null; try { System.out.println("1"); Connection con = DriverManager.getConnection(url, user, pass); System.out.println("2"); //Class.forName(d
12345
,而不是12345
,我不知道为什么后面会出现成功,但不会从SQL表中打印数据
Statement st = null;
ResultSet rs = null;
try {
System.out.println("1");
Connection con = DriverManager.getConnection(url, user, pass);
System.out.println("2");
//Class.forName(driver);
System.out.println("3");
st = con.createStatement();
rs = st.executeQuery("SELECT productcost " +
"FROM producttable " +
"WHERE productid = 3;");
System.out.println("4");
if (rs.next()) { //get first result
System.out.println(rs.getInt(1)); //coloumn 1
}
System.out.println("5");
System.out.println("Success");
} catch (Exception e) {
System.out.println("Error");
}
您必须将“3”作为第一列中的数据!因为您正在打印第一列记录的数据。我看这里没有任何问题 之所以看到
3
介于4
和5
之间,是因为结果集包含3
检索当前行中指定列的值
此ResultSet对象是一个int
尝试调试您的代码并逐步完成该部分。您将看到我在下面显示的内容
你能写更多关于你的问题的细节吗。。可能它输出了
123435成功
,因为查询中的productcost
等于3
,当它们之间有println(rs.getInt(1))
时,为什么您希望它不打印4
和5
之间的内容。当它打印值3
,即productcost
的值时,为什么说它“不打印sql表中的数据”?如果您将print语句更改为类似于println(“productcost=“+rs.getInt(1))
,您可能会识别自己的输出!感谢您抽出时间提供答案。正是因为像你这样乐于助人的同龄人,我们才能作为一个社区一起学习。以下是一些如何让你的答案更完美的技巧:。布莱恩,谢谢你提供的信息。我对这个平台还是新手。随着时间的推移,我会努力提高自己@安德烈亚斯哈哈!更正!我的世界中的RS==ADODB.Recordset。在我们的许多遗留应用程序中大量使用。非常感谢!!真不敢相信我竟然没有意识到这一点