Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PreparedStatement, how can i skip a column?

 
carlos flamenco
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to insert values into a table that has four columns, I only want to update columns one and three though! Sometimes I will be doing an update instead and will update columns 2,3 and 4. My problem is that I keepp getting requested to update the second column, when in fact i want the value there to remain the same.Here is a snippet of my code anyway, any help would be greatly appreciated!
Statement stmt = conn.createStatement();
PreparedStatement insertuser = conn.prepareStatement("INSERT INTO TEST.THE_USER VALUES(?,?)");
insertuser.clearParameters();
insertuser.setString(1,userid);
insertuser.setString(3,last_update);
insertuser.executeUpdate();
 
Anusaya Sehgal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by carlos flamenco:
Hi,
I want to insert values into a table that has four columns, I only want to update columns one and three though! Sometimes I will be doing an update instead and will update columns 2,3 and 4. My problem is that I keepp getting requested to update the second column, when in fact i want the value there to remain the same.Here is a snippet of my code anyway, any help would be greatly appreciated!
Statement stmt = conn.createStatement();
PreparedStatement insertuser = conn.prepareStatement("INSERT INTO TEST.THE_USER VALUES(?,?)");
insertuser.clearParameters();
insertuser.setString(1,userid);
insertuser.setString(3,last_update);
insertuser.executeUpdate();


If I understand ur problem then the solutions is
PreparedStatement insertuser= con.prepareStatement("INSERT INTO TEST.THE_USER(userid,last_update) VALUES(?,?)");
insertuser.clearParameters();
insertuser.setString(1,userid);
insertuser.setString(2,last_update);
insertuser.executeUpdate();
i hope this helps u

[This message has been edited by Anusaya Sehgal (edited November 09, 2001).]
 
ayman ms
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose i have two column named "name", "email"
then i can use this code to insert values to the database containing table( std_tb)
with two column


if i want to insert second column only then i can use this code to same table containing two column

but don't forget to set first column a null value , if you are using wamp or xampp then ,you can do this by going "http://localhost/phpmyadmin/" then access to your table then put tick mark on null
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic