Monday, March 30, 2015

mysql query write using join Not subQuery



mysql query write using join :

two query convert into one query using join



//1st query :
SELECT b.all_users_email,b.room_id FROM rbs_room_booking b WHERE b.booking_id = 277

SELECT b.all_users_email,b.room_id,  r.ud_room_id, r.room_name, r.room_type,r.capacity
FROM rbs_room_booking b

 JOIN rbs_room r ON r.room_id = b.room_id
WHERE booking_id = 277



//2nd query :
SELECT ud_room_id, room_name, room_type,capacity FROM rbs_room r WHERE room_id = 41




server Auto run when pc run





 Please go to start->all programs -> set-> here drag and drop short cut icon from desktop







sql in and out query Writing way ( mySql )


sql in  query Writing way :

 SELECT *FROM rbs_user

  SELECT e_mail FROM rbs_user WHERE user_id IN(1,2)//it is correct,it is auto increment id
  SELECT e_mail FROM rbs_user WHERE ud_user_id IN('gp101','gp102')


  SELECT e_mail FROM rbs_user WHERE ud_user_id IN(gp101,gp102) //Wrong way


 SELECT *FROM rbs_user




Sunday, March 29, 2015

mySql Date format


SELECT NOW();
SELECT CURDATE();
SELECT CURTIME();

SELECT  DATE_FORMAT(NOW(), '%d/%m/%Y') FROM DUAL
SELECT  DATE_FORMAT(CURDATE(), '%d/%m/%Y') FROM DUAL


or

SELECT  DATE_FORMAT(SYSDATE(), '%d/%m/%Y') FROM DUAL




                // String query = "SELECT booking_id,type_id,title,room_id,booking_date,start_time,end_time,description,attendy_people FROM rbs_room_booking where booking_id="+bookingid+"";
               //mySQL date format
                 String query = "SELECT booking_id,type_id,title,room_id,booking_date,DATE_FORMAT(start_time, '%d/%m/%Y')start_time,DATE_FORMAT(end_time, '%d/%m/%Y')end_time,description,attendy_people FROM rbs_room_booking where booking_id="+bookingid+"";



See the following Example :
//search criteria :: mySQL date format 
( SELECT DATE_FORMAT(booking_date, '%d/%m/%Y')booking_date FROM rbs_room_booking )
// String query = "SELECT booking_id,type_id,title,room_id,DATE_FORMAT(booking_date, '%d/%m/%Y')booking_date,DATE_FORMAT(start_time, '%d/%m/%Y')start_time,DATE_FORMAT(end_time, '%d/%m/%Y')end_time,description,(SELECT user_name FROM rbs_user WHERE user_id = a.entered_by)user_name FROM rbs_room_booking a where booking_id="+bookingid+"";

//search criteria :: get time from datetime column sql
( SELECT CAST(start_time AS TIME)starttime,CAST(end_time AS TIME)endtime FROM rbs_room_booking )
String query = "SELECT booking_id,type_id,title,room_id,DATE_FORMAT(booking_date, '%d/%m/%Y')booking_date,CAST(start_time AS TIME)starttime,CAST(end_time AS TIME)endtime,description,(SELECT user_name FROM rbs_user WHERE user_id = a.entered_by)user_name FROM rbs_room_booking a where booking_id="+bookingid+"";
    
 

Saturday, March 7, 2015

java Mysql image insert 2 way and DB coloumn datatype longblob( Form submit by jquery )

image insert  2nd way( Form submit by jquery ) :

1.jsp page :
<form id="myForm" action="javascript:insertRoomTypeModalClose();" 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>





2/ script load by jquery :
 function insertRoomTypeModalClose(){ //Start INSERT Meeting room Modal and Close
        // alert("insert query : Room Type");
    var kname="joya";
      /*    var udefineAddmroom = $("#udefineAddmroom").val();
         var rnameAddmroom = $("#rnameAddmroom").val();
        
         var pid1 = document.getElementById('parentidAddmroom');
         var parentidAddmroom = pid1.options[pid1.selectedIndex].value;
        
         var rt1 = document.getElementById('roomtypeAddmroom');
         var roomtypeAddmroom = rt1.options[rt1.selectedIndex].value;
        
         var sizeAddmroom = $("#sizeAddmroom").val();
         var capacityAddmroom = $("#capacityAddmroom").val();
         var deviceAddmroom = $("#deviceAddmroom").val();
        
       var ajaxURL = "insertMeetingRoomCloseRI.do?udefineAddmroom="+udefineAddmroom+"&rnameAddmroom="+rnameAddmroom+"&parentidAddmroom="+parentidAddmroom+"&roomtypeAddmroom="+roomtypeAddmroom+"&sizeAddmroom="+sizeAddmroom+"&capacityAddmroom="+capacityAddmroom+"&deviceAddmroom="+deviceAddmroom+"&userImage=userImage";
   
        $.ajax({
            async:false,
            url : ajaxURL,
            success : function(result) {
                $("#Kcontent_page").html(result);
               
            },
            complete:function(){
                dataTableInitializeMeetingRoomGP();
                $('#ARGSCCModal').modal('hide');
            }
           
        }); */
       
        $('#myForm').submit(function(event){
            //alert("Img");
            //disable the default form submission
              event.preventDefault();
            //grab all form data 
              var formData = new FormData($(this)[0]); 

            $.ajax({
                url: "insertMeetingRoomCloseMra.do?kname="+kname,
                type: "POST",     
                cache: false,
                processData: false,
                contentType: false,
                data: formData,
                success: function (res) {
                    $("#Kcontent_page").html(res);
                },
                complete:function(){
                    dataTableInitializeMeetingRoomGP();
                    $('#ARGSCCModal').modal('hide');
                } ,    

                error: function(jqXHR, textStatus, errorThrown) {
                        alert(textStatus+' : '+ errorThrown);
                     }

              });

        });

   
    }






struts.xml :
 <action name="*Mra" class="action.MeetingRoomAction" method="{1}">

    <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";       
    }




//FROM KYAMC iMage iNsert
    private File userImage;
    private String userImageContentType;
    private String userImageFileName;
    private HttpServletRequest servletRequest;


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











}
   

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
            }