Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Apache common DBCP and JOCL file

843859Nov 15 2005
I'm just a beginner with jdbc technology. I developed a sample application using Apache Common DBCP and MySQL.

In the first attempt I create a PoolingDriver, I register the Pool, get the connection and submit the query.
In this attempt all works properly.

In the second attemp I try to set the connection parameter whith a jocl file defined into the classpath (in the config
subdirectory). This exception has been raised:

[java] Registering driver ... done.
[java] [Fatal Error] :5:26: Invalid byte 2 of 3-byte UTF-8 sequence.
[java] Establishing connection from connection pool using jocl file ... ERROR !!
[java] ERROR: Could not parse configuration file
[java] org.apache.commons.dbcp.SQLNestedException: Could not parse configuration file
[java] at org.apache.commons.dbcp.PoolingDriver.getConnectionPool(PoolingDriver.java:114)
[java] at org.apache.commons.dbcp.PoolingDriver.connect(PoolingDriver.java:170)
[java] at java.sql.DriverManager.getConnection(DriverManager.java:525)
[java] at java.sql.DriverManager.getConnection(DriverManager.java:193)
[java] at prova.jdbc.Main.<init>(Unknown Source)
[java] at prova.jdbc.Main.main(Unknown Source)
[java] Caused by: org.xml.sax.SAXParseException: Invalid byte 2 of 3-byte UTF-8 sequence.
[java] at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1269)
[java] at org.apache.commons.jocl.JOCLContentHandler.parse(JOCLContentHandler.java:335)
[java] at org.apache.commons.jocl.JOCLContentHandler.parse(JOCLContentHandler.java:264)
[java] at org.apache.commons.dbcp.PoolingDriver.getConnectionPool(PoolingDriver.java:111)
[java] ... 5 more

This is the java code:
package prova.jdbc;

import java.sql.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

//Connection pool
import org.apache.commons.pool.impl.*;
import org.apache.commons.dbcp.*;
import org.apache.commons.pool.*;

public class Main{
	
	private final static String JDBC_URL_NO_PWD		= "jdbc:mysql://localhost:3306/test";
	private final static String POOL_DRIVER_NAME 	= "pooltest";
	private final static String JDBC_POOL_URL			= "jdbc:apache:commons:dbcp:" + POOL_DRIVER_NAME;
	private final static String JDBC_JOCL_URL			= "jdbc:apache:commons:dbcp:/configuration";
	private final static String MYSQL_DRIVER			= "com.mysql.jdbc.Driver";
	private final static String POOLING_DRIVER		= "org.apache.commons.dbcp.PoolingDriver";
	
	public Main(){
		
		try{
			
			Calendar cal = Calendar.getInstance();
			java.util.Date today = cal.getTime();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
			System.out.println("---------------- " + formatter.format(today) + "----------------");
			
			System.out.print("Loading jdbc driver ...");
			Class.forName(MYSQL_DRIVER);
			System.out.println(" done.");
			
			String secondQuery = "select * from prova where name = ?";
				
			/**
		 	 * Test 3: Uso del dbcp (connection pool)
		 	 */
			
			//creo il pool
			ObjectPool pool = new GenericObjectPool(null);
			
			//creo un connection factory supponendo di usare per stabilire la connessione un
			//DriverManager
			DriverManagerConnectionFactory connFactory = new DriverManagerConnectionFactory(JDBC_URL_NO_PWD, "root", "dba");
			
			PoolableConnectionFactory poolableConnFactory = new PoolableConnectionFactory(connFactory, pool, null, null, false, true);
			
			//Registro il driver
			PoolingDriver poolingDriver = new PoolingDriver();
			poolingDriver.registerPool(POOL_DRIVER_NAME, pool);
			
			System.out.print("Establishing connection from connection pool ... ");
			Connection conn = DriverManager.getConnection(JDBC_POOL_URL);
			System.out.println(" done."); 	
			
			System.out.print("\n\nCreating prepared statement .....");
			PreparedStatement pstm = conn.prepareStatement(secondQuery);
			System.out.println("\nSubmitting query: <" + secondQuery + ">\n");
			pstm.setString(1,"Mauro");
			
			ResultSet rs = pstm.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			
			int cols = rsmd.getColumnCount();
			
			//Stampo il nome delle colonne ...
			for (int i = 1; i <= cols; i++){
				System.out.print(rsmd.getColumnName(i) + "\t");
			}
			System.out.println("\n--------------------------------------------\n");
			
			//... ed i dati
			while (rs.next()){
				StringBuffer sb = new StringBuffer();
				
				for (int i = 1; i <= cols; i++){
					sb.append(rs.getString(i));	
					sb.append("\t");
				}
				System.out.println(sb.toString());
			}
			
			System.out.print("\n\nReleasing resources (resultset, statement, connection) ... ");
			rs.close();
			pstm.close();
			conn.close();
			System.out.println(" done");
			
			/**
			 * Esempio 4: utilizzo di dbcp con file jocl (Java Object Configuration Language) di configurazione
			 */
			
			//Registro il driver
			System.out.print("\n\nRegistering driver ...");
			Class.forName(POOLING_DRIVER);
			System.out.println(" done.");
			
			//Recupero la connessione specificando nella JDBCURL il file jocl di configurazione
			System.out.print("Establishing connection from connection pool using jocl file ... ");
			conn = DriverManager.getConnection(JDBC_JOCL_URL);
			System.out.println(" done."); 	
			
			System.out.print("\n\nCreating prepared statement .....");
			pstm = conn.prepareStatement(secondQuery);
			System.out.println("\nSubmitting query: <" + secondQuery + ">\n");
			pstm.setString(1,"Mauro");
			rs = pstm.executeQuery();
			
			rsmd = rs.getMetaData();
			
			cols = rsmd.getColumnCount();
			
			//Stampo il nome delle colonne ...
			for (int i = 1; i <= cols; i++){
				System.out.print(rsmd.getColumnName(i) + "\t");
			}
			System.out.println("\n--------------------------------------------\n");
			
			//... ed i dati
			while (rs.next()){
				StringBuffer sb = new StringBuffer();
				
				for (int i = 1; i <= cols; i++){
					sb.append(rs.getString(i));	
					sb.append("\t");
				}
				System.out.println(sb.toString());
			}
			
			System.out.print("\n\nReleasing resources (resultset, statement, connection) ... ");
			rs.close();
			pstm.close();
			conn.close();
			System.out.println(" done");
		}
		catch (Exception e){
			System.out.println("ERROR !!");
			System.out.println("ERROR: " + e.getMessage());
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	public static void main(String[] args){
		new Main();
	}
}
This is the jocl file:
<?xml version="1.0" encoding="UTF-8"?>   
   
<object class="org.apache.commons.dbcp.PoolableConnectionFactory" xmlns="http://apache.org/xml/xmlns/jakarta/commons/jocl">

	<!-- Il primo argomento � il ConnectionFactory -->
	<object class="org.apache.commons.dbcp.DriverManagerConnectionFactory">
		<string value="jdbc:mysql://localhost:3306/test"/>	
		<string value="root"/>	
		<string value="dba"/>
	</object>

	<!-- Il secondo argomento � l'ObjectPool -->
	<object class="org.apache.commons.pool.impl.GenericObjectPool">
		<object class="org.apache.commons.pool.PoolableObjectFactory" null="true"/>
		
		<!-- massimo numero connessioni attive -->
		<int value="10"/> 
		<!-- quando non ci sono piu' connessioni libere 0 = errore, 1 = blocca, 2 = cresci --> 
		<byte value="1"/>
		<!-- massima attesa -->
		<long value="2000"/> 
		<!-- massimo numero di connessioni in attesa -->
		<int value="10"/> 
		<!-- esegui il test sulla richiesta -->
		<boolean value="true"/> 
		<!-- esegui il test dopo il ritorno -->
		<boolean value="false"/> 
		<!-- millisecondi di attesa tra i cicli del processo di controllo -->
		<long value="10000"/> 
		<!-- numero di connessioni da verificare perch� si esegua il processo di controllo --> 
		<int value="5"/> 
		<!-- tempo minimo di controllo -->
		<long value="5000"/> 
		<!-- test quando in attesa -->
		<boolean value="false"/> 
	</object>
	
	<!-- Il terzo argomento � il KeyedObjectPoolFactory -->
	<object class="org.apache.commons.pool.KeyedObjectPoolFactory" null="true"/>

	<!-- Il quarto argomento � la stringa di validazione -->
	<string value="select count(*) from dual"/>

	<!-- Il quinto argomento � il flag per il read-only -->
	<boolean value="false"/>
	
	<!-- Il sesto argomento � il flag di auto commit -->
	<boolean value="true"/>
</object>
I don't know what's wrong .. any help ?

Thanks in advance,
Mauro.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 13 2005
Added on Nov 15 2005
0 comments
1,143 views