Advertisement
Guest User

Untitled

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