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
c 连接mysql 通用类_JDBC 连接操作数据库通用类
最新推荐文章于 2022-09-22 22:00:00 发布