user8565055
user8565055

Reputation:

Syntax for MySQL trying to update a MySql table, however syntax is off

Currently this is my code to update my sql, and I'm getting a syntax error. the carid is being passed in from the front end. Can anyone assist me with fixing the syntax.

String carid = req.getParameter("id");

int rs;
Connection conn = null;
java.sql.PreparedStatement st= null;
String nativeSQL = "";

try {
    Context    ctx = new InitialContext();
    Context env = ( Context )ctx.lookup( "java:comp/env" );
    DataSource ds = ( DataSource )env.lookup( "jdbc/carRentalSystem");
    conn = ds.getConnection();

    st = conn.prepareStatement("update cardetails SET Availability = Unavailable where id='"+ carid+ "'");
    st.clearParameters();
    rs= st.executeUpdate();
            if(rs != 0) {
                res.sendRedirect("carRental.jsp");
                return;
            }else {

            }
    }
    catch(Exception e) {
        e.printStackTrace();
        }
    finally {
        try{ if(st != null ) st.close(); } catch(java.sql.SQLException e){}
        try{ if(conn != null ) conn.close(); } catch(java.sql.SQLException e){}

    }
}

Upvotes: 0

Views: 32

Answers (2)

sagi
sagi

Reputation: 40491

Strings should be wrapped with quotes :

"update cardetails SET Availability = 'Unavailable' where id='"+ carid+ "'"

Upvotes: 0

adkstar
adkstar

Reputation: 178

Change the sql query line to

 st = conn.prepareStatement("update cardetails SET Availability = 'Unavailable' where id='"+ carid+ "'");

Upvotes: 1

Related Questions