設定流程:
1、軟體下載:
Microsoft JDBC Driver 4.0 for SQL Server
http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
2、目錄設定:
- 下載後即可點選執行檔,即可進行解壓縮。
- 解壓縮後,在 %ProgramFiles% 建立一個名稱為 Microsoft JDBC Driver 4.0 for SQL Server 的目錄。
- 再將檔案複製到此目錄下,目錄如下。
%ProgramFiles%\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_<version>\<language>\help\default.htm. This will display the help system in your Web browser.
3、環境變數設定:
CLASSPATH = .;C:\Program Files (x86)\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc.jar
4、範範程式:
import java.*;
import java.sql.*;
public class test_connection{
private java.sql.Connection con = null;
private final String url = "jdbc:sqlserver://";
private final String serverName= "SQL_SERVER_NAME";
private final String portNumber = "1433";
private final String databaseName= "DB_NAME";
private final String userName = "USER_NAME";
private final String password = "PASSWORD";
// Constructor
public test_connection(){}
private String getConnectionUrl(){
return url+serverName+":"+portNumber+";databaseName="+databaseName+";user="+userName+";password="+password+";";
}
private java.sql.Connection getConnection(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(getConnectionUrl());
if(con!=null) System.out.println("Connection Successful!");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
/*
Display the driver properties, database details
*/
public void displayDbProperties(){
java.sql.DatabaseMetaData dm = null;
java.sql.ResultSet rs = null;
try{
con= this.getConnection();
if(con!=null){
dm = con.getMetaData();
System.out.println("Driver Information");
System.out.println("\tDriver Name: "+ dm.getDriverName());
System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
System.out.println("\nDatabase Information ");
System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
System.out.println("Avalilable Catalogs ");
rs = dm.getCatalogs();
while(rs.next()){
System.out.println("\tcatalog: "+ rs.getString(1));
}
rs.close();
rs = null;
}else System.out.println("Error: No active Connection");
}catch(Exception e){
e.printStackTrace();
}
finally {
closeConnection();
}
dm=null;
}
public void test_insert(){
java.sql.Statement stmt = null;
String str_sql = "";
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat();
try{
con= this.getConnection();
stmt = con.createStatement();
df.applyPattern("yyyy-MM-dd HH:mm:ss");
if(con!=null){
str_sql = "insert into cary_test_table " +
"values(" + i + ",'test')";
stmt.execute(str_sql);
java.util.Date now = new java.util.Date();
System.out.println("Inserted Sucessful:" + df.format(now));
}else System.out.println("Error: No active Connection");
}catch(Exception e){
e.printStackTrace();
}
finally {
if(stmt != null){
try{
stmt.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
closeConnection();
}
}
private void closeConnection(){
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
test_connection myDbTest = new test_connection();
myDbTest.displayDbProperties();
myDbTest.test_insert();
}
}
參考網址:
Microsoft JDBC Driver 4.0 for SQL Server
http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
如何: 著手進行 Microsoft JDBC
http://support.microsoft.com/kb/313100
關鍵字:SQL Server、Java、JDBC
你好,我看了你的網誌連資料庫,不過一直無法成功,想跟你討論,因為沒看到電子郵件,故留訊息.不過希望能用電子信箱討論,我的電子信箱:tracy78w@yahoo.com.tw,謝謝你的觀看,希望有所回音,謝謝~
回覆刪除