標籤

顯示具有 連接資料庫 標籤的文章。 顯示所有文章
顯示具有 連接資料庫 標籤的文章。 顯示所有文章

2013年5月5日 星期日

WSH 連接 SQLServer

基本上SQLSERVER的連結跟昨天所講的差不多
只是在Open方法的連結引數上有所不同
看先一下範例:
  1. Option Explicit  
  2.   
  3. Private StrConn  
  4.   
  5. strConn = "Provider=SQLOLEDB;Password=1234;" & _   
  6.                    "Persist Security Info=True;User ID=AA;" & _   
  7.                    "Initial Catalog=TEST;" & _   
  8.                    "Data Source=SQLSERVER;Connect Timeout=15"   
  9.   
  10. With CreateObject("ADODB.Connection")  
  11.     .Open strConn  
  12.     .Close  
  13. End With  

大致上有改變的地方是:
Provider=SQLOLEDB; -->連結方式
Password=1234; -->連結所需的密碼
User ID=AA; -->連結所需的帳號
Initial Catalog=TEST; -->資料庫名稱

一般公司都會使用到SQLSERVER
小弟.我也會有需要,所以特地做了研習
有結果,當然是提供出來做分享囉..

提供給有需要的人.

2007年1月9日 星期二

利用 PreparedStatement 批次查詢

平常Statement及PreparedStatement的executeUpdate()的方法只能執行一個SQL敘述, 如果有很多SQL要執行的話,就得用addBatch()及 executeBatch()。

ex1.

String strqry="insert into test (col1,col2) values (?,?)";
PreparedStatement pstmt = con.prepareStatement(strqry);
pstmt.setString(1,"test1");
pstmt.setString(2, "123");
pstmt.addBatch();
pstmt.setString(1,"test2");
pstmt.setString(2, "456");
pstmt.addBatch();
pstmt.executeBatch();

ex2:

Statement stmt=con.createStatement();
stmt.addBatch("insert into test (col1,col2) values ('test1','123')");
stmt.addBatch("update test set col2='789' where col1='test4'");
stmt.executeBatch();

2006年11月16日 星期四

JDBC 實務:使用 JDBC 時,一定要放在 try 區塊中,並於 finally 區塊釋放 Connection

在 try/catch/finally 語法中,若有定義 finally 區塊,則執行 try 區塊後,一定會執行 finally 區塊的程式碼。
所以若程式有使用 JDBC 時,一定要包在 try 區塊中,並在 finally 中關閉 connection 釋放資源,才不會耗盡資源。
一般實務作法如下例:


Connection con=null;
try{
con=new Connection();
...
...
...
}finally{
try{
con.close();
}catch(Throwable e) {
String errmsg="close connection failed! errmsg is "+e.getMessage();
log.error(errmsg);
}
}