共用方式為


基本資料類型範例

此 Microsoft SQL Server 2005 JDBC Driver 範例應用程式示範如何使用結果集 getter 方法,來擷取基本的 SQL Server 資料類型值,以及如何使用結果集 update 方法,來更新那些值。

此範例的程式碼檔案名稱為 basicDT.java,可以在下列位置找到它:

<安裝目錄>\sqljdbc_<版本>\<語言>\help\samples\datatypes

需求

若要執行此範例應用程式,您必須設定 Classpath 以包括 sqljdbc.jar 檔。如果 Classpath 遺漏 sqljdbc.jar 的項目,範例應用程式將會擲出「找不到類別」的一般例外狀況。您也需要對 SQL Server 2005 AdventureWorks 範例資料庫具有存取權。如需如何設定 Classpath 的詳細資訊,請參閱<使用 JDBC 驅動程式>。

您也必須在 SQL Server 2005 AdventureWorks 範例資料庫中建立下列資料表及範例資料:

CREATE TABLE DataTypesTable 
   (Col1 int IDENTITY, 
    Col2 char,
    Col3 varchar(50), 
    Col4 bit,
    Col5 decimal(18, 2),
    Col6 money,
    Col7 datetime);

INSERT INTO DataTypesTable 
VALUES ('A', 'Some text.', 0, 15.25, 10.00, '01/01/2006 23:59:59.991');

範例

在下列範例中,範例程式碼會建立連接到 SQL Server 2005 AdventureWorks 資料庫,然後從 DataTypesTable 測試資料表擷取單一資料列。然後,會呼叫自訂的 displayRow 方法,使用 SQLServerResultSet 類別的各種 get<Type> 方法,來顯示結果集中包含的所有資料。

接著,範例會使用 SQLServerResultSet 類別的各種 update<Type> 方法,更新結果集中包含的資料,然後呼叫 updateRow 方法,將該資料存回資料庫。

最後,範例會重新整理結果集中包含的資料,然後呼叫自訂的 displayRow 方法,顯示結果集中包含的資料。

import java.sql.*;

public class basicDT {

   public static void main(String[] args) {

      // Create a variable for the connection string.
      String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
            "databaseName=AdventureWorks;integratedSecurity=true;";

      // Declare the JDBC objects.
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;

      try {
         // Establish the connection.
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         con = DriverManager.getConnection(connectionUrl);

         // Create and execute an SQL statement that returns some data
         // and display it.
         String SQL = "SELECT * FROM DataTypesTable;";
         stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
         rs = stmt.executeQuery(SQL);
         rs.next();
         displayRow("ORIGINAL DATA", rs);

         // Update the data in the result set.
         rs.updateString(2, "B");
         rs.updateString(3, "Some updated text.");
         rs.updateBoolean(4, true);
         rs.updateDouble(5, 77.89);
         rs.updateDouble(6, 1000.01);
         rs.updateTimestamp(7, new Timestamp(System.currentTimeMillis()));
         rs.updateRow();

         // Get the updated data from the database and display it.
         rs = stmt.executeQuery(SQL);
         rs.next();
         displayRow("UPDATED DATA", rs);
      }

      // Handle any errors that may have occurred.
      catch (Exception e) {
         e.printStackTrace();
      }

      finally {
         if (rs != null) try { rs.close(); } catch(Exception e) {}
         if (stmt != null) try { stmt.close(); } catch(Exception e) {}
         if (con != null) try { con.close(); } catch(Exception e) {}
      }
   }

   public static void displayRow(String title, ResultSet rs) {
      try {
         System.out.println(title);
         System.out.println(rs.getInt(1) + " " +  // SQL integer type.
               rs.getString(2) + " " +            // SQL char type.
               rs.getString(3) + " " +            // SQL varchar type.
               rs.getBoolean(4) + " " +           // SQL bit type.
               rs.getDouble(5) + " " +            // SQL decimal type.
               rs.getDouble(6) + " " +            // SQL money type.
               rs.getTimestamp(7));               // SQL datetime type.
         System.out.println();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

另請參閱

其他資源

使用資料類型 (JDBC)