c 连接mysql 通用类_JDBC 连接操作数据库通用类

public class DBUtils {private static Properties props;static {props = new Properties();// mysql// props.put("driver_class", "com.mysql.jdbc.Driver");// props.put("url", "jdbc:mysql://localhost:3306/test");// props.put("username", "root");// props.put("password", "root");// sql2005// props.put("driver_class", "com.microsoft.sqlserver.jdbc.SQLServerDriver");// props.put("url", "jdbc:sqlserver://localhost:1433;databaseName=petshop4;selectMethod=cursor");// props.put("username", "sa");// props.put("password", "sa");// oracle// props.put("driver_class", "oracle.jdbc.OracleDriver");// props.put("url", "jdbc:oracle:thin:@localhost:1521:XE");// props.put("username", "scott");// props.put("password", "tiger");// sql jtdsprops.put("driver_class", "net.sourceforge.jtds.jdbc.Driver");props.put("url", "jdbc:jtds:sqlserver://localhost:1433/pubs");props.put("username", "sa");props.put("password", "sa");// odbc// props.put("driver_class", "sun.jdbc.odbc.JdbcOdbcDriver");// props.put("url", "jdbc:odbc:test");}private static DBUtils me = new DBUtils();private DBUtils() { }public static DBUtils getInstance() { return me; }private static ThreadLocal localConn = new ThreadLocal();/*** 打开一个新的Connection* @return*/public Connection openConnection() {Connection conn = null;try {Class.forName(props.getProperty("driver_class"));conn = DriverManager.getConnection(props.getProperty("url"),props.getProperty("username"),props.getProperty("password"));conn.setAutoCommit(false);} catch (Exception e) {throw new RuntimeException("不能获得连接", e);}return conn;}/*** 释放ResultSet,Statement资源,请注意参数顺序应为ResultSet -> Statement* 注意Connection不需要你作为参数传入*/public void closeAll(Object... objects) {if(objects != null) {for (int i = 0; i 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值