我正在尝试获取第一列(列ID并获取关联的名字和姓氏)。我尝试了很多东西,却无法让它发挥作用。
它返回我的错误CUSTOMERID未找到。
public String getCustomerUsingId(int id){
T
我使用H2作为数据库,这就像是
String firstName = null;
String lastName = null;
Statement stmt = null;
String getCustomerQuery = "SELECT FIRSTNAME,LASTNAME FROM CUSTOMERS WHERE CUSTOMERID ='"
+ id + "'";
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
stmt.execute(getCustomerQuery);
ResultSet rs = stmt.getResultSet();
if(rs.next()){
id = rs.getInt("CUSTOMERID");
System.out.println("id is:" + id);
if(id == -1){
System.out.println("value is true");
firstName = rs.getString(2);
lastName = rs.getString(3);
System.out.println("First Name :" + firstName);
System.out.println("First Name :" + lastName);
}
}
}
这就是我创建表格的方式
CUSTOMERID FIRSTNAME LASTNAME
1 P S
2 K S
答案 0 :(得分:2)
您还需要在选择查询中明确包含列名。
因此,变量getCustomerQuery
应该类似于
String getCustomerQuery = "SELECT CUSTOMERID,FIRSTNAME,LASTNAME FROM CUSTOMERS WHERE CUSTOMERID ='"
+ id + "'";