
/*
* 修改
* 1.sql语句
*update tb_brand
set brand_name=?,
company_name=?,
ordered=?,
description=?,
status=?
where id=?
* 2.参数:需要所有参数信息
* 3.结果:boolean
* */
@Test
public void testBrand2() throws Exception {
//模拟用户输入内容
String brandName="香飘飘";
String companyName="香飘飘有限公司";
int ordered=1000;
String description="绕地球三圈";
int status=1;
int id = 4;
//1.导入jar包
//2.定义配置文件
//3.加载配置文件
Properties prop = new Properties();
//获取数据库的位置信息,加载数据库
prop.load(new FileInputStream("F:\\Develop\\code\\Demo2\\src\\druid.txt"));
//4.获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5.获取数据库连接Connection
Connection connection = dataSource.getConnection();
//6.定义SQL
String sql="update tb_brand\n" +
" set brand_name=?,\n" +
" company_name=?,\n" +
" ordered=?,\n" +
" description=?,\n" +
" status=?\n" +
" where id=?\n";
//7.获取pstmt对象
PreparedStatement pstmt = connection.prepareStatement(sql);
//8.设置参数
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
pstmt.setInt(6,id);
//9.执行sql
int update = pstmt.executeUpdate();//返回受影响行数
//10.判断返回值
if (update>0){
System.out.println("执行成功");
}else {
System.out.println("执行失败");
}
//11.释放资源
connection.close();
pstmt.close();