首页>代码>java常用工具类iceroot开源类库>/iceroot/src/main/java/com/icexxx/util/IceDbUtil.java
package com.icexxx.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
/**
 * 数据库操作相关工具类
 * @author IceWater
 * @date 2017-03-03
 * @version 1.0
 */
public class IceDbUtil {
    /**
     * 获取数据库连接
     * @param driver 数据库驱动名称
     * @param url 数据库url
     * @param username 用户名
     * @param password 密码
     * @return
     */
    public static Connection getConnection(String driver, String url, String username, String password) {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("数据库驱动jar包无法加载");
        }
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(driver, username, password);
        } catch (SQLException e) {
            String errorMessage = e.getMessage();
			if (errorMessage.indexOf("No suitable driver found for com.mysql.jdbc.Driver") != -1) {
				if (url.indexOf("?") != -1) {
					url = url.substring(0, url.indexOf("?"));
				}
				url = url + "?user=" + username + "&password=" + password + "&useUnicode=true&characterEncoding=utf-8";
				try {
					connection = DriverManager.getConnection(url);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
        }
        return connection;
    }
    
    /**
     * 执行sql语句
     * 
     * @param connection
     *            数据库连接
     * @param sql
     *            需要执行的sql语句
     * @return 成功执行的sql条数
     */
    public static int exeSql(Connection connection, String sql) {
        Statement createStatement = null;
        int executeUpdate = 0;
        try {
            sql = sql.replace("&lt;", "<");
            sql = sql.replace("&gt;", ">");
            sql = sql.replace("&amp;", "&");
            sql = sql.replace("&quot;", "\"");
            sql = sql.replace("&apos;", "'");
            createStatement = connection.createStatement();
            executeUpdate = createStatement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (createStatement != null) {
                try {
                    createStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return executeUpdate;
    }
    
    /**
     * 执行sql语句
     * 
     * @param connection
     *            数据库连接
     * @param sql
     *            需要执行的sql
     * @param params
     *            sql中的参数
     * @return 执行成功的条数
     */
    public static int exeSql(Connection connection, String sql, Object[] params) {
        PreparedStatement prepareStatement = null;
        int executeUpdate = 0;
        try {
            sql = sql.replace("&lt;", "<");
            sql = sql.replace("&gt;", ">");
            sql = sql.replace("&amp;", "&");
            sql = sql.replace("&quot;", "\"");
            sql = sql.replace("&apos;", "'");
            prepareStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                Object param = params[i];
                if (param instanceof String) {
                    prepareStatement.setString(i + 1, (String) param);
                } else if (param instanceof Integer) {
                    prepareStatement.setInt(i + 1, (Integer) param);
                } else if (param instanceof Long) {
                    prepareStatement.setLong(i + 1, (Long) param);
                } else if (param instanceof Boolean) {
                    prepareStatement.setBoolean(i + 1, (Boolean) param);
                } else if (param instanceof java.sql.Date) {
                    prepareStatement.setDate(i + 1, (java.sql.Date) param);
                } else if (param instanceof Date) {
                    prepareStatement.setDate(i + 1, new java.sql.Date(((Date) param).getTime()));
                } else if (param instanceof Double) {
                    prepareStatement.setDouble(i + 1, (Double) param);
                } else if (param instanceof Byte) {
                    prepareStatement.setByte(i + 1, (Byte) param);
                } else if (param instanceof Short) {
                    prepareStatement.setShort(i + 1, (Short) param);
                }
            }
            prepareStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (prepareStatement != null) {
                try {
                    prepareStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return executeUpdate;
    }
}
最近下载更多
TE6688  LV24 2017年6月27日
1040287230  LV4 2017年5月10日
zyl  LV34 2017年5月10日
kong.yee  LV40 2017年5月6日
xdd211414  LV17 2017年5月5日
zhljava  LV2 2017年5月5日
aihui523  LV34 2017年5月5日
海盗来了  LV20 2017年5月5日
最代码官方  LV167 2017年5月4日
最近浏览更多
1358849392  LV21 2022年11月11日
crosa_Don  LV18 2022年3月31日
zuidaima_liuzg  LV1 2021年5月13日
叽哩咕噜  LV2 2020年12月24日
dongzhan  LV12 2020年12月9日
wkc  LV21 2020年7月26日
Gyq灬ming  LV11 2019年11月30日
小资李  LV13 2019年9月17日
497100512 2019年4月27日
暂无贡献等级
哈哈zr  LV11 2019年4月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友