Pages

2011年8月13日 星期六

Microsoft JDBC 連接 URL 範例


在下列範例中,範例程式碼會在連接 URL 中設定各種連接屬性,然後呼叫 DriverManager 類別的 getConnection 方法,以傳回SQLServerConnection 物件。
接著,範例程式碼會使用 SQLServerConnection 物件的 createStatement 方法建立 SQLServerStatement 物件,然後呼叫 executeQuery方法來執行 SQL 陳述式。
最後,範例會使用從 executeQuery 方法傳回的 SQLServerResultSet 物件,重複執行 SQL 陳述式所傳回的結果。


  1. import java.sql.*;
  2.  
  3. public class connectURL {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. // Create a variable for the connection string.
  8. String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
  9. "databaseName=AdventureWorks;user=UserName;password=*****";
  10.  
  11. // Declare the JDBC objects.
  12. Connection con = null;
  13. Statement stmt = null;
  14. ResultSet rs = null;
  15.  
  16. try {
  17. // Establish the connection.
  18. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  19. con = DriverManager.getConnection(connectionUrl);
  20.  
  21. // Create and execute an SQL statement that returns some data.
  22. String SQL = "SELECT TOP 10 * FROM Person.Contact";
  23. stmt = con.createStatement();
  24. rs = stmt.executeQuery(SQL);
  25.  
  26. // Iterate through the data in the result set and display it.
  27. while (rs.next()) {
  28. System.out.println(rs.getString(4) + " " + rs.getString(6));
  29. }
  30. rs.close();
  31. rs = null;
  32. stmt.close();
  33. stmt = null;
  34. con.close();
  35. con = null;
  36. }
  37.  
  38. // Handle any errors that may have occurred.
  39. catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. finally {
  43. if (rs != null) try { rs.close(); } catch(Exception e) {}
  44. if (stmt != null) try { stmt.close(); } catch(Exception e) {}
  45. if (con != null) try { con.close(); } catch(Exception e) {}
  46. }
  47. }
  48. }

Related Posts:

  • Java String 依照單字出現次數/頻率高的依序印出功能: 依照每個單字出現的次數,由大到小,排列印出。 假設: String oriString = "This is a book. That is a pencil" 輸出: is 出現 2 次 a 出現 2 次 This 出現 1 次 That 出現 1 次 book 出現 1 次 pencil 出現 1 次 程式碼: import java.util.regex.*; import java.util.*; public class… Read More
  • Microsoft JDBC 連接 URL 範例 在下列範例中,範例程式碼會在連接 URL 中設定各種連接屬性,然後呼叫 DriverManager 類別的 getConnection 方法,以傳回SQLServerConnection 物件。 接著,範例程式碼會使用 SQLServerConnection 物件的 createStatement 方法建立 SQLServerStatemen… Read More
  • Big5部分中文顯示?處理最近在抓股票資料,但是因為有些上市櫃公司中文字,在預設Big5編碼中不支援,文字抓出來後會顯示?號,因此需要指定特定的編碼方式,才能正確的寫入資料庫中,目前Java的的解決方式如下: File sotckFile = new File(fileName); InputStreamReader isr = null; BufferedReader br = null; try { isr = new InputStreamReader(… Read More
  • 使用Triple DES 加密字串最近因為有需求,需要使用加密的方法來讓連線的帳號密碼用成亂碼來連線。 因此找到了使用 Tripple DES的方式加密字串。 1. 首先,先寄建立Decrypter與Encrypter package security.crypto; import java.io.UnsupportedEncodingException; import javax.crypto.Cipher; import javax.crypto.IllegalBloc… Read More
  • 自動擷取憑證與Https頁面資料在寫JSP使用Redirect的時候,如果是導向HTTPS頁面,也可能發生某些異常。 1. java.net.MalformedURLException: unknown protocol https ANS: 使用的方法不是 HTTPS 的方式,將jce.jar與jsse.jar導入classpath,在使用https連線函式 2. 頁面停留過久 ANS: 就像打開IE一樣,會叫你點選接受憑證,這時用程式跑就自動停住了,居然不會timeout … Read More

0 意見: