Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. package servlets;
  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.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  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 DodajOsoby extends HttpServlet {
  17. private static final long serialVersionUID = 1L;
  18. private final String dbURL = "jdbc:derby://localhost:1527/nazwabazy;create=true;user=test;password=test";
  19. private final String tableName = "osoby";
  20. // jdbc Connection
  21.  
  22. private Connection conn = null;
  23. private Statement stmt = null;
  24.  
  25. @Override
  26. public void init() throws ServletException {
  27. super.init();
  28. connectDB();
  29. createTable();
  30. shutdown();
  31. }
  32.  
  33. private void shutdown() {
  34. try {
  35. if (stmt != null) {
  36. stmt.close();
  37. }
  38. if (conn != null) {
  39. DriverManager.getConnection(dbURL + ";shutdown=true");
  40. conn.close();
  41. }
  42. } catch (SQLException sqlExcept) {}
  43. }
  44.  
  45. private void connectDB() {
  46. try {
  47. Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
  48. //Get a connection
  49. conn = DriverManager.getConnection(dbURL);
  50. } catch (Exception except) {
  51. except.printStackTrace();
  52. }
  53. }
  54.  
  55. private void createTable() {
  56. try {
  57. stmt = conn.createStatement();
  58. stmt.execute("create table "+ tableName +"(id int primary key, imie varchar(20), nazwisko varchar(30) )");
  59. stmt.close();
  60. } catch (SQLException sqlExcept) { }
  61. }
  62.  
  63. private void insertOsoba(int id, String imie, String nazwisko) {
  64. try {
  65. stmt = conn.createStatement();
  66. stmt.execute("insert into " + tableName + " values (" +
  67. id + ",'" + imie + "','" + nazwisko +"')");
  68. stmt.close();
  69. } catch (SQLException sqlExcept) { }
  70. }
  71. private void printData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  72. response.setContentType("text/html; charset=UTF-8");
  73. PrintWriter out = response.getWriter();
  74. out.println("<html>");
  75. out.println("<head>");
  76. out.println("<title>Pomyslnie dodano!</title>");
  77. out.println("</head>");
  78. out.println("<body>");
  79. out.println("<h1>Dodano!!!</h1>");
  80. out.println("<tr>");
  81.  
  82. connectDB();
  83. int id3=0;
  84. try {
  85. stmt = conn.createStatement();
  86.  
  87. String firstname = request.getParameter("firstname");
  88. String lastname = request.getParameter("lastname");
  89. ResultSet results = stmt.executeQuery("select id from " + tableName);
  90. while(results.next()) {
  91. id3 = results.getInt(1);
  92.  
  93.  
  94. id3=id3+1;
  95. insertOsoba(id3, firstname, lastname);
  96. shutdown();}
  97. stmt.close();
  98. } catch (SQLException e) {
  99. e.printStackTrace();
  100. }
  101. shutdown();
  102. out.println("</tr>");
  103. out.println("</table>");
  104. out.println("</body>");
  105. out.println("</html>");
  106. }
  107.  
  108. @Override
  109. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  110. printData(request, response);
  111. }
  112.  
  113. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  114. printData(request, response);
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement