首先下載對應的JDBC驅動包,sql2008的,然後引入到專案裡 透過這段程式碼可以獲取到連結 static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=資料庫的名字"; static final String USER = 你資料庫的使用者名稱; static final String PWD = 密碼; public static Connection getConnection() { Connection con = null; try { Class.forName(DRIVER); con = DriverManager.getConnection(URL, USER,PWD); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; }
首先下載對應的JDBC驅動包,sql2008的,然後引入到專案裡 透過這段程式碼可以獲取到連結 static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=資料庫的名字"; static final String USER = 你資料庫的使用者名稱; static final String PWD = 密碼; public static Connection getConnection() { Connection con = null; try { Class.forName(DRIVER); con = DriverManager.getConnection(URL, USER,PWD); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; }