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

[ 原创 ] JDBC 如何连接 SQL SERVER 2000 的默认实例和命名实例

 

引用请注明出处 :http//www.blogjava.net/SINOJAVA

 

( ) 下载 Microsoft SQL Server 2000 Service Pack 3a 并安装
http://www.microsoft.com/downloads/details.aspx?FamilyId=90DCD52C-0488-4E46-AFBF-ACACE5369FA3&displaylang=zh-cn

 

( ) 下载并安装 SQL SERVER 2000 JDBC 驱动 ( SQL Server 2000 Driver for JDBC Service Pack 3 )

http://www.microsoft.com/downloads/details.aspx?FamilyId=07287B11-0502-461A-B138-2AA54BFDC03A&displaylang=en

 

( ) 配置 JDBC 驱动 classpath 环境变量 :

方法 1:

 

填加 :

C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;

C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar;

C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar;

 

: C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\ 为我 JDBC 得安装路径

 

方法 2:

 

mssqlserver.jar; msutil.jar; msbase.jar 拷贝到 JDK lib 目录下 , 然后设置 classpath

 

D:\Java\jdk1.5.0_05\lib\sqlserver\msutil.jar;

D:\Java\jdk1.5.0_05\lib\sqlserver\msbase.jar;

D:\Java\jdk1.5.0_05\lib\sqlserver\mssqlserver.jar;

 

( ) 创建连接数据库默认实例得连接代码 :

 

import java.sql.*;

public class ConSqlserver {

    

     Connection con=null;

     //Statement st=null;

     //ResultSet re=null;

    

     public ConSqlserver()

     {

            try

            {

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

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

                Class.forName(driver).newInstance();

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

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

                con.close();

           

            }

            catch(Exception e)

            {

                   e.printStackTrace();

            }

           

           

     }

 

       public static void main(String[] args)

     {

            // TODO 自动生成方法存根

            ConSqlserver con=new ConSqlserver();

           

 

     }

 

}

 

( ) 创建连接数据库命名实例的连接代码 ( 实例名为 SINOIT\SINOSERVER) L

 

import java.sql.*;

public class ConSqlserverppp {

    

     Connection con=null;

     //Statement st=null;

     //ResultSet re=null;

    

     public ConSqlserverppp()

     {

            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();

            }

           

           

     }

 

     public static void main(String[] args)

     {

           

            ConSqlserverppp con=new ConSqlserverppp();

     }

 

}

posted on 2006-04-10 07:24 SINOJAVA 阅读(2644) 评论(1)  编辑  收藏

FeedBack:
# re: [ 原创--置顶] JDBC 如何连接 SQL SERVER 2000 的默认实例和命名实例
2007-01-26 17:35 | marigold、
简单明了,很好呀!  回复  更多评论
  

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


网站导航: