hello,
i am using preparestatement to insert muliple rows into database.

String query = "insert into table_temp(col1,col2) values(?,?)";   
pstmt = con.prepareStatement(query);   
pstmt.setString(1,"a");   
pstmt.setInt(2,100);   
pstmt.addBatch();   
pstmt.setString(1,"b");   
pstmt.setInt(2,null);   
pstmt.addBatch();   
int[] cnt = pstmt.executeBatch();

My problem is that in the 2nd addBatch block i dont want to insert any value into the col2, which is a numeric field.
And i dont want to do it in a different query.
How to do that.
Please help

Recommended Answers

All 5 Replies

you can't store a null value into a primitive type.
you can do this with an Integer, but not with an int

Check the API for the PrepareStatement class. There is something called: setNull(int, int) . The first argument is the index of the '?' and the other the type of the column.

Assuming that col2 is type of NUMBER at your database table then you can do this:

pstmt.setNull(2, java.sql.Types.NUMERIC)

instead of this:

//pstmt.setInt(2,null);

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/PreparedStatement.html#setNull%28int,%20int%29

Check the API for the PrepareStatement class. There is something called: setNull(int, int) . The first argument is the index of the '?' and the other the type of the column.

Assuming that col2 is type of NUMBER at your database table then you can do this:

pstmt.setNull(2, java.sql.Types.NUMERIC)

instead of this:

//pstmt.setInt(2,null);

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/PreparedStatement.html#setNull%28int,%20int%29

Thanks a lot for your precious instructions
That is where i was missing the point

Hi,

i am developing a java application with mysql. i have design a form for input it contains 19 fields to enter and all the values will be stored into mysql. But if i enter any missing field it shows an error " Unable to save for input String"""
String sql = "insert into biodata (name, gender, dob, age, address, city, pincode, state, country, phone, mobile, email) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement stmt =
connection.prepareStatement(sql);

pls help me

Hi,

i am developing a java application with mysql. i have design a form for input it contains 19 fields to enter and all the values will be stored into mysql. But if i enter any missing field it shows an error " Unable to save for input String"""
String sql = "insert into biodata (name, gender, dob, age, address, city, pincode, state, country, phone, mobile, email) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement stmt =
connection.prepareStatement(sql);

pls help me

Start a new thread and post some code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.