• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending data using prepared statement to database(*oracle)

 
tuhin mitra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try
{
System.out.println("a");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
System.out.println("b");

System.out.println("c");
PreparedStatement st=con.prepareStatement("insert into patients(name,age,gender,disease,appointment_no,doctors_name,address,contact_no) values(?,?,?,?,seq1.nextval,?,?,?)");
System.out.println("b");
st.setString(1,name3);
st.setString(2,age3);
st.setString(3,gen3);
st.setString(4,dis3);
/* st.setString(5,app3);*/
st.setString(6,doc3);
System.out.println(con3);
st.setString(7,add3);
st.setString(8,con3);

st.executeUpdate();

JOptionPane.showMessageDialog(null, "Data is successfully inserted into database");

setVisible(true);
con.close();
}
catch(Exception er){
System.out.println("error");
JOptionPane.showMessageDialog(null, "error in submitting data");
}
}
});




i want to update one of the column acc to sequence .....but it is throwing exception..cant locate the error..
 
Paul Clapham
Sheriff
Posts: 21975
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tuhin, welcome to the Ranch!

Your PreparedStatement has 7 parameters, but your code fails to set parameter number 5 and then tries to set parameter number 8. At which point it fails. You said

tuhin mitra wrote:cant locate the error


But you would have been able to locate it if you had put



in your catch block. That would print a stack trace which tells you not only the actual error thrown but also the line of code which threw it. I'm sure you'll agree that is much more useful information than just the word "error".

 
tuhin mitra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help Paul....
Easily located the error and rectified....
Thanks !!!
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!