This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes error in inserting string into database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "error in inserting string into database" Watch "error in inserting string into database" New topic
Author

error in inserting string into database

Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
Hello pals,
this is a code snippet in which I insert two strings into MSACESS .while
Im able to insert the string myname,Im not able to insert pc.Is there any problem with making more than one prepared statement on a connection object or is it some silly sql syntax error?
public void storePhone(String x,String y){
myname=x;pc=y;
try{
System.out.println("in storephone pc="+pc);

String sqlname="INSERT INTO PHONEDIARY(NAME)VALUES(?)";
ps=con.prepareStatement(sqlname);
ps.setString(1,myname);
ps.executeUpdate();


String sqlphone="INSERT INTO PHONEDIARY(PHONENUMBER)VALUES(?)";
ps2=con.prepareStatement(sqlphone);
ps2.setString(2,pc);

ps2.executeUpdate();

}
catch(Exception e){}
}
Thanks in advance for the help!
regs-Rajesh
Makarand Parab
Ranch Hand

Joined: Dec 10, 2004
Posts: 121
Hi Rajesh
Please post the exception, then only we can find the exact problem.

Regards
Makarand Parab
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Rajesh,
Some databases don't support having multiple statements open on a connection at the same time. In any case, you want to close your PreparedStatement object as soon as you are done with it to conserve resources. In this case, that would be immediately after the first update.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
Consider Paul's rocket mass heater.
 
subject: error in inserting string into database