0

I'm trying to insert in dabase with following code

try{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:ProductDSN";
Class.forName(driver);
Connection con = DriverManager.getConnection(url);
System.out.println("Connection established");
System.out.println("clear point 1");
String sql  = "INSERT INTO product
(id,productName,category,price,availability,quantity,description)
VALUES('ID','name','catag','price','avail','quantity','discript')";
System.out.println("clear point 2");
PreparedStatement stmt = con.prepareStatement(sql);
System.out.println("clear point 3");
stmt.executeUpdate();
System.out.println("clear point 4");

JOptionPane.showMessageDialog(null, "Record Save Successfully");

if (con != null)
  stmt.close();
con.close();
System.out.println("Connection Closed");
}
catch(SQLException sqle)
{
  System.err.println("Error is in save method");
  System.err.println(sqle);
}

and following are errors in compiling..

Connection established
clear poitn 1
clear point 2
clear point 3
Error is in save method
java.sql.SQLException: General error
7
  • 2
    all the datatypes of table are varchar only? Commented Dec 9, 2013 at 6:05
  • 2
    please provide a stacktrace Commented Dec 9, 2013 at 6:08
  • 2
    Please check the datatype of id,price and quantity whether they are integer or varchar Commented Dec 9, 2013 at 6:11
  • Can you please provide the datatypes of the columns. Commented Dec 9, 2013 at 6:13
  • Post your database table structure Commented Dec 9, 2013 at 6:16

3 Answers 3

2

You are confused with the Prepared statement.

The setter methods (setShort, setString, and so on) for setting IN parameter values must specify types that are compatible with the defined SQL type of the input parameter. For instance, if the IN parameter has SQL type INTEGER, then the method setInt should be used.

So please try

String sql  = "INSERT INTO product
(id,productName,category,price,availability,quantity,description)
VALUES(?,?,?,?,?,?,?)";

and

stmt.setInt(1, value)// for Integer where 1 is the index
stmt.setString(2, value)// for string where 2 is the index
.....

and finally,

stmt.executeUpdate();
Sign up to request clarification or add additional context in comments.

Comments

0

sql exception means error is int SQL query. Check your sql query separately on the query browser. Check whether you have given all attributes datatype as String.. you have given the values to insert as Strings..price,id etc..

check if it is in the same string format in the db also.. plz provide the error display so i can help you more..

Comments

0

Check out for Prepared Statement format & how to use it.

Comments

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.