用jtds连接SQL2008的方法

要点:   

数据库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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值