2

I created a simple mysql database table using following query:

CREATE TABLE customer(
name varchar(20),
C_ID int NOT NULL AUTO_INCREMENT,
address  varchar(20),
email varchar(20),
PRIMARY KEY(C_ID)
);

Now I want to insert values to this table. My client like this:

package com.orderdata.ws;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;

import com.mysql.jdbc.Statement;

public class OrderData {
public static void main(String[] args)throws Exception{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/orderdata","root","chathura");
    Statement stmt = (Statement) con.createStatement();
    String insert = "INSERT INTO customer(name,C_ID,address,email)      VALUES (a,5,b,c)";
    stmt.executeUpdate(insert);

}

}

But this gives an exception "Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(a,5,b,c)' at line 1............"

How can I insert data using eclipse???

2 Answers 2

4

When inserting varchar text to MySQL tables, you need to surround it in single quotes like this:

String insert = "INSERT INTO customer(name,C_ID,address,email) VALUES ('a',5,'b','c');";
Sign up to request clarification or add additional context in comments.

Comments

1

Access databases need ";" at the end of sql command.

String insert = "INSERT INTO customer(name,C_ID,address,email) VALUES ('a',5,'b','c');";

Edit: And you need to put text data into query like this

VALUES ('a',5,'b','c')

You may need to "escape" ' (quote) characters. I dont know how to do this in java, maybe like this:

VALUES (\'a\',5,\'b\',\'c\')

1 Comment

Still have the same problem "Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(a,5,b,c)' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native M...

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.