查询操作

2017-12-19  本文已影响0人  冰天雪地_6409

/*

*/
public class Update {
public static void main(String[] args) throws SQLException {
//创建QueryRunner对象
QueryRunner qr = new QueryRunner(JDBCUtils.getDS());
//执行sql语句
String sql = "UPDATE product set price = ? where pid = ?";
Object[] params = {10,10};

   //执行的 更新操作  (增删改)update
   int row = qr.update(sql,params);
   
   System.out.println("影响了:"+row+"行");

}
}

做查询之前
创建一个javabean类 与 数据库表中字段对应上
/*

*/
public class Product {
private int pid;
private String pname;
private double price;
private String cname;

public Product() {
    super();
    // TODO Auto-generated constructor stub
}

public int getPid() {
    return pid;
}

public void setPid(int pid) {
    this.pid = pid;
}

public String getPname() {
    return pname;
}

public void setPname(String pname) {
    this.pname = pname;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

public String getCname() {
    return cname;
}

public void setCname(String cname) {
    this.cname = cname;
}

@Override
public String toString() {
    return "Product [pid=" + pid + ", pname=" + pname + ", price=" + price + ", cname=" + cname + "]";
}

}
/*

*/
public class QueryDemo {
public static void main(String[] args) throws SQLException {
//创建QueryRunner对象
QueryRunner qr = new QueryRunner(JDBCUtils.getDS());

  //执行sql语句
  String sql = "select * from product";
  
  /*
   * sql语句 
   *  第二个参数 处理结果集(二维表)的方式  
   *  第三个参数 代表  ? 
   */
  Object[] params = {};
  List<Product> query = qr.query(sql, new BeanListHandler<Product>(Product.class), params);

  for (Product product : query) {
      System.out.println(product);
  }

}
}

上一篇 下一篇

猜你喜欢

热点阅读