java4all@1986 java. Powered by Blogger.

How to insert new records into database by checking that the record is already inserted or not?

>> Friday, June 17, 2011

import java.sql.*;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;
 

public class Test1 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Connection conn = null;
       String url = "jdbc:mysql://192.168.1.101:3306/";//here 192.168.1.101 is server dsn Address(i.e in our system localhost)
       String dbName = "atrdev";
       String driver = "com.mysql.jdbc.Driver";
       String userName = "atrdev"; 
       String password = "centris";
       ResultSet  rs  =null; 
       PreparedStatement prest;
       Statement st;
      
       try{
       Class.forName(driver).newInstance();
       conn = DriverManager.getConnection(url+dbName,userName,password);
       Calendar now = Calendar.getInstance();
       
        String[] months = {"Jan", "February",    // 
         /* System.out.println("Current Year is : " + now.get(Calendar.YEAR));
          // month start from 0 to 11
          System.out.println("Current Month is : " + (now.get(Calendar.MONTH) + 1));
          System.out.println("Current Date is : " + now.get(Calendar.DATE));
     */
         "March", "April", "May", "June", "July",
         "August", "September", "October", "November",
         "December"};
        
         Calendar cal = Calendar.getInstance(); 
         String month = months[(cal.get(Calendar.MONTH)-1)];//to get previous month 
         System.out.println("month.................. "+month);
         boolean check=true;
        String query = "select  date_time from atr_employee_info_audit where  month_salary like'"+month+"%' order by date_time limit 1";//here LIMIT class is used to get record based on number here we had taken 1,if 2 means ,2 records
           st = conn.createStatement();
          
              rs= st.executeQuery(query);
                
                         
                             
           while(rs.next()){
            check=false;
               String s=rs.getString("date_time");
               System.out.println("false");
               String arr[]=s.split("-");
                   
                     System.out.println(arr[0]);
                     String ss=arr[0];//we get year 
                     System.out.println("dasddfa"+ss);
                    
                    Calendar cal1=Calendar.getInstance();
                    int year=cal1.get(Calendar.YEAR);
                    String yearstring = Integer.toString(year);
                    System.out.println("Current Year: "+yearstring );
            
                     
                     
             System.out.println("datetime"+s);
           }
           if(check==true)
           {
                                  //here if records are not avaiable w.r.t previous month then inserting records into dataBase
           st=conn.createStatement();
           int i=st.executeUpdate("insert into atrdev.atr_employee_info_audit  values (emp_info_audit_key, empcode, empname, emp_designation, emp_department, emp_bank_acc_no, emp_email, emp_no_ofdays_worked, emp_basic, emp_hra, emp_conveyance,emp_onsite_allowence,emp_medical,emp_pf_emp_con,emp_gross,emp_pf, emp_itax,emp_pro_tax,emp_other_deduct,emp_tot_deduct,emp_net_sal,date_time,month_salary,create_user,modify_user,message_body)");
            System.out.println(+i);
            System.out.println("ffdgfdgsdfgs");
           }
       }catch (Exception e) {
   // TODO: handle exception
        e.printStackTrace();
  }
 }

}

0 comments:

Post a Comment

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP