JDBC关于数据库的添加操作
public class BrandTest {
@Test
public void testAdd() throws Exception{
// 1。 接受页面提交的参数
String brandName = "香飘飘";
String companyName = "香飘飘";
int ordered = 1;
String description ="地球一圈";
int status =1;
// 1,获取conneiton
// 3.加载配置文件
Properties prop =new Properties();
prop.load(new FileInputStream("C:\\Users\\lengxin\\IdeaProjects\\jdbc-demo\\lib\\druid.properties"));
// 4.获取连接池数据对象
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
// 5.获取数据库链接
Connection conn=dataSource.getConnection();
// 2.定义sql语句
// String sql ="select * from tb_brand";
String sql ="insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
// 3.获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4。设置参数
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
// 5.执行sql
int count=pstmt.executeUpdate();
// 6.处理结果
System.out.println(count>0);
// 7.释放资源
pstmt.close();
conn.close();
}
}
public class BrandTest {
@Test
public void testAdd() throws Exception{
// 1。 接受页面提交的参数
String brandName = “香飘飘”;
String companyName = “香飘飘”;
int ordered = 1;
String description =“地球一圈”;
int status =1;
// 1,获取conneiton
// 3.加载配置文件
Properties prop =new Properties();
prop.load(new FileInputStream(“C:\Users\lengxin\IdeaProjects\jdbc-demo\lib\druid.properties”));
// 4.获取连接池数据对象
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
// 5.获取数据库链接
Connection conn=dataSource.getConnection();
// 2.定义sql语句
// String sql =“select * from tb_brand”;
String sql =“insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);”;
// 3.获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4。设置参数
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
// 5.执行sql
int count=pstmt.executeUpdate();
// 6.处理结果
System.out.println(count>0);
// 7.释放资源
pstmt.close();
conn.close();
}
}
这时候刷新数据库 查看数据库的添加操作是否成功
*这里在学习过程中犯了一个错误,就是把原来的testAdd增加操作给删除了,导致代码块错误,在这里,修改直接在添加操作下面开始编写代码即可
//修改
//1.update tb_brand
// set brand_name = ?,
// company_name = ?,
// ordered = ?,
// status = ?,
//品牌数据的增删改查
public class BrandTest {
@Test
public void testAdd() throws Exception{
// 1。 接受页面提交的参数
String brandName = "香飘飘";
String companyName = "香飘飘";
int ordered = 1;
String description ="地球一圈";
int status =1;
// 1,获取conneiton
// 3.加载配置文件
Properties prop =new Properties();
prop.load(new FileInputStream("C:\\Users\\lengxin\\IdeaProjects\\jdbc-demo\\lib\\druid.properties"));
// 4.获取连接池数据对象
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
// 5.获取数据库链接
Connection conn=dataSource.getConnection();
// 2.定义sql语句
// String sql ="select * from tb_brand";
String sql ="insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
// 3.获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4。设置参数
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
// 5.执行sql
int count=pstmt.executeUpdate();
// 6.处理结果
System.out.println(count>0);
// 7.释放资源
pstmt.close();
conn.close();
}
@Test
public void testUpdate() throws Exception{
// 1。 接受页面提交的参数
String brandName = "香飘飘";
String companyName = "香飘飘";
int ordered = 1000;
String description ="地球三圈";
int status =1;
int id = 4;
// 1,获取conneiton
// 3.加载配置文件
Properties prop =new Properties();
prop.load(new FileInputStream("C:\\Users\\lengxin\\IdeaProjects\\jdbc-demo\\lib\\druid.properties"));
// 4.获取连接池数据对象
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
// 5.获取数据库链接
Connection conn=dataSource.getConnection();
// 2.定义sql语句
// String sql ="select * from tb_brand";
String sql ="update tb_brand\n" +
"set brand_name = ?,\n" +
"company_name = ?,\n"+
"ordered = ?,\n"+
"description = ?,\n"+
"status = ?\n"+
"where id = ?";
// 3.获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4。设置参数
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
pstmt.setInt(6,id);
// 5.执行sql
int count=pstmt.executeUpdate();
// 6.处理结果
System.out.println(count>0);
// 7.释放资源
pstmt.close();
conn.close();
}
}
运行

这里可以查看数据库是否修改成功

修改完成
## 下面开始说删除功能
删除是根据id删除,只需要修改sql语句即可
//修改
//1.update tb_brand
// set brand_name = ?,
// company_name = ?,
// ordered = ?,
// status = ?,
//品牌数据的增删改查
public class BrandTest {
@Test
public void testAdd() throws Exception {
// 1。 接受页面提交的参数
String brandName = "香飘飘";
String companyName = "香飘飘";
int ordered = 1;
String description = "地球一圈";
int status = 1;
// 1,获取conneiton
// 3.加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("C:\\Users\\lengxin\\IdeaProjects\\jdbc-demo\\lib\\druid.properties"));
// 4.获取连接池数据对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
// 5.获取数据库链接
Connection conn = dataSource.getConnection();
// 2.定义sql语句
// String sql ="select * from tb_brand";
String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
// 3.获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4。设置参数
pstmt.setString(1, brandName);
pstmt.setString(2, companyName);
pstmt.setInt(3, ordered);
pstmt.setString(4, description);
pstmt.setInt(5, status);
// 5.执行sql
int count = pstmt.executeUpdate();
// 6.处理结果
System.out.println(count > 0);
// 7.释放资源
pstmt.close();
conn.close();
}
@Test
public void testUpdate() throws Exception {
// 1。 接受页面提交的参数
String brandName = "香飘飘";
String companyName = "香飘飘";
int ordered = 1000;
String description = "地球三圈";
int status = 1;
int id = 4;
// 1,获取conneiton
// 3.加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("C:\\Users\\lengxin\\IdeaProjects\\jdbc-demo\\lib\\druid.properties"));
// 4.获取连接池数据对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
// 5.获取数据库链接
Connection conn = dataSource.getConnection();
// 2.定义sql语句
// String sql ="select * from tb_brand";
String sql = "update tb_brand\n" +
"set brand_name = ?,\n" +
"company_name = ?,\n" +
"ordered = ?,\n" +
"description = ?,\n" +
"status = ?\n" +
"where id = ?";
// 3.获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4。设置参数
pstmt.setString(1, brandName);
pstmt.setString(2, companyName);
pstmt.setInt(3, ordered);
pstmt.setString(4, description);
pstmt.setInt(5, status);
pstmt.setInt(6, id);
// 5.执行sql
int count = pstmt.executeUpdate();
// 6.处理结果
System.out.println(count > 0);
// 7.释放资源
pstmt.close();
conn.close();
}
@Test
public void testDeleteByid() throws Exception {
// 1。 接受页面提交的参数
int id = 4;
// 1,获取conneiton
// 3.加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("C:\\Users\\lengxin\\IdeaProjects\\jdbc-demo\\lib\\druid.properties"));
// 4.获取连接池数据对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
// 5.获取数据库链接
Connection conn = dataSource.getConnection();
// 2.定义sql语句
// String sql ="select * from tb_brand";
String sql = "delete from tb_brand where id = ?";
// 3.获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4。设置参数
pstmt.setInt(1, id);
// 5.执行sql
int count = pstmt.executeUpdate();
// 6.处理结果
System.out.println(count > 0);
// 7.释放资源
pstmt.close();
conn.close();
}
}
## 总结:增删改查基于3步
1.想要通过sql语句进行什么操作
2.需要用什么参数
3.要得到什么样的结果