public Connection getConnection() {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=db";
String username = "sa";
String password = "******";
Connection con = null;
try {
Class.forName(driverName);
con = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return con;
}
java数据库连接例子,在主程序里调用一下就行。
Connection conn = this.getConnection();