Guest User

Untitled

a guest
Jan 10th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. <%@page language="java" contentType="text/html; charset=ISO-8859-2" pageEncoding="UTF-8"  %>
  2. <%@page import="java.sql.*"%>
  3.  
  4. <%
  5. String driverName = "com.mysql.jdbc.Driver";
  6. String URL = "jdbc:mysql://localhost:3306/Blog";
  7. String USERNAME = "root";
  8. String PASSWORD = "password";
  9.  
  10. try{
  11.     Class.forName(driverName); 
  12. }catch(ClassNotFoundException e){
  13.     e.printStackTrace();
  14. }
  15.  
  16. Connection connection = null;
  17. Statement statement = null;
  18. ResultSet resultSet = null;
  19. %>
  20.  
  21. <html>
  22.     <head>
  23.    
  24.         <title>Dodawanie komentarza</title>
  25.    
  26.         <style>
  27.        
  28.             .container0{
  29.                 margin-top:20px;
  30.                 width:190px;
  31.                 height:200px;
  32.             }
  33.            
  34.             .container1{
  35.                 margin-top:10px;
  36.                 margin-left:90px;
  37.                 width:190px;
  38.                 height:20px;
  39.             }
  40.            
  41.             .container2{
  42.                 margin-top:10px;
  43.                 margin-left:90px;
  44.                 width:190px;
  45.                 height:200px;
  46.             }
  47.        
  48.             .input{
  49.                 type:text;
  50.                 size:50x;
  51.                 width:200px;
  52.             }
  53.             .button1{
  54.                 width:100px;
  55.             }
  56.        
  57.         </style>
  58.        
  59.     </head>
  60.     <body>
  61.    
  62.         <div class="container0">
  63.        
  64.             <form name="form0" action="insert.jsp" method="post">  
  65.                
  66.                 <table align="center">
  67.                     <tbody>
  68.                         <tr>
  69.                             <td>Id: </td>
  70.                             <td><input class="input" name="Id" value="" /></td>
  71.                         </tr>
  72.                         <tr>
  73.                             <td>Imię: </td>
  74.                             <td><input class="input" name="imie" value="" /></td>
  75.                         </tr>
  76.                         <tr>
  77.                             <td>Nazwisko: </td>
  78.                             <td><input class="input" name="nazwisko" value="" /></td>
  79.                         </tr>
  80.                         <tr>
  81.                             <td>Komentarz: </td>
  82.                             <td><input class="input" name="komentarz" value="" /></td>
  83.                         </tr>
  84.                        
  85.                     </tbody>
  86.                 </table>
  87.                
  88.                 <div class="container1">
  89.                     <input class="button1" type="submit" value="Zatwierdź" name="Zatwierdź" />
  90.                 </div>
  91.                
  92.             </form>
  93.            
  94.             <div class="container2">
  95.            
  96.                 <form action="index.jsp">
  97.                     <button class="button1">Wróc</button>
  98.                 </form>
  99.            
  100.             </div>
  101.            
  102.         </div>
  103.        
  104.     </body>
  105. </html>
  106.  
  107. <%
  108. String Id = request.getParameter("Id");
  109. String imie = request.getParameter("imie");
  110. String nazwisko = request.getParameter("nazwisko");
  111. String komentarz = request.getParameter("komentarz");
  112.        
  113. connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  114. statement = connection.createStatement();
  115. String sql = "INSERT INTO komentarz (Id, imie, nazwisko, komentarz) VALUES ('"+Id+"','"+imie+"','"+nazwisko+"','"+komentarz+"')";
  116.        
  117. try{
  118.     int action = connection.createStatement().executeUpdate(sql);
  119.     connection.setAutoCommit(true);
  120.    
  121.     if(action>=1){
  122.         out.println("Zapisano");
  123.     }
  124. }catch(Exception e){
  125.     e.printStackTrace();
  126. }
  127. %>
Add Comment
Please, Sign In to add comment