Java JDBC Driver getStatement()

Here you can find the source of getStatement()

Description

Get db statement

License

Open Source License

Return

null if exception occurred

Declaration

public static Statement getStatement() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.sql.Connection;
import java.sql.Driver;

import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;

public class Main {
    private static String dbuser = "", dbpwd = "", dbname = "", dbhost = "";
    private static Driver driver = null;
    private static Object NULL = new Object();
    private static List<Connection> freeConns = new ArrayList<Connection>();
    private static Map<Connection, Object> usedConns = new ConcurrentHashMap<Connection, Object>();
    private static Map<Statement, Connection> statConns = new ConcurrentHashMap<Statement, Connection>();

    /**/*from   w w w.ja  v a 2 s  .c  o m*/
     * Get db statement
     * 
     * @return null if exception occurred
     */
    public static Statement getStatement() {
        try {
            Connection conn = getConnection();
            Statement stmt = conn.createStatement();
            statConns.put(stmt, conn);
            return stmt;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Get db statement, change dbname if necessary, not stored in connection
     * pool
     * 
     * @param dbname
     * @return null if exception occurred
     */
    public static Statement getStatement(String dbname) {
        try {
            if (driver == null)
                driver = new org.gjt.mm.mysql.Driver();
            Connection conn = driver.connect(
                    String.format("jdbc:mysql://%s/%s?user=%s&password=%s", dbhost, dbname, dbuser, dbpwd), null);
            Statement stmt = conn.createStatement();
            return stmt;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Get db connection
     * 
     * @return null if exception occurred
     */
    public synchronized static Connection getConnection() {
        try {
            Connection conn = null;
            if (freeConns.isEmpty()) {
                if (driver == null)
                    driver = new org.gjt.mm.mysql.Driver();
                conn = driver.connect(
                        String.format("jdbc:mysql://%s/%s?user=%s&password=%s", dbhost, dbname, dbuser, dbpwd),
                        null);
            } else {
                conn = freeConns.get(freeConns.size() - 1);
                freeConns.remove(conn);
                try {
                    Statement stmt = conn.createStatement();
                    stmt.execute("SELECT 1");
                    stmt.close();
                    stmt = null;
                } catch (Exception e) {
                    if (!conn.isClosed())
                        conn.close();
                    conn = null;
                    return getConnection();
                }
                // if (conn.isClosed()) {
                // // break weak reference, notify system gc
                // conn = null;
                // return getConnection();
                // }
            }
            usedConns.put(conn, NULL);
            return conn;
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. getDriverClassName(String rawUrl)
  2. getDriverClassName(String rawUrl)
  3. getDriverFromPath(String path, String className)
  4. getRegisteredDrivers()
  5. getRSInfo(Object rsObj, int[] rsInfo, long[] rsCounter, Object[] conn, int[] errCode, String[] errDetail)
  6. getUnderlyingDriver(String url)
  7. hasDriver()
  8. isDriverLoaded(String driver)
  9. loadDriver(String driver)