ivaneeo's blog

          自由的力量,自由的生活。

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
          Class.forName(props.getProperty("driver"));
          String url = props.getProperty("url");
          Connection con = DriverManager.getConnection(url, props);
          //        showWarnings(con.getWarnings());
          initLanguage(con);
          return con;

          上面的Connection類的方法getWarnings檢索調用這個Connection對象的第一個警告.如果有多個可以用getNextWarning方法得到.

          public void showWarnings(SQLWarning w) {
            while (w != null) {
              System.out.println(w.getMessage());
              w = w.getNextWarning();
            }
          }

          private void initLanguage(Connection con) throws SQLException {
          Statement stmt = con.createStatement();

          stmt.executeUpdate("set LANGUAGE 'us_english'");
          stmt.close();
          }
          上面函數顯示了一個比較簡單的調用語句的方法.

          這里是使用的屬性文件注冊.屬性文件如下:
          --------------------------------------------------------------------
          #屬性文件connection.properties.tmpl
          #
          # The URL of the server to which you want to connect
          #
          driver=net.sourceforge.jtds.jdbc.Driver
          url=jdbc:jtds:sqlserver://localhost/dbname
          USER=XXXX
          PASSWORD=YYYY
          SERVERNAME=localhost
          PORTNUMBER=1433
          DATABASENAME=dbname
          XAEMULATION=true

          #
          # TDS Version
          #

          #
          # Use this for MS SQL Server 6.5+
          #
          #TDS=4.2

          #
          # Use this for Sybase 10+
          #
          #TDS=5.0

          #
          # Use this for MS SQL Server 7.0+
          #
          #TDS=7.0

          #
          # Use this for MS SQL Server 2000 (default)
          #
          TDS=8.0
          --------------------------------------------------------------------

          讀入屬性文件如下:
          String CONNECTION_PROPERTIES = "conf/connection.properties";
          File propFile = new File(fileName);
          if (!propFile.exists()) {
            fail("Connection properties not found (" + propFile + ").");
          }
          try {
          Properties props = new Properties();
          props.load(new FileInputStream(propFile));
          return props;
          }
          catch (IOException e) {
          throw new RuntimeException(e.getMessage());
          }
          這里是fail函數是使用了junit的fail方法,表示測試失敗.
          posted on 2005-08-04 21:13 ivaneeo 閱讀(624) 評論(0)  編輯  收藏 所屬分類: java魔力
          主站蜘蛛池模板: 汕尾市| 特克斯县| 山西省| 会东县| 永修县| 香河县| 乐清市| 济南市| 灌阳县| 府谷县| 德江县| 达孜县| 综艺| 基隆市| 玉田县| 黄大仙区| 格尔木市| 夏河县| 鲁甸县| 凤阳县| 乡宁县| 平武县| 塔河县| 麻阳| 郓城县| 晋中市| 白玉县| 灵璧县| 会宁县| 长岛县| 察雅县| 苗栗县| 岳西县| 钦州市| 柯坪县| 泽库县| 沙雅县| 布拖县| 大邑县| 鹤庆县| 普安县|