Yousaf
Yousaf

Reputation: 29282

Inserting into Access database from NetBeans

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.

Upvotes: 0

Views: 3234

Answers (1)

Charles
Charles

Reputation: 112

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);

Upvotes: 3

Related Questions