Advertisement
Guest User

Untitled

a guest
Feb 16th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #public class Pojo {
  2. String fnm;
  3. String lnm;
  4. InputStream b;
  5. public String getFnm() {
  6. return fnm;
  7. }
  8. public void setFnm(String fnm) {
  9. this.fnm = fnm;
  10. }
  11. public String getLnm() {
  12. return lnm;
  13. }
  14. public void setLnm(String lnm) {
  15. this.lnm = lnm;
  16. }
  17. public InputStream getB() {
  18. return b;
  19. }
  20. public void setB(InputStream b) {
  21. this.b = b;
  22. }
  23.  
  24. }
  25.  
  26. public class BusinessLogic {
  27. public boolean insert(Pojo bean)
  28. {
  29. boolean check_connection=false;
  30.  
  31. try
  32. {
  33. System.out.println("inside Registration");
  34. Connection conn= (Connection) DBConnection.connect();
  35.  
  36. String sql = "INSERT INTO image1 (first_name, last_name, photo) values (?, ?, ?)";
  37. PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
  38. ps.setString(1,bean.getFnm());
  39. ps.setString(2,bean.getLnm());
  40. ps.setBlob(3, bean.getB());
  41. ps.executeUpdate();
  42. check_connection =true;
  43. conn.close();
  44. }
  45. catch(Exception e)
  46. {
  47. System.out.println(""+e);
  48. }
  49. return check_connection;
  50. }
  51. }
  52.  
  53. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  54. String firstName = request.getParameter("firstName");
  55. String lastName = request.getParameter("lastName");
  56. InputStream inputStream = null; // input stream of the upload file
  57. Part filePart = request.getPart("photo");
  58. if (filePart != null) {
  59. // prints out some information for debugging
  60. System.out.println(filePart.getName());
  61. System.out.println(filePart.getSize());
  62. System.out.println(filePart.getContentType());
  63.  
  64. // obtains input stream of the upload file
  65. inputStream = filePart.getInputStream();
  66. }
  67. Pojo p=new Pojo();
  68. p.setFnm(firstName);
  69. p.setLnm(lastName);
  70. if (inputStream != null) {
  71. // fetches input stream of the upload file for the blob column
  72. p.setB(inputStream);
  73. // statement.setBlob(3, inputStream);
  74. }
  75. BusinessLogic bl=new BusinessLogic();
  76. boolean b=bl.insert(p);
  77. if(b)
  78. {
  79. System.out.println("success");
  80. }
  81. else {
  82. System.out.println("success");
  83. }
  84. }
  85.  
  86. public class DBConnection {
  87.  
  88. public static Connection con;
  89. public static Connection connect()
  90. {
  91. try
  92. {
  93. Class.forName("com.mysql.jdbc.Driver");
  94. con=DriverManager.getConnection("jdbc:mysql://localhost:3306/img","root","");
  95. System.out.println("connected!!");
  96. }
  97. catch(Exception e)
  98. {
  99. System.out.println(e);
  100. }
  101. return con;
  102. }
  103. public static void close() throws SQLException
  104. {
  105. if(con!=null)
  106. {
  107. con.close();
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement