Jdbc connections recursive way


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 1 of 1

Thread: Jdbc connections recursive way

Hybrid View

  1. #1
    Join Date
    Apr 2010
    Location
    Bogota Colombia South America
    Posts
    1

    Smile Jdbc connections recursive way

    DEVX TEAM
    JAVA DEVELOPERS
    AND PROGRAMMERS

    I request your help in the following topic,
    i am doing a java program that store a
    complete file system in an Oracle Database 11GR2.

    So i use a recursive algorithm to do that
    but i don't know how to handle the JDBC
    connections in order to not overload the total of
    physical connections available to the database.

    Thanks for your help.
    Regards from Colombia - South America


    I am using the following code:

    *********************
    Code:
    import java.io.* ;
    import java.sql.*;
    import java.lang.String;
    import java.util.Date;
    import java.util.Calendar;
    import java.util.*;
    import java.text.SimpleDateFormat;
    import oracle.jdbc.*;
    import oracle.jdbc.pool.*;
    import javax.naming.*;
    import javax.naming.spi.*;
    
    public class InsertArcDir
    {
       public void AddFiles( File file )
       {
        try
        {
         Calendar fechamod=Calendar.getInstance();     
         String SQL;
         PreparedStatement pstmt = null;
    //
         java.util.Properties prop = new java.util.Properties();
         prop.setProperty("MinLimit", "2");
         prop.setProperty("MaxLimit", "10");
         OracleDataSource ods = new OracleDataSource();
         ods.setURL("jdbc:oracle:thin:@//oracle:1521/xe");
         ods.setUser("hr");
         ods.setPassword("hr");
         ods.setConnectionCachingEnabled(true);
         ods.setConnectionCacheProperties (prop);
         //ods.getConnectionCacheName();    
         Connection conn = ods.getConnection ("hr","hr",prop);
        
    //
         Statement stmt = conn.createStatement();
    
    
         fechamod.setTimeInMillis(file.lastModified());
         SQL="INSERT INTO tbarchivos(nombrearc,fechaarc,direcarc) VALUES(?,?,?)";    
         pstmt = conn.prepareStatement(SQL);
         java.sql.Timestamp sqlDate = new java.sql.Timestamp(fechamod.getTimeInMillis());
         pstmt.setString(1, file.getName());
         pstmt.setTimestamp(2, sqlDate);
         pstmt.setString(3, file.getParent());
         pstmt.executeUpdate();    
         stmt.close();
         conn.close();
        }
        catch(Exception e)
        {
         System.out.println("Exepcion"+e.getMessage());
         System.out.println("OTHER TEST");
        }
       }
       /**
        * Works on a single file system entry and
        * calls itself recursively if it turns out
        * to be a directory.
        * @param file A file or a directory to process
        */
       public void traverse( File file )
       {     
         
           // Check if it is a directory
         
           if( file.isDirectory() )
           {
              // Get a list of all the entries in the directory
              String entries[] = file.list() ;
     
              // Ensure that the list is not null
              if( entries != null )
              {
                 // Loop over all the entries
                 for( String entry : entries )
                 {
                   // Recursive call to traverse
                    traverse( new File(file,entry) ) ;
                 }
              }
           }
           else
           {
            if(file.isFile())
            {
             InsertArcDir rta = new InsertArcDir() ;
             rta.AddFiles(file);       
                 
                    
            }
            else
            {
             System.out.println("*** VALOR INCORRECTO ***");
            }      
           }
        //try
        //{
         //stmt.close();
         //conn.close();
        //}
        //catch(Exception e)
        //{
        // System.out.println("Exepcion"+e.getMessage());
        //}
       }
    
       /**
        * The program starts here.
        * @param args The arguments from the command line
        */
       public static void main( String args[] )
       {
        try
        {
         java.util.Properties prop = new java.util.Properties();
         prop.setProperty("MinLimit", "2");
         prop.setProperty("MaxLimit", "10");    
         OracleDataSource ods = new OracleDataSource();
         ods.setURL("jdbc:oracle:thin:@//oracle:1521/xe");
         ods.setUser("hr");
         ods.setPassword("hr");
         ods.setConnectionCachingEnabled(true);
         ods.setConnectionCacheProperties (prop);
         ods.setConnectionCacheName("ImplicitCache01");
         Connection conn = ods.getConnection ();
         conn.close();
    
       
         //Statement stmt = conn.createStatement();
          // Create an object of this class
          InsertArcDir rt = new InsertArcDir() ;
    
          if( args.length == 0 )
          {
             // If there are no arguments, traverse the current directory
             rt.traverse( new File(".") ) ;
          }
          else
          {
             // Else process every argument sequentially
             for( String arg : args )
             {
                rt.traverse( new File(arg) ) ;
             }
          }
         //stmt.close();
         //conn.close();
        }
        catch(Exception e)
        {
         System.out.println("Exepcion"+e.getMessage());
        }
       }
    }
    *********************
    Last edited by Hack; 04-14-2010 at 07:46 AM. Reason: Added Code Tags

Similar Threads

  1. Replies: 0
    Last Post: 11-22-2006, 10:06 AM
  2. Replies: 3
    Last Post: 08-24-2006, 10:15 PM
  3. JDBC configuration in Linux
    By manoj nahar in forum Database
    Replies: 0
    Last Post: 03-07-2002, 01:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center