日期:2014-05-19  浏览次数:20648 次

我喜欢的数据操作方式 读<抛弃框架,走自己的路>后
hibernate我觉得将数据的数据全部缓存起来有点消耗内存,你要是自己写bean吧,又很麻烦.新手sql都还没有搞明白,又来一个hql.
ibatis呢,在做查询的时候,如果没有查询参数,就不使用那个字段作为查询条件,这种情况需要在xml里写一大堆逻辑判断是很傻很难维护的事情.

我就用过上面的2种咚咚,我觉得数据应该这么使用,综合起来性价比较高
Java code

/**
 * @author Aray Chou
 *         Email: Aray(dot)Chou(dot)CN(at)gmail(dot)com
 *         Replace "(dot)" with "." and replace "(at)" with "@"
 */
package com.teenyee.frame.core.common.db;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.DataSources;

/**
 * 数据库工具
 */
public final class DatabaseUtils
{
    private final static String JDBC_URL;

    // private final static String JDBC_URL =
    // "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db1.95130951.com)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=db2.95130951.com)(PORT=1521))(LOAD_BALANCE=yes)(failover=yes)(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=db.yft)))";

    private final static String JDBC_USER;

    private final static String JDBC_PASSWORD;

    static
    {
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("database.properties");
        Properties p = new Properties();
        try
        {
            p.load(in);
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.out.println(e);
        }
        JDBC_URL = (String) p.get("JDBC_URL");
        JDBC_USER = (String) p.get("JDBC_USER");
        JDBC_PASSWORD = (String) p.get("JDBC_PASSWORD");

    };

    // private final static String JDBC_PASSWORD = "zheshiframe";

    private static DataSource dataSource;

    /**
     * @return 网络连接
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException
    {
        return dataSource.getConnection();
    }

    // test only
    public static void main(String[] argv)
    {
        try
        {

            System.out.println(getNextNo());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {

        }

    }

    /**
     * 断开数据库连接池,20s后重新连接
     */
    public static void reconnect()
    {
        try
        {
            DataSources.destroy(dataSource);
            Thread.sleep(1000 * 20);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        connect();
    }

    public static void connect()
    {
        Driver driver;
        try
        {
            driver = (Driver) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            java.sql.DriverManager.registerDriver(driver);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        
        DataSource unpooled;
        try
        {
            unpooled = DataSources.unpooledDataSource(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
            dataSource = DataSources.pooledDataSource(unpooled);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 断开数据库连接池
     */
    public static void destroy()
    {