评论(0)

SQL Server 2000和JDBC的融合实例

2008-03-05 09:11 CSDN afg021984 A+

  2、我们这次不通过odbc桥来操作数据库,我们采用SQL Server driver 来实现对SQLServer数据库的操作,这将是我们这篇文章的重点,因为JDBC-odbc桥是一种常见的操作windows系统数据库的常用方法,但它存在的缺点很多,所以现在很多开发者都侧重于使用SQLServer driver来操作,在这里我们通过一步步的调试,来加深读者对这种连接的理解。

  在通常的理解下,只要我们装了SQLServer driver for JDBC我们便可进行数据库编程,事实则不然,首先我们看下边的代码:

  /***********************************************
  /*
  /*DbTest.Java
  /*
  /******************************************* */
  import Java.SQL.*;
  public class DbTest {
  Connection con;
  Statement sta;
  ResultSet rs;
  String driver;
  String url;
  String user;
  String pwd;
  public DbTest()
  {
  driver = "com.microsoft.JDBC.SQLServer.
  SQLServerDriver";;
  url  = "JDBC:microsoft:SQLServer:
  //localhost:1433;DatabaseName
  =StoreManager";
  user  = "sa";
  pwd  = "potsmart10";
  init();
  }
  public void init()
  {
  try{
  Class.forName(driver);
  System.out.println("driver is ok");
  con = DriverManager.getConnection
  (url,user,pwd);
  System.out.println("conection is ok");
  sta = con.createStatement();
  rs = sta.executeQuery
  ("select * from room");
  while(rs.next())
  System.out.println
  (rs.getInt("roomNum"));
  }catch(Exception e)
  {
  e.printStackTrace();
  }
  }
  public static void main
  (String args [])
  //自己替换[]
  {
  new DbTest();
  }
  }


  这段代码跟上变得代码是一样的,差别在于驱动,还有url,这是在使用SQLServer driver for JDBC 中遇到的困惑

  按道理讲,上边这段代码应该没错,可首先我们来看一下,如果SQLser服务器没有升级到sp3(在使用JDBC时,如果系统是xp或者2003务必要把SQLServer 升级到sp3),我们看看运行结果:

  driver is ok
  Java.SQL.SQLException:
  [Microsoft][SQLServer 2000 Driver
  for JDBC]Error establis
  hing socket.
  at com.microsoft.JDBC.base.
  BaseExceptions.createException
  (Unknown Source
  )
  at com.microsoft.JDBC.base.
  BaseExceptions.getException
  (Unknown Source)
  at com.microsoft.JDBC.base.
  BaseExceptions.getException
  (Unknown Source)
  at com.microsoft.JDBC.SQLServer.
  tds.TDSConnection.
  (Unknown Source)
  at com.microsoft.JDBC.SQLServer.
  SQLServerImplConnection.open
  (Unknown Sou
  rce)
  at com.microsoft.JDBC.base.BaseConnection.
  getNewImplConnection(Unknown S
  ource)
  at com.microsoft.JDBC.base.
  BaseConnection.open(Unknown Source)
  at com.microsoft.JDBC.base.
  BaseDriver.connect(Unknown Source)
  at Java.SQL.DriverManager.
  getConnection(DriverManager.Java:523)
  at Java.SQL.DriverManager.
  getConnection(DriverManager.Java:171)
  at DbTest.init(DbTest.Java:32)
  at DbTest.(DbTest.Java:25)
  at DbTest.main(DbTest.Java:46)
  Press any key to continue...

本文导航 SQL Server 2000和JDBC的融合(2)

打开客户端,无广告困扰,阅读更专注

热门新闻

推荐内容