Java Sky of Zhao
          受挫一次,對生活的理解加深一層;失誤一次對人生的感悟增添一階;不幸一次,對世界的認識成熟一級;磨難一次,對成功的內涵透徹一遍!
          posts - 5,comments - 4,trackbacks - 0

          [ 原創 ] JDBC 如何連接 SQL SERVER 2000 命名實例的調試全過程

          ?

          引用請注明出處 :http//www.aygfsteel.com/SINOJAVA

          ?

          ( ) 整個操作及出錯簡要介紹 :

          ?

          我用微軟提供的 JDBC 驅動程序來連接 , 我機子 SQL SERVER2000 安裝時創建的是新的 SQL SERVER2000 命名實例 ( 沒有安裝 SQL Server 2000 數據庫默認實例 ), jdbc 安裝 , 配置好以后開始連接數據庫 , 我使用的連接語句如下 ( 我已經裝了 SP3 補丁 ) L

          ?

          try

          ?????????? {

          ?????????? ??? String driver="com.microsoft.jdbc.sqlserver.SqlServerDriver";

          ?????????? ??? String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=northwind;";

          ?????????? ???

          ?????????? ??? Class.forName(driver);

          ?????????? ??? con =DriverManager.getConnection(url,"sa","zhaopf");

          ?????????? ? ??System.out.println(" 連接成功 !");

          ?????????? ??? con.close();

          ??????????

          ?????????? }

          catch(Exception e)

          ?????????? {

          ????????????????? e.printStackTrace();

          ?????????? }

          運行程序后出現如下錯誤提示 :

          ?

          E:\Java\eclipse>java ConSqlserver

          java.lang.ClassNotFoundException: com.microsoft.jdbc

          ?????? ?at java.net.URLClassLoader$1.run(Unknown Sou

          ? ????? ?at java.security.AccessController.doPrivileg

          ??????? at java.net.URLClassLoader.findClass(Unknown

          ??????? at java.lang.ClassLoader.loadClass(Unknown S

          ??????? at sun.misc.Launcher$AppClassLoader.loadClas

          ????? ? ?at java.lang.ClassLoader.loadClass(Unknown S

          ??????? at java.lang.ClassLoader.loadClassInternal(U

          ??????? at java.lang.Class.forName0(Native Method)

          ??????? at java.lang.Class.forName(Unknown Source)

          ????? ? ?at ConSqlserver.<init>(ConSqlserver.java:19)

          ??????? at ConSqlserver.main(ConSqlserver.java:40)

          ?

          ?

          ?

          ( ) 調試過程如下 :

          ?

          (1) ?????? 排除 jdbc 驅動出錯 : 將配置 jdbc 驅動的環境變量刪除后運行 , 顯示的錯誤信息如下 :

          ?

          ? E:\Java\eclipse>java ConSqlserver

          java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

          ??????? at java.net.URLClassLoader$1.run(Unknown Source)

          ??????? at java.security.AccessController.doPrivileged(Native Method)

          ??????? at java.net.URLClassLoader.findClass(Unknown Source)

          ??????? at java.lang.ClassLoader.loadClass(Unknown Source)

          ??????? at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

          ??????? at java.lang.ClassLoader.loadClass(Unknown Source)

          ??????? at java.lang.ClassLoader.loadClassInternal(Unknown Source)

          ??????? at java.lang.Class.forName0(Native Method)

          ??????? at java.lang.Class.forName(Unknown Source)

          ??????? at ConSqlserver.<init>(ConSqlserver.java:37)

          ??????? at ConSqlserver.main(ConSqlserver.java:60)

          ?

          : 排除是 jdbc 驅動安裝的問題

          (2) ?????? 排除 SQL SERVER 安裝問題 , 重新安裝 SQL SERVER 2000, 此次安裝選用 SQL SERVER2000 的默認實例 , 還用上述的連接代碼段 , 然后繼續調試程序成功 , 運行顯示 :

          ?

          ?? E:\Java\eclipse>java ConSqlserver

          連接成功 !

          (3) ?????? 確認數據庫的安裝中創建實例概念模糊

          (4) ?????? 徹底弄清 SQL SERVER 2000 的默認實例與命名實例的含義

          (5) ?????? 重新查找關于 SQL SERVER 2000 JDBC 的幫助文檔 ,

          ?

          查得資料如下 :

          ?

          Microsoft SQL Server 2000 supports multiple instances of a SQL Server database running

          concurrently on the same server. An instance is identified by an instance name.

          ?

          To connect to a named instance using a connection URL, use the following URL format:

          ?

          jdbc:microsoft:sqlserver://server_name\\instance_name?

          ?

          NOTE: The first backslash character (\) in \\instance_name is an escape character.

          ?

          where:

          ?

          server_name is the IP address or hostname of the server.

          ?

          instance_name is the name of the instance to which you want to connect on the server.

          ?

          For example, the following connection URL connects to an instance named instance1 on

          server1:

          ?

          jdbc:microsoft:sqlserver://server1\\instance1;User=test;Password=secret

          ?

          To connect to a named instance using a data source, specify the ServerName connection

          property as described in the "Connection String Properties" topic.

          ?

          ???

          (6) ?????? 重新修改連接代碼如下 :

          ?

          try

          ??????????? {

          ??????????? ??? //String driver="com.microsoft.jdbc.sqlserver.SqlServerDriver";

          ??????????? ??? String url="jdbc:microsoft:sqlserver://SINOIT\\SINOSERVER;DatabaseName=Northwind";

          ??????????? ???

          ??????????? ??? Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

          ??????????????????

          ??????????? ??? //Class.forName(driver);//.newInstance();

          ??????????? ??? con =DriverManager.getConnection(url,"sa","zse");

          ??????????? ??? //st=con.createStatement();

          ??????????? ??? System.out.println(" 連接成功 !");

          ??????????? ??? con.close();

          ???????????

          ??????????? }

          ??????????? catch(Exception e)

          ??????????? {

          ?????????????????? e.printStackTrace();

          ??????????? }

          (7) 經測試連接成功

          posted on 2006-04-10 07:09 SINOJAVA 閱讀(1622) 評論(0)  編輯  收藏 所屬分類: Java VS DataBase
          主站蜘蛛池模板: 砚山县| 高碑店市| 嘉定区| 临漳县| 吉林市| 武定县| 老河口市| 和政县| 南陵县| 洪江市| 利川市| 黔西县| 报价| 大连市| 靖西县| 韶山市| 武威市| 托里县| 棋牌| 肃宁县| 拜城县| 中西区| 尚义县| 威信县| 灌阳县| 迁西县| 渭源县| 中西区| 尼玛县| 桓台县| 沿河| 烟台市| 格尔木市| 黔西| 新干县| 保康县| 汉寿县| 凤庆县| 措勤县| 巩留县| 克东县|