Monday, February 7, 2011

Jdbc type 4 driver

import driver from microsoft website and include in the lib folder.
package com.mycomp;
import java.io.BufferedReader;
import java.io.IOException;
import java.sql.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Hashtable;
import java.util.ArrayList;
public class DataBaseConnection 
{
    public static java.sql.Statement stmt = null;
    ResultSet rs = null;
   // public static final Connection conn = null;
    String tableName = "";
    
    public static final java.sql.Connection  conn = null;
    private final String url = "jdbc:microsoft:sqlserver://";
    private final String serverName= "localhost";
    private final String portNumber = "1433";
    private final String databaseName= "gpro_dev";
    private final String userName = "gproProd";
    private final String password = "gproProdPrivate";
    // Informs the driver to use server a side-cursor, 
    // which permits more than one active statement 
    // on a connection.
    private final String selectMethod = "cursor"; 
    
    // Constructor
    
    
    private String getConnectionUrl(){
         return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
    }
 DataBaseConnection(String tableName){
  try {
        //Register the JDBC driver for MySQL.
       // Class.forName("com.mysql.jdbc.Driver");
        //Define URL of database server.
        // with the default port number 3306.
       // String url =  "jdbc:mysql://localhost:3306/test";
        //Get a connection to the database for a test.
       // con = DriverManager.getConnection(url,"root", "Manu12345");
                    /*Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    Connection con = DriverManager.getConnection("jdbc:odbc:test", "sa", "Orange08");
   Connection conn=db.dbConnect(
          "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=");
*/
 
 
   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
   Connection conn = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
                       
   /*   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection conn = DriverManager.getConnection("jdbc:odbc:gpro_dev", "GProProd", "GProProdPrivate");
               if(conn==null)
               {System.out.println(conn);}
              // System.out.println(conn);
              */ 
               stmt = conn.createStatement();
         this.tableName = tableName;
  } catch (Exception e) {
   e.printStackTrace();
   // TODO: handle exception
  }
 }

    public void createFixedMessageTable(TreeMap p_columnHeaderValues)
    {
     try {
       StringBuffer strBuff = new StringBuffer();
       
     

       strBuff.append("create table " + tableName+" " + "(");
       
       boolean isValidQuery = false;
       Set words = p_columnHeaderValues.keySet();      
       for(Iterator it=words.iterator(); it.hasNext();)
       {             
         String element = (String) it.next();
         if(element.length()<=2)
         {
          strBuff.append("___" + element + "  varchar(200),");
                  }
         else{
     strBuff.append("_" + element + "  varchar(200),");
         }
    }
    if (strBuff.lastIndexOf(",") != -1) {
     strBuff.setCharAt(strBuff.lastIndexOf(","), ' ');
      strBuff.append(")");
      isValidQuery = true;
    }
   /// System.out.println("hi");
   // System.out.println("strBuff.toString() " + strBuff.toString());
 
         //Get a Statement object
      
    //  System.out.println(strBuff);
      
      stmt.executeUpdate(strBuff.toString());
    //  System.out.println("hi1");
          //   stmt.close();
 
       
     }catch(Exception exp){
      exp.printStackTrace();
     }
  }
    
    public void insertFIXValuesIntoTable(BufferedReader bufferedreader,TreeMap p_columnHeaderValues) throws IOException
    {
     StringBuffer strbuff1= new StringBuffer();
     String strLine = "";
     StringBuffer query = null;
     String tableheaderName = null;
  String tableValues = null;
  boolean isValidQuery = false;
  String elementvalues=null;
  StringBuffer strbuffValues= new StringBuffer();
  TreeMap realmap = new TreeMap();
  String myvalues = "i";
  TreeMap BigMap = new TreeMap();
  Collection headerwords=null;
   Collection headerwords1=null;
   Set keyString= null;
   Set words=null;
   Set word1=null;
     try {
       
      // System.out.println(valueset.toString());
       
    
   while(true){
     headerwords=null;
     headerwords1=null;
     words=null;
     keyString=null;
     word1=null;
     BigMap.clear();
     strbuff1.setLength(0);
     strbuffValues.setLength(0);
     if((strLine = bufferedreader.readLine()) == null){ break; }
     StringTokenizer strTokenizer = new StringTokenizer(strLine, "$"); 
     int numOfTokens = strTokenizer.countTokens(); 
     String value = "";
     String key = "";
     tableheaderName = new String();
     tableValues = new String();
     query = new StringBuffer();
     isValidQuery = false;
     tableheaderName="";
     int i = 0;
    // System.out.println("hi");
  
     BigMap.putAll(p_columnHeaderValues);
     words = BigMap.keySet();
  
     for(Iterator it=words.iterator(); it.hasNext();)
       { 
        i=i+1;
        String elem = (String)it.next();
            BigMap.put(elem,"imy"+i);
            }
     
      headerwords = BigMap.values();
     Set valueset = new HashSet(headerwords);
     //System.out.println("hi");
        for (int number = 0 ; number < numOfTokens  ; number++)
     { 
     String strData = strTokenizer.nextToken();
      if(strData.indexOf("=") != - 1)
      {
       key = strData.substring(0, strData.indexOf("="));
       value = strData.substring(strData.indexOf("=") + 1);
          BigMap.put(key, value);
      }
      }
      //  System.out.println("hi");
        /* For placing values in the new map*/
        keyString = BigMap.keySet();
     headerwords1 = BigMap.values();
        
     ArrayList valueString = null;
     
     try {
       valueString = new ArrayList (headerwords1);
        }
     catch(ClassCastException e) { }
     
     String regex ="imy%";
           for(Iterator it=valueString.iterator(); it.hasNext();)
         {
            elementvalues = (String)it.next();
            if(elementvalues.matches(regex))
            {elementvalues=null;}
            strbuffValues.append("\'" + elementvalues +"\'" +",");
          }
        //   System.out.println("hi4");
           if (strbuffValues.lastIndexOf(",") != -1) 
             {
             strbuffValues.setCharAt(strbuffValues.lastIndexOf(","), ' ');
             strbuffValues.append(")");
             isValidQuery = true;
             }
      //     System.out.println("hi5");
     /*Creating a buffer to get the matching header*/
             word1 = BigMap.keySet(); 
   //          System.out.println(word1.toString()); 
         for(Iterator it=word1.iterator(); it.hasNext();)
          {             
             String element = (String) it.next();
            // System.out.println(element);
              if(element.length()<=2)
              {
               strbuff1.append("___" + element + ",");
              }
              else
              {
             strbuff1.append("_" + element + ",");
              }
           
           }
     //    System.out.println("hi");
         if (strbuff1.lastIndexOf(",") != -1) 
          {
           strbuff1.setCharAt(strbuff1.lastIndexOf(","), ' ');
          }
      tableheaderName = strbuff1.toString();
    //  System.out.println("hi");
     //Insert Query
      query.append("insert into " + tableName +" "+ "(");
      query.append(tableheaderName.toString() + ")");
      query.append(" values (" +" "+ strbuffValues );
      System.out.println("query.. " + query.toString());
      isValidQuery = true;
      stmt.executeUpdate(query.toString());
              
      
   }
     }catch(Exception exp){
      exp.printStackTrace();
     }
    }
    
    /*public void closeConnection()
    {
     try {
     // conn.close();
     }catch(Exception exp){
      exp.printStackTrace();
     }
    }*/
}

No comments:

Post a Comment