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()); } } }


Reply With Quote



Bookmarks