日期:2014-05-16 浏览次数:20471 次
public static Connection getPowerConnection2() { Connection conn = null; try{ //加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //建立数据库连接 String url = "jdbc:oracle:thin:@127.0.0.1:1521:demo"; conn = DriverManager.getConnection(url, "scott", "tiger"); }catch(ClassNotFoundException e){ System.out.println("找不到指定的驱动程序类"); }catch(SQLException e){ e.printStackTrace(); } return conn; }
public static void getPowerConnection(String[] srg) { String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=test"; //连接服务器和数据库test String userName = "sa"; //默认用户名 String userPwd = "123456"; //密码 Connection dbConn; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("Connection Successful!"); //如果连接成功控制台输出Connection Successful! } catch (Exception e) { e.printStackTrace(); } } }