连接sqlserver2005数据库

import java.sql.*;
public class ConnSqlServer2005 {
/**
  * @param args
  */
public static void main(String[] args) {
  // TODO Auto-generated method stub
  try {
   ConnSqlServer2005 sql = new ConnSqlServer2005();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
}


private Connection con;
private Statement stmt;
private ResultSet rs;
public ConnSqlServer2005() throws SQLException {
  try {  
   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();//1295  
   String url = "jdbc:sqlserver://localhost:1433;DatabaseName=search";
   String name = "";
   String password = "";
   con = DriverManager.getConnection(url,name,password);  
   stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
   String str = "select *  from news";  
   rs = stmt.executeQuery(str);  
   while (rs.next()) {    
    System.out.println(rs.getInt(1));  
    System.out.println(rs.getString(2));  
    System.out.println(rs.getString(3));  
    }  
   /* rs = stmt.executeQuery("select max(id) from news");  
    *  rs.next();  
    * int maxId = rs.getInt(1);  
    * maxId++;  
    * System.out.println("maxId:"+maxId);  
    * String insertSql = "insert into news(id,title,content)values("+maxId+",'新闻标题','新闻内容')";   boolean go = stmt.execute(insertSql);   System.out.println("go="+go);         */
   } catch (Exception e) {  
    e.printStackTrace();  
    }  
   if (con != null) {  
    con.close();  }
   if (stmt != null) {  
    stmt.close();  }
   if (rs != null) {  
    rs.close();  }
   }
  }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值