1

my database table is not inserted with this information and I can't see where the error is, and if the insert is even successful or not. Please help!

        try{ 

             Class.forName("com.mysql.jdbc.Driver").newInstance(); 
             Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", ""); 
             PreparedStatement ps = con.prepareStatement("INSERT INTO payroll_system.employee_info(employeeID, FirstName, LastName, Admin,DOB,Address,Email,HourlyRate,Gender,ALeaveBalance,SLeaveBalance,ActiveStatus,Role,BSB,BankName,AccNumber,SuperNumber,SuperCompany) values(?,?,?,?,?,?,?,?,?,?,?,1,?,?,?,?,?,?)"); 
             ps.setString(1, employee_id);
             ps.setString(2, firstName);
             ps.setString(3, lastName);
             ps.setString(4, admin);
             ps.setString(5, DOB);
             ps.setString(6, address);
             ps.setString(7, email);
             ps.setString(8, HPR);
             ps.setString(9, gender);
             ps.setString(10, ALB);
             ps.setString(11, SLB);
             ps.setString(12, Role);
             ps.setString(13, BSB);
             ps.setString(14, BankName);
             ps.setString(15, BAN);
             ps.setString(16, SAC);
             ps.setString(17, SAN);
             ResultSet rs = ps.executeQuery(); 
             st=rs.next(); 
             if(st) { 
                 out.println("Account successfully created!");
                 RequestDispatcher rd = request.getRequestDispatcher("index.html"); 
                 rd.include(request, response);
             }
             else{ 
                 RequestDispatcher rd = request.getRequestDispatcher("changePassAdmin.html"); 
                 rd.include(request, response);
             }
         }catch(Exception e)
        {
            e.printStackTrace();
        }
      out.close();
Cœur
  • 37,241
  • 25
  • 195
  • 267
Programmer
  • 1,266
  • 5
  • 23
  • 44
  • are you getting any exceptions? Please post the stacktrace – WeMakeSoftware Jan 26 '16 at 08:21
  • @Funtik This is a web application. When I press the submit button in the html (which has form action to this servlet), it just shows a blank page with the url of this servlet. – Programmer Jan 26 '16 at 08:27

1 Answers1

0

executeQuery is only used for queries, you need to write

 ps.executeUpdate(); 
wero
  • 32,544
  • 3
  • 59
  • 84
  • Thanks, I'll try this out. – Programmer Jan 26 '16 at 08:25
  • I'm getting an error Type mismatch: cannot convert from int to ResultSet – Programmer Jan 26 '16 at 08:26
  • read the Javadoc. `executeUpdate` does not return a `ResultSet` (what should be in the result set ? - you are not executing a query) but the number of affected records as int. For your insert you want to get a 1. – wero Jan 26 '16 at 08:28
  • Thanks it works! I'll accept your answer, but can you please tell me how to print("Successful") if update was done successfully? I've updated my code above to show you the changes. – Programmer Jan 26 '16 at 08:40
  • `int count = ps.executeUpdate(); if (count == 1) { out.println("success"); ...` – wero Jan 26 '16 at 08:43
  • I made a rollback to your edit because it was hiding the original question and problem. You can always ask new questions... – wero Jan 26 '16 at 08:46
  • and if it wasn't a success then use else{ ? – Programmer Jan 26 '16 at 08:46
  • And in that case, what's the purpose of catch(Exception e)? – Programmer Jan 26 '16 at 08:47