当前位置:   article > 正文

jdbc连接SQL Server 2005_jdbc sqlserver sql2005

jdbc sqlserver sql2005

 

安装SQL Server 2005

帐户类型:本地系统

身份验证模式:混合模式

usr_id:sa

pwd:list_seq

实例名:默认

 

使用JDBC 1.2连接SQL Server 2005

1 据说因为SQL SERVER2005被攻击,所以SP2版对LOCALHOST端口1433进行了屏蔽

  进入Micro Sql Server 2005->配制工具->SQL Server Configuration Manager 

   ->SQL Server配制管理 器(本地)

   ->SQL Server 2005 网络配制

   ->MSSQLSERVER的协议->TCP/IP 

   启用1433端口,启用tct/ip协议.

   重起Microsoft SQL Server Management Studio.

 

 

 

2 连接数据库

 

2.1 web应用连通性测试

  本地deploy一个WeblogicWeb应用,进入控制台http://localhost:7001/console

  配置 Connection Pool:

    Database Type:MS SQL Server

    Database Driver: *BEA's MS SQL Server Driver (Type 4) Versions:7.0, 2000

      测试成功

 

2.2 java应用程序连接性测试(以下为test.java文件的内容)

** 需要两个文件的支持: sqljdbc_auth.dll , 放在WEB应用的根目录下. sqljdbc.jar,放在C:/Program Files/Microsoft SQL Server 2005 JDBC Driver/sqljdbc_1.2/chs/sqljdbc.jar, 并加入WEB应用的.classpath中即可.

public class test

{  

public static void main(String[] args)

{      

       //Create a variable for the connection string.

       String connectionUrl = "jdbc:sqlserver://localhost:1433;integratedSecurity=true;databaseName=scx;";      

       // Declare the JDBC objects.      

       Connection con = null;      

       Statement stmt = null;      

       ResultSet rs = null;      

       try

       {         

              // Establish the connection.        

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

              con = DriverManager.getConnection(connectionUrl);         

              // Create and execute an SQL statement that returns some data.         

              String SQL = "SELECT * FROM usr_info";         

              stmt = con.createStatement();         

              rs = stmt.executeQuery(SQL);         

              // Iterate through the data in the result set and display it.         

              while (rs.next())

              {            

                     System.out.println(rs.getString(1) + " " + rs.getString(2));         

              }       

       }       

       catch (Exception e)

       {         

              e.printStackTrace();     

       }finally

       {         

              if (rs != null)

              try

        {

                     rs.close();

              } catch(Exception e)

              {

                     System.out.println(e.toString());  

              }         

              if (stmt != null)

              try

              {

                     stmt.close();

              }catch(Exception e)

              {

                     System.out.println(e.toString());  

              }         

              if (con != null)

              try

              {

                     con.close();

              }catch(Exception e)

              {

                     System.out.println(e.toString());  

              }     

       }  

      }

}

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/600502
推荐阅读
相关标签
  

闽ICP备14008679号