Zubair
Zubair

Reputation: 23

error in insert statment in jdbc

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

Upvotes: 0

Views: 69

Answers (3)

Aditya
Aditya

Reputation: 2311

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

Upvotes: 0

Linga
Linga

Reputation: 10573

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();

Upvotes: 2

Lijo
Lijo

Reputation: 6788

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..

Upvotes: 0

Related Questions