-3
desc = "Fishing Hooks";
code = "FH";
size = "#2";
cost = 2;
qnty = Integer.parseInt(hook2amount.getText());
amnt = qnty * cost;
Products h2 = new Products(desc,code,size,cost,qnty,amnt);
list.add(h2);
try {
    Class.forName("com.mysql.jdbc.Driver");

    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
    pst = con.prepareStatement("insert into purchase(username,code,desc,size,cost,quantity,date) values(?,?,?,?,?,?,?)");
    pst.setString(1, user);
    pst.setString(2, code);
    pst.setString(3,desc);
    pst.setString(4,size);
    pst.setString(5, String.valueOf(cost));
    pst.setString(6, String.valueOf(qnty));
    pst.setString(7, date);
    pst.executeUpdate();

} catch (Exception e) {
    System.out.println("Error " + e);
}

I'm getting a syntax error saying

Error java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'desc,size,cost,quantity,date) values('sam','FH','Fishing Hooks','#2','2','2','7/' at line 1

I cant find my error

Blue
  • 22,608
  • 7
  • 62
  • 92
Samasha
  • 369
  • 1
  • 5
  • 16

2 Answers2

1

desc is a reserved keyword in MariaDB, put some backticks around it and it should work.

Julian
  • 837
  • 1
  • 11
  • 24
  • by solving that error i got another error saying Error com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect date value: '7/28/2018' for column 'date' at row 1 – Samasha Jul 28 '18 at 09:47
0

The insert statement itself is

insert into purchase(username,code,desc,size,cost,quantity,date) values(?,?,?,?,?,?,?)

Notice that desc is a reserved keyword.

You need to escape it or change its name. It is good practice to escape all column names anyway, so change the insert statement to:

insert into purchase(`username`,`code`,`desc`,`size`,`cost`,`quantity`,`date`) values (?,?,?,?,?,?,?)

You should also set the parameters with the methods that correspond with their data types. Instead of setString everywhere, use setInt for the integer values cost and qnty, given your code.

geco17
  • 5,152
  • 3
  • 21
  • 38