JavaWeb增加&修改&删除//不要再打错字母了!!!
ResultSet是数据中查询结果返回的一种对象
获取字段的值
ResultSet.getXXX(col_name);
获取列名为col_name的值
添加
1 String sql = "insert into tb_brand(brand_name,company_name,ordered,description,status) values(?,?,?,?,?);";
2
3 PreparedStatement preparedStatement = connection.prepareStatement(sql);
4 preparedStatement.setString(1,brandName);
5 preparedStatement.setString(2,companyName);
6 preparedStatement.setInt(3,ordered);
7 preparedStatement.setString(4,description);
8 preparedStatement.setInt(5,statue);//自动产生的不需要设置
修改
首先知道id值然后通过id的值来查找
1 int id = 4;
2 String sql = "update tb_brand\n"+
3 "set brand_name = ?,\n"+
4 "company_name = ?,\n"+
5 "ordered = ?,\n"+
6 "description = ?,\n"+
7 "status = ?\n"+
8 "where id = ?";
9 PreparedStatement preparedStatement = connection.prepareStatement(sql);
10 preparedStatement.setString(1,brandName);
11 preparedStatement.setString(2,companyName);
12 preparedStatement.setInt(3,ordered);
13 preparedStatement.setString(4,description);
14 preparedStatement.setInt(5,status);
15 preparedStatement.setInt(6,id);
删除
1 String sql = "delete from tb_brand where id = ?";
2 preparedStatement.setInt(1,id);