-
June 2nd, 2017, 09:40 AM
#1
What is wrong with my Insert Method using PreparedStatement ?
Dear expert,
I would like to know if the below method and my Post servlet is there is any error cos no matter how I tried, the data just would not go into the database
Code:
public void insertSubject(subject s) throws MyDataException {
try {
openConnection();
connection.setAutoCommit(false);
String qry = INSERT_QRY1;
ps = connection.prepareStatement(qry);
List<subject>subjects = new LinkedList<subject>();
String[] subject = subjects.toArray(new String[subjects.size()]);
for( int i = 0 ; i < subject.length; i++ ) {
s.getSubjects();
ps.setObject(i + 1, subject[i]);
ps.executeUpdate();
connection.commit();
closeConnection();
}}
catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally{
finalclose();
}
}
// throw new MyDataException("DB Error");
}}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
tutor m4 = new tutor();
manager mgr = new manager();
try {
String name = request.getParameter("name");
if (name != null){
m4.setName(name);
String email = request.getParameter("email");
m4.setEmail(email);
String address = request.getParameter("address1");
m4.setAddress(address);
String contactNo = request.getParameter("contactNo");
m4.setContactNo(contactNo);
Integer age = Integer.parseInt(request.getParameter("age"));
m4.setAge(age);
String gender = request.getParameter("Gender");
m4.setGender(gender);
mgr.insertTutor(m4);
subject m1 = new subject();
//mgr.insertSubject(m1);
try{
List<subject>subjects = new LinkedList<subject>();
String[] subject = subjects.toArray(new String[subjects.size()]);
subject = request.getParameterValues("subject");
for (int i = 0; i< subjects.size(); i++){
m1.setSubjects1(subject[i]);
i++;
mgr.insertSubject(m1);
String result1 = subject[i];
out.println(result1 + " ");
out.println("Successful registration");
}}catch (Exception ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
Last edited by 2kaud; June 2nd, 2017 at 11:39 AM.
Reason: Added code tags
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|