import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

 

public class ConnServre

{

public ConnServre()

{

String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  // 記得下載需要使用到的dirver jar檔

try

{

Class.forName(driver); 

} catch (ClassNotFoundException e)

{

System.err.println("驅動程式沒上線");

e.printStackTrace();

}

 
String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=你的資料庫名稱";

// 127.0.0.1 是本機, 1433 是通訊埠號,

// 如果要連他人的server,就把127.0.0.1 改成他的server IP,通訊埠號也要改

 

Connection conn = null;

try

{

conn = DriverManager.getConnection(url, "你的登入帳號", "你的登入密碼");
System.out.println("資料庫連線成功!!");

} catch (SQLException e)

{

System.err.println("資料庫連線失敗");

e.printStackTrace();

}

 

try

{

conn.close();

} catch (SQLException e)

{

System.err.println("資料庫連線未關閉!!");

e.printStackTrace();

}

}

}

 

登入的帳號、密碼 可以在 MSSQL server management tool 的[安全性]->[登入] 設定,如果要供他人使用的帳號要留意權限喔!!

 未命名  

 

by ting


 

創作者介紹
創作者 cycu98的部落格 的頭像
cycu98

cycu98的部落格

cycu98 發表在 痞客邦 留言(0) 人氣( 2104 )