java 串連SqlServer資料庫測試代碼
package com.connDBTest;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class GetConnectionSqlServer {public void getConnectionSqlServer() {String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";String dbURL = "jdbc:sqlserver://localhost:1433;databasename=USCSecondhandMarketDB"; // 1433是連接埠,"USCSecondhandMarketDB"是資料庫名稱String userName = "sa"; // 使用者名稱String userPwd = "sa"; // 密碼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 blocke.printStackTrace();}}}public static void main(String[] args) {GetConnectionSqlServer getConn = new GetConnectionSqlServer();getConn.getConnectionSqlServer();}}