数据库比较工具

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DBFunction
{
    
    public static Connection getConn(DbInfo dbInfo)
    {
        Connection conn = null;
        try
        {
            if (null == conn)
            {
                Class.forName(dbInfo.getDriver());
                conn = DriverManager.getConnection(dbInfo.getUrl(), dbInfo.getUserName(), dbInfo.getPwd());
            }
        }
        catch (ClassNotFoundException cnfe)
        {
            System.out.println("ClassNotFoundException");
        }
        catch (SQLException sqle)
        {
            System.out.println("conn exception ");
        }
        return conn;
    }
    
    /**
     * <一句话功能简述>执行sql语句并得到结果
     * <功能详细描述>
     * @param sql
     * @return
     * @throws SQLException 
     * @see [类、类#方法、类#成员]
     */
    public static List executeSql(String sql, Connection conn)
        throws SQLException
    {
        
        Statement st = null;
        ResultSet rs = null;
        List result = new ArrayList();
        
        try
        {
            
            sql = sql.toUpperCase();
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            String columName = "";
            String values = "";
            
            // 只取select 和 from中间的字段信息
            String[] columns = sql.substring(7, sql.indexOf(" FROM")).split(",");
            while (rs.next())
            {
                values = "";
                
                for (int i = 0; i < columns.length; i++)
                {
                    columName = columns[i].trim();
                    
                    // 这个字段的值是<long>型要特殊处理
                    if (columName.trim().equalsIgnoreCase("DATA_DEFAULT"))
                    {
                        values = values + Tools.streamToString(rs.getBinaryStream(i + 1));
                    }
                    else
                    {
                        values = values + rs.getString(i + 1);
                    }
                    
                    if (i < columns.length - 1)
                    {
                        values = values + ",";
                    }
                    
                }
                
                result.add(values);
            }
        }
        catch (Exception ex)
        {
            System.out.println("sql:" + sql);
            System.out.println("Excute sql occur exception:" + ex.getMessage());
        }
        finally
        {
            try
            {
                if (null != st)
                {
                    st.close();
                    
                }
                if (null != rs)
                {
                    rs.close();
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
        
        return result;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值