JDBC连接数据库,对数据库进行增删查改操作

1 篇文章 0 订阅

 

 

public static Connection getCon() {
  Connection conn=null;
  try {

//2005连接方法
   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
   conn = DriverManager
     .getConnection("jdbc:sqlserver://localhost;username=sa;password=;DatabaseName=DB_Shop");
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return conn;
 }

 


 private Connection conn = null;
 private Statement stmt = null;
 private ResultSet rs = null;
 private String sql = null;
 private boolean b = false;
 public Collection<LiuVo> c = null;
 private ArrayList<LiuVo> list = null;
 private PreparedStatement pstmt = null;
 private LiuVo liuvo = null;
 private int i=0;

 /**
  * 查询不重复的类型
  *
  * @return
  */
 public Collection select_distinct_type() {
  try {
   sql = "select distinct(types) from userinfo";
   stmt = DB.getConnection().createStatement();
   rs = stmt.executeQuery(sql);
   while (rs.next()) {
    list = new ArrayList<LiuVo>();
    do {
     liuvo = new LiuVo();
     liuvo.setType(rs.getString(1));
     list.add(liuvo);
    } while (rs.next());
   }
  } catch (Exception ex) {

  }
  return list;
 }

 


 /**
  * 修改
  *
  * @return
  */
 public int update_result(String id ,String result) {
  sql = "update userinfoset results='"+result+" 'where ids="+id;

  System.out.println(sql);
  
  try{
  pstmt= DB.getConnection().prepareStatement(sql);
  i=pstmt.executeUpdate();

  }catch(Exception ex){
   ex.printStackTrace();
  }
  return i;
 }

 /**
  * 添加
  *
  * @return
  */
 public boolean add(LiuVo vo) {
  sql = "INSERT INTO userinfo( numbers, names, types, contexts,remark) VALUES (?,?,?,?,?)";

  try {
   pstmt = DB.getConnection().prepareStatement(sql);
   pstmt.setString(1, vo.getNumber());
   pstmt.setString(2, vo.getName());
   pstmt.setString(3, vo.getType());
   pstmt.setString(4, vo.getContext());
   pstmt.setString(5, vo.getRemark());
   int i = pstmt.executeUpdate();
   if (i > 0) {
    b = true;
   }
  } catch (Exception ex) {
   ex.printStackTrace();
  } finally {
   if (pstmt != null)
    try {
     pstmt.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  }

  return b;
 }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值