PooledConnection unverständlich

Alex2xm

Mitglied
Hallo Zusammen,

habe ein ziemlich nerviges Problem:
Um die Performance der DB -Anfragen zu verbessern möchte ich PooledConnections verwenden. Nur das Problem ist, dass ich nicht so ganz durchblicke.
Habe folgenden Beispielsource:

Code:
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.sql.PooledConnection;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlPooledConnection;

public class JDBCConnection3
{
    public static void main (String args[])
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch (ClassNotFoundException cnfe)
        {
            System.err.println("Driver class not found");
            cnfe.printStackTrace();
   
        }
        Connection con = null;
        try
        {
            con =
                    (Connection) DriverManager.getConnection(
                            "jdbc:mysql://localhost/db/",
                            "javauser",
                            "12345");

        }
        catch (SQLException e1)
        {
            System.err.println("Error establishing database connection");
            e1.printStackTrace();
        }
        PooledConnection pc = new MysqlPooledConnection(con);
        java.sql.Connection con1 = null;
        try
        {
            con1 = pc.getConnection();
   
        }
        catch (SQLException sqle)
        {
            sqle.printStackTrace();
       
        }
    }
}

Soweit so gut, nur wie bekomme ich nun dieses Beispiel in eine Methode die ich dann aufrufen kann, wenn ich eine DB-Verbindung benötige?

Für die Hilfe im Voraus vielen Dank

Alexander
 
Servus!

Versuchs doch mal so ...

(Ausporbiert mit dem aktuellen MySQL JDBC Treiber: http://www.mysql.com/downloads/api-jdbc-stable.html

Code:
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import java.sql.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlPooledConnection;

/*
 * Created on 06.01.2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class MySQLPooledTest {

	public MySQLPooledTest() {

	}

	public static void main(String[] args) {
		new MySQLPooledTest().doIt();
	}

	/**
	 * 
	 */
	private void doIt() {
		// TODO Auto-generated method stub
		MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
		ds.setDatabaseName("nordwind");
		ds.setPort(3306);
		ds.setUser("Administrator");
		ds.setServerName("localhost");
		MysqlPooledConnection polConn = null;
		Connection con = null;
		try {
			//Hier wird eine physische DB Connection geöffnet...
			polConn = (MysqlPooledConnection) ds.getPooledConnection();
			//Hier wird eine logische DB Connection geöffnet...
			con = polConn.getConnection();

			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("Select * from Artikel;");
			ResultSetMetaData rsmd = rs.getMetaData();

			while (rs.next()) {
				for (int i = 1; i <= rsmd.getColumnCount(); i++) {
					System.out.print(rs.getString(i) + " | ");
				}
				System.out.println();
			}
			rs.close();
			stmt.close();
			con.close();
			polConn.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

Gruß Tom
 

Neue Beiträge

Zurück