0

I have created a GUI based Hotel Management System in java netbeans which is connected with Ms Access Database. In database, i have a table named "RoomInfo".

When i try to execute the following query, i get two kinds of errors.

String sql = "INSERT INTO RoomInfo(RoomNumber,Reserved,RoomCategory,AirConditioned,
    BedType, RentPerDay)VALUES("+objr.roomno+","+objr.reserved+","+objr.category+","
   +objr.AirConditioned+","+objr.bedtype+","+objr.rent+")";

First error is net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.7 unexpected token: , I get this error when i leave all the Jtextfields empty and try to insert new record in to the database.

Second Error is net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.7 user lacks privilege or object not found: Economy (Economy is the entry for room category) I get this error when i enter data in to the Jtextfields and try to save it in to the database.

Need help identifying the problem.

1
  • 1
    Two problems: (1) You should be using question marks (?) as parameter placeholders in your SQL command text, e.g., ... VALUES (?,?,?,?,?,?). (2) You are doing your "None of the fields can be left empty" check after trying to insert the row, instead of before. Commented Nov 19, 2016 at 21:16

1 Answer 1

3

Instead of passing the the variable names directly into the sql string and then setting them again using ps.setString(); just use place holders in the sql string. What I mean is

String sql = "INSERT INTO RoomInfo (RoomNumber, Reserved, RoomCategory, AirConditioned, BedType, RentPerDay) 
VALUES (?, ?, ?, ?,?)";
        ps = con.prepareStatement(sql);

        ps.setString(1, objr.roomno);
        ps.setString(2, objr.reserved);
        ps.setString(3, objr.category);
        ps.setString(4, objr.AirConditioned);
        ps.setString(5, objr.bedtype);
        ps.setString(6, objr.rent);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. So the syntax of my INSERT QUERY is wrong ?
Looks that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.