Thursday, March 5, 2015

java Mysql image insert 2 way and DB coloumn datatype (img)

**nb**/for  insert Image,date and timestamp must be use prepared statement not other way
 Date bookingDate = DateFormate.getFormattedDate(insetStartTimeFullcalendar, "dd/MM/yyyy");
            Date startTime = DateFormate.getFormattedDate(soneDateTime, "dd/MM/yyyy HH:mm");
            Date endTime = DateFormate.getFormattedDate(stwoDateTime, "dd/MM/yyyy HH:mm");

            pStmt.setDate(4, new java.sql.Date(bookingDate.getTime()));
            pStmt.setTimestamp(5, new java.sql.Timestamp(startTime.getTime()));
            pStmt.setTimestamp(6, new java.sql.Timestamp(endTime.getTime()));

java Mysql image insert 2 way :
1.Direct form submit
2.Form submit by jquery

N.B:  Database img coloumn data type is blob  support  for oracle

But 

if   Database img coloumn data type is blob use for mySql then show error
here :   img coloumn data type is longblob

1.Direct form submit :
jsp page  :
<form id="myForm" action="insertMeetingRoomCloseRI.do" class="form-horizontal" method="post" enctype="multipart/form-data">
                      <div class="modal-body">
                       
                             
                      <!-- START PAGE CONTENT-->   
                          <!-- BEGIN EXAMPLE TABLE widget-->
                <div class="widget red">
                    <div class="widget-title">
                        <!--h4><i class="icon-reorder"></i> Data Table</h4-->
                            <!--span class="tools">
                                <a href="javascript:;" class="icon-chevron-down"></a>
                                <a href="javascript:;" class="icon-remove"></a>
                            </span-->
                    </div>
                    <div class="widget-body">
                   
                    <div>
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td width="31%" style="text-align:right;font-family:Arial;font-size:12px;font-weight:bold;color:#000;">Search : </td>
                                <td width="17%"><input type="text" class="form-control" id="usr"></td>
                                <td width="7%"> <button type="submit" class="btn ">SEARCH</button></td>
                                <td width="45%">&nbsp;</td>
                              </tr>
                        </table>
                    </div>
                    <div style="padding-top:5px;">
                        <table width="100%" class="" id="">
                          <tr>
                              <td style="text-align:right;">Room Type :</td>
                              <td>
      <select id="roomtypeAddmroom" name="roomtypeAddmroom" class="span3 " tabindex="1" data-placeholder="Choose a Category" onchange="roomTypesPARENTIDneed(this);">
            <option value="">Select</option>
            <s:iterator value="roomTypeList"  status="rowstatus">
                        <option value="<s:property value='lookup_id'/>"><s:property value='dtl_name'/></option>  
            </s:iterator>
      </select>     </td>
                              <td>&nbsp;</td>
                              <td style="text-align:right;">&nbsp;</td>
                          </tr>
                          <tr>
                            <td width="35%" style="text-align:right;">UD Room Id :</td>
                            <td width="20%" style="text-align:left;"><input id="udefineAddmroom" name="udefineAddmroom" type="text" class="form-control" ></td>
                            <td width="9%" style="text-align:center;">&nbsp;</td>
                            <td width="36%" style="text-align:right;">&nbsp; </td>
                          </tr>
                           
                            <tr >
                              <td style="text-align:right;">Room Name : </td>
                              <td><input id="rnameAddmroom" name="rnameAddmroom" type="text" class="form-control" ></td>
                              <td>&nbsp;</td>
                              <td style="text-align:right;">&nbsp;</td>
                            </tr>
                            <tr >
                              <td style="text-align:right;">Parent Id :</td>
                              <td id="roomTypesPARENTIDneed_div">
       <select id="parentidAddmroom" name="parentidAddmroom" class="span3 " tabindex="1" data-placeholder="Choose a Category">
             <option value="">Select</option>
       </select>      </td>
                              <td>&nbsp;</td>
                              <td style="text-align:right;">&nbsp;</td>
                            </tr>

                            <tr>
                              <td style="text-align:right;">Size :</td>
                              <td><input id="sizeAddmroom" name="sizeAddmroom" type="text" class="form-control" ></td>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                              </tr>
                            <tr >
                              <td style="text-align:right;">Capacity : </td>
                              <td><input id="capacityAddmroom" name="capacityAddmroom" type="text" class="form-control" ></td>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                            </tr>
                            <tr >
                              <td style="text-align:right;">Device : </td>
                              <td><input id="deviceAddmroom" name="deviceAddmroom" type="text" class="form-control" ></td>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                            </tr>
                            <tr >
                              <td style="text-align:right;">Room Image </td>
                              <td colspan="3">
                                 <input id="userImage" name="userImage" class="" type="file" >    </td>
                              </tr>
                            <tr >
                              <td style="text-align:right;">Colour :</td>
                              <td><input id="colorAddmroom" name="colorAddmroom" type="text" class="form-control"></td>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                            </tr>
                            <tr >
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                            </tr>
                        </table>
                    </div>
                   
<!--a href="#" onclick="appCanPOPFunction();">second POPUP</i></a-->
                    </div>
                </div>
   <!-- END EXAMPLE TABLE widget-->

<!-- END PAGE CONTENT-->
                       
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <!-- <button type="submit" class="btn btn-primary" >Save changes</button> -->
                        <button type="submit" class="btn btn-primary">Save changes</button>
                      </div>
                     
         </form>


Struts configure :
 <action name="*RI" class="action.MeetingRoomAction" method="{1}">
       <interceptor-ref name="fileUpload">
            <param name="maximumSize">2097152</param>
            <param name="allowedTypes">
                image/png,image/gif,image/jpeg,image/pjpeg
            </param>
            <param name="contentType">text/html</param>
        </interceptor-ref>
        <interceptor-ref name="defaultStack"></interceptor-ref>           
          <result name="insertMeetingRoomClose">ManageOrSetupPage/meetingRoom.jsp</result>
   </action>




MeetingRoomAction.java :
public class MeetingRoomAction extends ActionSupport implements ServletRequestAware{
public String insertMeetingRoomClose(){
        MeetingRoomSQL meetingRoomSQL = new MeetingRoomSQL();
        System.out.println("Meeting Room info and image save");
       

            try {
               
                String filePath = servletRequest.getRealPath("/");
               
                System.out.println("Server path:" + filePath);
                File fileToCreate = new File(filePath, this.userImageFileName);
                FileUtils.copyFile(this.userImage, fileToCreate);
                filePath += userImageFileName;
                File image = new File(filePath);
                System.out.println(image);
                FileInputStream   fis = new FileInputStream(image);
                meetingRoomSQL.insertMeetingRoomCloseData(udefineAddmroom,rnameAddmroom,parentidAddmroom,roomtypeAddmroom,sizeAddmroom,capacityAddmroom,deviceAddmroom,image,fis);

            //    meetingRoomSQL.insertMeetingRoomCloseData(udefineAddmroom,rnameAddmroom,parentidAddmroom,roomtypeAddmroom,sizeAddmroom,capacityAddmroom,deviceAddmroom,null,null);
            } catch (Exception e) { //catch (IOException e)
                e.printStackTrace();
               
            }
           
            meetingRoomList = meetingRoomSQL.selectMeetingRoomData();
       
        return "insertMeetingRoomClose";       
    }




image code from above:
/*
    System.out.println("Server path:" + filePath);
                File fileToCreate = new File(filePath, this.userImageFileName);
                FileUtils.copyFile(this.userImage, fileToCreate);
                filePath += userImageFileName;
                File image = new File(filePath);
                System.out.println(image);
                FileInputStream   fis = new FileInputStream(image);
*/


//variable part
    private File userImage;
    private String userImageContentType;
    private String userImageFileName;
    private HttpServletRequest servletRequest;

//set get part


    public File getUserImage() {
        return userImage;
    }
    public void setUserImage(File userImage) {
        this.userImage = userImage;
    }


    public String getUserImageContentType() {
        return userImageContentType;
    }
    public void setUserImageContentType(String userImageContentType) {
        this.userImageContentType = userImageContentType;
    }


    public String getUserImageFileName() {
        return userImageFileName;
    }
    public void setUserImageFileName(String userImageFileName) {
        this.userImageFileName = userImageFileName;
    }




    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
       
    }



}


MeetingRoomSQL.java :
 //FROM KYAMC image insert Prepared Statement
  //***********************  Start user registration
      // public void insertMeetingRoomCloseData(String udefineAddmroom,String rnameAddmroom,String parentidAddmroom,String roomtypeAddmroom,String sizeAddmroom,String capacityAddmroom,String deviceAddmroom,File image,FileInputStream fis){
             public void insertMeetingRoomCloseData(String udefineAddmroom,String rnameAddmroom,String parentidAddmroom,String roomtypeAddmroom,String sizeAddmroom,String capacityAddmroom,String deviceAddmroom,File image,FileInputStream fis){
   
          int userid = (Integer) ActionContext.getContext().getSession().get("userId");
        int orgid = (Integer)ActionContext.getContext().getSession().get("orgid");  
         //String collegeid;
            System.out.println("kaniz");
            boolean fg = true;
            con = dbc.connectDB();

             if (con == null) {
                 fg = false;
             }
          
        if (fg) {
         try { 

          /* String sql="call PRC_BUILD_PK(?, ?,?,?,?,?)";
        
                cs = con.prepareCall(sql);
                cs.setString(1, "USER");
                cs.setString(2, "SEQ_COLLEGEINFO_ID");
                cs.setString(3, "P");
                cs.setString(4, "1");
                cs.registerOutParameter(5, java.sql.Types.VARCHAR);
                cs.setString(6, "Y");
                
                cs.execute();
                collegeid = cs.getString(5);

                System.out.println(collegeid);*/
         
                st = con.createStatement();      
         
                //String fullName = firstName + lastName; // It is call concatenation
                //System.out.println(fullName);
              
         Date today = new Date();
      // query = "insert into kyamc_user_type(ID,USER_FULL_NAME,USER_ID,USER_EMAIL,GROUP_ID,USER_PASS,USER_MOBILE,GENDER,ENTRY_TIMESTAMP,COLLEGE_NO ) values (SEQ_COLLEGEINFO_ID.nextval,'"+fullName+"','"+userName+"','"+userEmail+"','"+roleIdA+"','"+userPassword+"','"+mobNumber+"','"+gender +"',sysdate,'1')";
      //  System.out.println("Query" + query);
           query = "insert into rbs_room(ud_room_id,room_name,parent_id,room_type,size,capacity,device,room_image,entered_by,entry_timestamp) values (?,?,?,?,?,?,?,?,?,?)";

        pre = con.prepareStatement(query);
        pre.setString(1, udefineAddmroom);
        pre.setString(2, rnameAddmroom);
        pre.setString(3, parentidAddmroom);
        pre.setString(4, roomtypeAddmroom);
        pre.setString(5, sizeAddmroom);
        pre.setString(6, capacityAddmroom);
        pre.setString(7, deviceAddmroom);
        pre.setBinaryStream(8, fis, (int) image.length());
        pre.setInt(9, userid);
        pre.setTimestamp(10, new java.sql.Timestamp(today.getTime()));//db coloumn name timestamp
     
        pre.execute();
        fis.close(); //use 4 image
       // st.execute(query);
   

        } catch (SQLException sq) {         
          sq.printStackTrace();
        } catch (Exception e) {
          e.printStackTrace();
      }finally {
          try {
              if (rs != null) {
                  rs.close();
                fis.close(); //use 4 image
              }
            fis.close();//use 4 image              con.close();
             
          } catch (Exception ex) {
              ex.printStackTrace();
          }
        }
        }
       
               // return "subBankInformation";// As void, so not return String
            }

     
      

No comments:

Post a Comment