Java Sky of Zhao
受挫一次,对生活的理解加深一层;失误一次对人生的感悟增添一阶;不幸一次,对世界的认识成熟一级;磨难一次,对成功的内涵透彻一遍!
posts - 5,comments - 4,trackbacks - 0

[ 原创 ] JDBC 如何连接 SQL SERVER 2000 命名实例的调试全过程

 

引用请注明出处 :http//www.blogjava.net/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 阅读(1613) 评论(0)  编辑  收藏 所属分类: Java VS DataBase

只有注册用户登录后才能发表评论。


网站导航: