增删改查

时间:2020-09-28 17:39:09   收藏:0   阅读:44

一、插入数据
@Test
public void insert() throws SQLException{
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
String sql ="INSERT INTO product VALUES(6,‘米饭‘,0.2)";
int insert = queryRunner.update(sql);
System.out.println("成功插入"+ insert +"条");
}

二、查询

@Test
public void select() throws SQLException{
QueryRunner query = new QueryRunner(C3P0Utils.getDataSource());
String sql= "select * from product";
List<Product> dt = query.query(sql,new BeanListHandler<Product>(Product.class));
System.out.println(dt);
}

三、修改

@Test
public void update4() throws SQLException{
QueryRunner queryrunner = new QueryRunner(C3P0Utils.getDataSource());
String sql = "update product set pname=‘老三‘,price=12232 where pid=4";
int update = queryrunner.update(sql);
System.out.println("成功修改"+update+"条");
}

 

四、删除

 

@Test
public void delete() throws SQLException{
QueryRunner run = new QueryRunner(C3P0Utils.getDataSource());
String sql2 = "delete from product where pid=? ";
Object [] parmes = {2};
int re = run.update(sql2,parmes);
System.out.println(re);
}

@test:代替main方法

原文:https://www.cnblogs.com/wsx123/p/13745871.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!