Question:
The error i get is Java.sql.SqlException..dis is the error?
Frnds
2010-09-07 20:18:33 UTC
have givn my coding and db structure i get a error java.sql exception.What is dis error..plss help...?
try{
con = DriverManager.getConnection("jdbc:mysql:…
stmt = con.createStatement();
String rt = (String)rtcb.getSelectedItem();
if(rt.equals("Single")){
String query = "select roomno from room where roomtype = 'Single' ;";
System.out.println(query);
ResultSet rs = stmt.executeQuery(query);
rncb.addItem(rs.getString("Roomno"));
}
else if (rt.equals("Double")){
String query = "select roomno from room where roomtype = 'Double' ;" ;
System.out.println(query);
ResultSet rs = stmt.executeQuery(query);
rncb.addItem(rs.getString("Roomno"));

}
else{
String query = "select roomno from room where roomtype = 'Triple' ;" ;
System.out.println(query);
ResultSet rs = stmt.executeQuery(query);
rncb.addItem(rs.getString("Roomno"));
}


}
catch(Exception e){
op.showMessageDialog(null,"Error in connection " + e);
}
}
CREATE TABLE ROOM
(ROOMNO INT(5) PRIMARY KEY,
ROOMTYPE VARCHAR(15) NOT NULL,
NOOFBEDS INT(10) NOT NULL,
MONTHLYFEE DECIMAL(9,2));
Three answers:
Nigel
2010-09-08 00:39:18 UTC
Guessing at your error - but you don't need the semi-colon at the end of your SQL statements ( and I mean inside the quotes - not outside ).



String query = "select roomno from room where roomtype = 'Single'";



As a matter of coding though you have 3 sets of code which are almost identical - you should be thinking about how you can reduce these types of situations and coming up with common code and putting that in methods.
TheMadProfessor
2010-09-08 14:20:30 UTC
Depending on the DBMS, it is likely complaing either about the semicolon at the end of the query string (some expect it, some don't) or with using single quotes around your literal instead of double (some are picky about this, some aren't.)



As another suggested, it would be less messy (not to mention flexible) if you eliminated the redundant nested ifs. Instead, something like





String query = "select roomno from room where roomtype = '" & rt & "'";



not only eliminates the need for duplicated code, but allows you to add new room types without changing the code.
Linkz
2010-09-08 03:34:30 UTC
Maybe i am an idiot, but saying "have givn my coding and db structure" does not actually give us the information we need.

we need the actual error message, or maybe you're posting it somewhere else, if so, we need to know where.



If you don't know how to get us the actual error message, then we can't help.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...