Guest User

Untitled

a guest
Sep 7th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. <------------------------------------------------------JSP File----------------------------------------------------------------------->
  2. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  3. <%@page import="pkg.*" %>
  4. <%@page import="java.sql.*" %>
  5. <%@page import="java.io.*" %>
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title> Page</title>
  10. </head>
  11. <body>
  12. <jsp:useBean id="iClass" class="Insert_Class" scope="page"></jsp:useBean>
  13. <jsp:setProperty name="iClass" property="name" param="name"></jsp:setProperty>
  14. <jsp:setProperty name="iClass" property="salary" param="salary"></jsp:setProperty>
  15. <jsp:useBean id="DAO1" class="DAO" scope="page" ></jsp:useBean>
  16. <%
  17. DAO1.savePerson(iClass);
  18. %>
  19. </body>
  20. </html>
  21. <--------------------------------------------------------Database Class------------------------------------------------------------>
  22.  
  23. package pkg;
  24. import java.io.Serializable;
  25. import java.sql.*;
  26. public class DAO implements Serializable {
  27. Connection con=null;
  28. public void DAO()
  29. {
  30. establishConnection();
  31. }
  32. public void establishConnection()
  33. {
  34.  
  35. try{
  36. Class.forName("com.msql.jdbc.Driver");
  37. String url="jdbc:mysql://localhost:3306/name_salary?zeroDateTimeBehavior=convertToNull";
  38. con=DriverManager.getConnection(url);
  39.  
  40. }catch(Exception e){System.out.println(e);}
  41.  
  42. }
  43. public void savePerson(Insert_Class obj)
  44. { try{
  45. String sql="INSERT INTO record1(name,salary)VALUES(?,?)";
  46. PreparedStatement pst=con.prepareStatement(sql);
  47. String name=obj.getName();
  48. int salary=(int)obj.getSalary();
  49. pst.setString(1, name);
  50. pst.setInt(2, salary);
  51. pst.executeUpdate();
  52. }catch(Exception e)
  53. {
  54. System.out.println(e);
  55. }
  56. }
  57.  
  58. }
  59. <--------------------------------------------------------JAVA Bean CLass-------------------------------------------------------------->
  60. package pkg;
  61.  
  62. import java.io.Serializable;
  63.  
  64. public class Insert_Class implements Serializable{
  65. private String name;
  66. private int salary;
  67. public Insert_Class()
  68. {
  69. name="";
  70. salary=0;
  71. }
  72. public void setName(String name)
  73. {
  74. this.name=name;
  75. }
  76. public void setSalary(int salary)
  77. {
  78. this.salary=salary;
  79. }
  80. public String getName()
  81. {
  82. return name;
  83. }
  84. public int getSalary()
  85. {
  86. return salary;
  87. }
  88.  
  89. }
Add Comment
Please, Sign In to add comment