我一直在尝试修复任何错误,但仍然会导致结果集错误。我的代码有什么问题吗?
stmt=con.createStatement();
String sql = "SELECT * FROM CUSTOMER";
rs=stmt.executeQuery(sql);
rs.next();
int id_col = rs.getInt("CUSTOMER_ID");
String id = Integer.toString(id_col);
String name= rs.getString("CUSTOMER_NAME");
String address = rs.getString("CUSTOMER_ADDRESS");
String phone = rs.getString ("CUSTOMER_PHONE");
String email = rs.getString ("CUSTOMER_EMAIL");
textID.setText(id);
textName.setText(name);
textAddress.setText(address);
textPhone.setText(phone);
textEmail.setText(email);
}
catch (SQLException err) {
JOptionPane.showMessageDialog(ComputerShop.this,err.getMessage());
}
}
答案 0 :(得分:0)
通过阅读它在Java中看起来像SQL的代码,我理解了什么。例如,如果您尝试从单个表中选择乘法行,则也可以这样做。
public static void customerName(Connection b, String c) throws SQLException
{
c = "'"+c+"'";
try
{
b = DriverManager.getConnection(dbURL, user, password);
b.clearWarnings();
stmt = b.createStatement();
ResultSet rs = stmt.executeQuery("SELECT CONTACTNAME FROM CUSTOMERS WHERE CUSTOMERID =" + c); // this is basicly where you install the SQL code minus the ";" at the end.
while(rs.next())
{
System.out.print(rs.getString("CONTACTNAME") + " ");
}
}
catch(SQLException e)
{
System.err.println(e.getMessage());
}
finally
{
if(!b.isClosed())
{
b.close();
}
}
}
我发现这通常有助于我保持一切清洁并组织我的代码。这将是我从我连接到数据库的主方法调用的方法。