[
原創
]
JDBC
如何連接
SQL SERVER 2000
命名實例的調試全過程
?
引用請注明出處
:http//www.tkk7.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)
經測試連接成功