Advertisement
Guest User

subota

a guest
Jan 18th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. package Servleti;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.SQLException;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15.  
  16. public class Unos extends HttpServlet
  17. {
  18. String ime;
  19. String prezime;
  20. String godStaza;
  21. int GodStaza;
  22.  
  23. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  24. throws ServletException, IOException
  25. {
  26. response.setContentType("text/html;charset=UTF-8");
  27.  
  28. provera(request,response);
  29. }
  30.  
  31. public void provera(HttpServletRequest request,HttpServletResponse response)
  32. throws ServletException, IOException {
  33. ime = request.getParameter("ime");
  34. prezime = request.getParameter("prezime");
  35. godStaza = request.getParameter("staz");
  36.  
  37. if(ime != null && ime.length() > 0 &&
  38. prezime != null && prezime.length() > 0 &&
  39. godStaza != null && godStaza.length() > 0) {
  40. try {
  41. GodStaza = Integer.parseInt(godStaza);
  42. upisiUBazu(request,response,GodStaza);
  43. }
  44. catch(Exception e) {
  45. request.setAttribute("msg", "Godine staza moraju biti ceo broj!" + e);
  46. request.getRequestDispatcher("Upis.jsp").forward(request, response);
  47. }
  48. }
  49. else {
  50. request.setAttribute("msg", "Sva polja moraju biti popunjena!");
  51. request.getRequestDispatcher("Upis.jsp").forward(request, response);
  52. }
  53. }
  54.  
  55. public void upisiUBazu(HttpServletRequest request,HttpServletResponse response,int GodStaza)
  56. throws ServletException, IOException {
  57. ime = request.getParameter("ime");
  58. prezime = request.getParameter("prezime");
  59. int godine = GodStaza;
  60.  
  61. try {
  62. Class.forName("com.mysql.jdbc.Driver");
  63. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/nastava","root","");
  64.  
  65. String upit = "INSERT INTO radnik(Ime,Prezime,GodineStaza)VALUES(?,?,?)";
  66. PreparedStatement ps = conn.prepareStatement(upit);
  67. ps.setString(1, ime);
  68. ps.setString(2, prezime);
  69. ps.setInt(3, godine);
  70. int izvrseno = 0;
  71.  
  72. izvrseno = ps.executeUpdate();
  73.  
  74. if(izvrseno > 0) {
  75. request.setAttribute("msg", "Uspesno ste upisali radnika u bazu!");
  76. request.getRequestDispatcher("index.jsp").forward(request, response);
  77. }
  78. else {
  79. request.setAttribute("msg", "Radnik nije upisan u bazu!");
  80. request.getRequestDispatcher("Upis.jsp");
  81. }
  82. }
  83. catch(SQLException sqle) {
  84. request.setAttribute("msg", "Greska u upitu!" + sqle);
  85. request.getRequestDispatcher("Upis.jsp");
  86. } catch (ClassNotFoundException ex)
  87. {
  88. request.setAttribute("msg", "Greska sa bazom!" + ex);
  89. request.getRequestDispatcher("Upis.jsp");
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement