jtds

          JTDS是一个开放源代码的100%纯Java的,用于JDBC 3.0驱动Microsoft SQL Server (6.5 ,7 ,2000和2005版本)和Sybase(10 ,11 ,12 ,15 版本)的驱动程序 。 JTDS是基于freetds的,并且是目前最快的生产准备JDBC驱动程序为SQL Server和Sybase 。 JTDS完全与JDBC 3.0兼容,支持只向前的,和可滚动/可更新的结果集(ResultSets)中并行(完全独立)语句,并且可实施所有databasemetadata和resultsetmetadata方法。

 

         JTDS不需要特别的安装,只需要把JAR文件放在classpath路径中就可以了。

  官方网站下载地址:http://sourceforge.net/project/showfiles.php?group_id=33291

  官方网站首页:http://jtds.sourceforge.net/

 

用jtds连接SQL2000的方法

 

要点:

  数据库URL:jdbc:jtds:sqlserver://localhost:1433;DatabaseName=bid

  驱动类:net.sourceforge.jtds.jdbc.Driver

  -----------------------------------------------------------------------

  对比:

  //microsoft

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

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

  //jtds

  Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();

  String url = "jdbc:jtds:sqlserver://localhost:1433;DatabaseName=pubs";

  //String url = "jdbc:jtds:sqlserver://localhost:1433/pubs";

  String user = "sa";

  String password = "dog";

  Connection conn = DriverManager.getConnection(url, user, password);

  Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

  String sql = "select top 10 * from titles"; //titles为表名;

  ResultSet rs = stmt.executeQuery(sql);

  ---------------------------------------------------------------------

  示例:

  JAVA使用JTDS连接SQL2000问题

  一般有以下几个方面:

  1.WINDOWS防火墙屏蔽了1433端口

  2.检查SQL2000是否使用的是1433端口

  3.检查SQL2000是否升级到SP3以上版本(基本都是这个原因)

  以下是使用JTDS连接SQL2000的代码段

  连接SQL2000下的TheTest库

  --------------------------------------------------------------

  public static Connection getConnection(){

  String dbDriver = "net.sourceforge.jtds.jdbc.Driver";

  String strConnection = "jdbc:jtds:sqlserver://localhost:1433/TheTest";

  String user = "sa";

  String password = "sa";

  Connection conn = null;

  try{

  //定义连接驱动

  Class.forName(dbDriver);

  }

  catch(java.lang.ClassNotFoundException e){

  System.err.println("DBconnection():"+e.getMessage());

  }

  //--------连接SQL数据库------------------

  try

  {

  conn = DriverManager.getConnection(strConnection,user,password);

  }

  catch(SQLException ex)

  {

  System.err.println("aq.executeQuery:"+ex.getMessage());

  }

  return conn;

  }

  -----------------------以下为关闭连接--------------------------

  public static void closeConnection(PreparedStatement ps,Connection conn,ResultSet rs){

  try{

  if (rs!=null){

  rs.close();

  }

  if (ps!=null){

  ps.close();

  }

  if (conn!=null){

  conn.close();

  }

  }

  catch(SQLException sqlerror){

  sqlerror.printStackTrace();

  }

  }

  public static void closeConnection(PreparedStatement ps,Connection conn){

  try{

  if (ps!=null){

  ps.close();

  }

  if (conn!=null){

  conn.close();

  }

  }

  catch(SQLException sqlerror){

  sqlerror.printStackTrace();

  }

  }

  public static void closeConnection(Connection conn){

  try{

  if (conn!=null){

  conn.close();

  }

  }

  catch(SQLException sqlerror){

  sqlerror.printStackTrace();

  }

  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值