Advertisement
Guest User

Untitled

a guest
Jun 13th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. <input type="text" name="login" size="45" />
  2.  
  3. <input type="password" name="password" size="45" />
  4.  
  5. <html>
  6. <head>
  7. <meta charset="utf-8">
  8. <link rel="stylesheet" type="text/css" href="style.css">
  9. </head>
  10. <body id="body">
  11. <form method="post" action="TheFirstServlet">
  12. <input name="login">
  13. <input name="password">
  14. <button>Записать</button>
  15. </form>
  16. </body>
  17. </html>
  18.  
  19. public class FirstServlet extends HttpServlet {
  20.  
  21. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  22. throws ServletException, IOException {
  23.  
  24. }
  25.  
  26. @Override
  27. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  28. throws ServletException, IOException {
  29. processRequest(request, response);
  30. }
  31.  
  32. @Override
  33. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  34. throws ServletException, IOException {
  35. response.setContentType("text/html;charset=UTF-8");
  36. request.setCharacterEncoding("UTF-8");
  37. String login = request.getParameter("login");
  38. String password = request.getParameter("password");
  39. insert(login, password);
  40. PrintWriter printWriter;
  41. try {
  42. printWriter = response.getWriter();
  43. printWriter.println("Ну, попытались");
  44. } catch(IOException exc) {}
  45. }
  46.  
  47. public void insert(String login, String password) {
  48. Connection conn = null;
  49. Statement stmt = null;
  50. try {
  51. Class.forName("com.mysql.jdbc.Driver");
  52. conn = DriverManager.getConnection("jdbc.url", "jdbc.login", "jdbc.password");
  53. stmt = conn.createStatement();
  54. String sql = "INSERT INTO NAME_TABLE (`login`,`password`)" +
  55. "VALUES ('" + login + "','" + password + "')";
  56. stmt.executeUpdate(sql);
  57. } catch (SQLException | ClassNotFoundException exc) {
  58. exc.printStackTrace();
  59. System.out.println("Не записал");
  60. }
  61. finally {
  62. try {
  63. if(conn!=null) conn.close();
  64. if(stmt!=null) stmt.close();
  65. } catch (SQLException exc) {}
  66. }
  67. }
  68.  
  69. /STEP 1. Import required packages
  70. import java.sql.*;
  71.  
  72. public class JDBCExample {
  73. // JDBC driver name and database URL
  74. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  75. static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
  76.  
  77. // Database credentials
  78. static final String USER = "username";
  79. static final String PASS = "password";
  80.  
  81. public static void main(String[] args) {
  82. Connection conn = null;
  83. Statement stmt = null;
  84. try{
  85. //STEP 2: Register JDBC driver
  86. Class.forName("com.mysql.jdbc.Driver");
  87.  
  88. //STEP 3: Open a connection
  89. System.out.println("Connecting to a selected database...");
  90. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  91. System.out.println("Connected database successfully...");
  92.  
  93. //STEP 4: Execute a query
  94. System.out.println("Inserting records into the table...");
  95. stmt = conn.createStatement();
  96.  
  97. String sql = "INSERT INTO Registration " +
  98. "VALUES (100, 'Zara', 'Ali', 18)";
  99. stmt.executeUpdate(sql);
  100. sql = "INSERT INTO Registration " +
  101. "VALUES (101, 'Mahnaz', 'Fatma', 25)";
  102. stmt.executeUpdate(sql);
  103. sql = "INSERT INTO Registration " +
  104. "VALUES (102, 'Zaid', 'Khan', 30)";
  105. stmt.executeUpdate(sql);
  106. sql = "INSERT INTO Registration " +
  107. "VALUES(103, 'Sumit', 'Mittal', 28)";
  108. stmt.executeUpdate(sql);
  109. System.out.println("Inserted records into the table...");
  110.  
  111. }catch(SQLException se){
  112. //Handle errors for JDBC
  113. se.printStackTrace();
  114. }catch(Exception e){
  115. //Handle errors for Class.forName
  116. e.printStackTrace();
  117. }finally{
  118. //finally block used to close resources
  119. try{
  120. if(stmt!=null)
  121. conn.close();
  122. }catch(SQLException se){
  123. }// do nothing
  124. try{
  125. if(conn!=null)
  126. conn.close();
  127. }catch(SQLException se){
  128. se.printStackTrace();
  129. }//end finally try
  130. }//end try
  131. System.out.println("Goodbye!");
  132. }//end main
  133. }//end JDBCExample
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement