Advertisement
Guest User

Untitled

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