Reputation: 663
I can't access the idRooms
column with the code below
try{
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HSM_schema","root","");
Statement myStmt = myConn.createStatement();
ResultSet myRs = myStmt.executeQuery("select * from Rooms");
while(myRs.next()){
System.out.println(myRs.getString("idRooms ")+", "+ myRs.getString("RoomType"));
}
}
catch (Exception ex){
ex.printStackTrace();
}
Here is my table:
When I run myRs.getString("RoomType")
it works perfectly fine. Any thoughts?
Thanks you.
Upvotes: 1
Views: 214
Reputation: 5712
You are having an space at end of the column name "idRooms " that must be the reason.
Since idRooms
is a int
column I suggest you to use getInt
method instead of string method
so you can use
System.out.println(myRs.getInt("idRooms")+", "+ myRs.getString("RoomType"));
or
System.out.println(myRs.getString(1)+", "+ myRs.getString(2)); // using coloumn index
or
System.out.println(myRs.getInt(1)+", "+ myRs.getString(2));
Upvotes: 1
Reputation: 311228
You have a redundant space after idRooms
. Since you don't have a columns called idRooms
, this will fail.
Just remove the redundant space, and you should be fine:
while(myRs.next()){
System.out.println(myRs.getString("idRooms")+", "+ myRs.getString("RoomType"));
// Here-----------------------------------^
}
Upvotes: 2
Reputation: 52185
You seem to have an extra white space at the of if idRooms
. Change myRs.getString("idRooms ")
to myRs.getString("idRooms")
.
Upvotes: 3