JDBC练习
环境准备
-- 删除tb_brand表
drop table if exists tb_brand;
-- 创建tb_brand表
create table tb_brand
(
-- id 主键
id int primary key auto_increment,
-- 品牌名称
brand_name varchar(20),
-- 企业名称
company_name varchar(20),
-- 排序字段
ordered int,
-- 描述信息
description varchar(100),
-- 状态:0:禁用 1:启用
status int
);
-- 添加数据
insert into tb_brand (brand_name, company_name, ordered, description, status)
values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),
('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),
('小米', '小米科技有限公司', 50, 'are you ok', 1);
SELECT * FROM tb_brand;
package pojo;
public class Brand {
private Integer id;
private String brandName;
private String companyName;
private Integer ordered;
private String description;
private Integer status;
public Brand() {}
public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, Integer status) {
this.id = id;
this.brandName = brandName;
this.companyName = companyName;
this.ordered = ordered;
this.description = description;
this.status = status;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Integer getOrdered() {
return ordered;
}
public void setOrdered(Integer ordered) {
this.ordered = ordered;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", brandName='" + brandName + '\'' +
", companyName='" + companyName + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
查询所有数据
![【Java】JDBC练习_第1张图片](http://img.e-com-net.com/image/info8/f16dbb81533c42d2b22c1bab2450238f.jpg)
package example;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import pojo.Brand;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class BrandTest {
public static void main(String[] args) throws Exception {
Properties prop=new Properties();
prop.load(new FileInputStream("C:\\Users\\Hayaizo\\IdeaProjects\\jdbc\\jdbc_test\\src\\druid.properties"));
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
Connection connection=dataSource.getConnection();
String sql="select * from tb_brand";
PreparedStatement pstmt=connection.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
List<Brand> list=new ArrayList<>();
while(rs.next()){
int id=rs.getInt("id");
String brandName=rs.getString("brand_name");
String companyName=rs.getString("company_name");
int ordered=rs.getInt("ordered");
String description=rs.getString("description");
int status=rs.getInt("status");
Brand brand=new Brand();
brand.setId(id);
brand.setBrandName(brandName);
brand.setCompanyName(companyName);
brand.setOrdered(ordered);
brand.setDescription(description);
brand.setStatus(status);
list.add(brand);
}
System.out.println(list);
rs.close();
pstmt.close();
connection.close();
}
}
添加&修改&删除
package example;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import pojo.Brand;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class BrandTest {
public static void main(String[] args) throws Exception {
Properties prop=new Properties();
prop.load(new FileInputStream("C:\\Users\\Hayaizo\\IdeaProjects\\jdbc\\jdbc_test\\src\\druid.properties"));
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
Connection connection=dataSource.getConnection();
connection.setAutoCommit(false);
String sql="insert into tb_brand(brand_name,company_name,ordered,description,status) values (?,?,?,?,?)";
String brand_name="香飘飘1";
String company_name="香飘飘1";
int ordered=10;
String description="香飘飘企业1";
int status=0;
PreparedStatement pstmt=connection.prepareStatement(sql);
pstmt.setString(1,brand_name);
pstmt.setString(2,company_name);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
int res= 0;
try {
res = pstmt.executeUpdate();
connection.commit();
} catch (Exception e) {
connection.rollback();
throw new RuntimeException(e);
}
if(res!=0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
pstmt.close();
connection.close();
}
}
![【Java】JDBC练习_第2张图片](http://img.e-com-net.com/image/info8/d6275fdf6a314c1f81665ce4abf4cec5.png)