public class GetConnectionSqlServer { public void getConnectionSqlServer() { String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL = "jdbc:sqlserver://133.96.46.100:1433;databasename=test"; // 1433是端口,"test"是数据库名称 String userName = "test"; // 用户名 String userPwd = "test"; // 密码 Connection dbConn = null; try { Class.forName(driverName).newInstance(); } catch (Exception ex) { System.out.println("驱动加载失败"); ex.printStackTrace(); } try { dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("成功连接数据库!"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (dbConn != null) dbConn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { GetConnectionSqlServer getConn = new GetConnectionSqlServer(); getConn.getConnectionSqlServer(); } }
时间: 2024-10-29 19:12:34