Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. <%@page import="java.sql.*"%>
  2. <%@page import="java.util.Date"%>
  3. <% Class.forName("com.mysql.jdbc.Driver").newInstance(); %>
  4. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  5.  
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  10. <title>Add Fruit</title>
  11. </head>
  12. <body>
  13. <h1>Add Fruit</h1>
  14. <%!
  15. public class DB {
  16. String URL = "jbdc:mysql://localhost:3306/redula";
  17. String Username = "root";
  18. String Password = "";
  19.  
  20. Connection connection = null;
  21. PreparedStatement insertQuery = null;
  22. ResultSet resultSet = null;
  23.  
  24. public DB(){
  25. try {
  26. connection = DriverManager.getConnection(URL, Username, Password);
  27.  
  28. insertQuery = connection.prepareStatement("INSERT INTO fruits (Name) VALUES (?)");
  29.  
  30. } catch (SQLException e){
  31. e.printStackTrace();
  32. }
  33. }
  34.  
  35. public int setDB(String Name){
  36.  
  37. int result = 0;
  38.  
  39. try{
  40. insertQuery.setString(1, Name);
  41. result = insertQuery.executeUpdate();
  42. } catch (SQLException e){
  43. e.printStackTrace();
  44. }
  45. return result;
  46. }
  47. }
  48. %>
  49. <%
  50. int result = 0;
  51.  
  52. String Name = new String();
  53.  
  54. if(request.getParameter("Name") != null){
  55. Name = request.getParameter("Name");
  56. }
  57.  
  58. DB db = new DB();
  59. result = db.setDB(Name);
  60. %>
  61. <form name="fruitForm" action="index.jsp" method="POST">
  62. <input type="text" name="Name" value="" size="7" />
  63. <input type="submit" value="Submit" name="submit" />
  64. </form>
  65. </body>
  66. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement