Kevin G
Kevin G

Reputation: 119

How do I solve a MySQL syntax error exception?

I am trying to insert data into my jtable from wampserver mysql database. I keep on getting this specific error com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Erreur de syntaxe près de ')' à la ligne 1.

Here is my code

public Boolean add( String Registration,String Departure, String capacity, String Date, String Destination, String Startpoint) {
    //SQL STATEMENT
    String sql = "INSERT INTO travel_schedule( Registrationno,Capacity, Date, Destination,Departuretime,Startpoint) VALUES('"+Registration+"', '"+ capacity + "','" + Date + "','" + Destination + "','" + Departure + "','" + Startpoint + "',)";
    try {
        //GET CONNECTION
        Connection con = DriverManager.getConnection(conString, username, password);

        //PREPARED STATEMENT
        Statement s = con.prepareStatement(sql);
        s.execute(sql);
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

Upvotes: 1

Views: 1327

Answers (2)

Balayesu Chilakalapudi
Balayesu Chilakalapudi

Reputation: 1406

In your sql query, Remove space after comma here Registration+"', '"+ and write it as Registration+"','"+ also Remove comma here Startpoint + "',)";

use the below statement,

 String sql = "INSERT INTO travel_schedule(Registrationno,Capacity,Date,Destination,Departuretime,Startpoint) VALUES('"+Registration+"','"+capacity+"','"+Date+"','"+ Destination +"','"+Departure+"','" +Startpoint+"')";

Upvotes: 1

Sajan Chandran
Sajan Chandran

Reputation: 11487

Fix the extra ', you have to remove both the comma and '

 String sql = "INSERT INTO travel_schedule( Registrationno,Capacity, Date, Destination,Departuretime,Startpoint) VALUES("+ Registration +"','"+ capacity + "','" + Date + "','" + Destination + "','" + Departure
+ "','" + Startpoint + ")";

Upvotes: 0

Related Questions