1.JDBC连接
- public static Connection getConnection() {
- Connection conn =null;
- try {
- Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
- .newInstance();
- String url= "jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=db";// db为数据库名
- String user = "sa";
- String pwd = "admin";
- conn = DriverManager.getConnection(url, user,
- pwd);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- return conn;
- }
要导入如下三个包:
2.JTDS连接
- public static Connection getConnection() {
- String dbDriver = "net.sourceforge.jtds.jdbc.Driver";
- String url = "jdbc:jtds:sqlserver://localhost:1433/db";
- String user = "sa";
- String pwd = "admin";
- Connection conn = null;
- try {
- Class.forName(dbDriver);
- conn = DriverManager.getConnection(url, user, pwd);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
其中要导入的包为:jtds-1.2.6.jar