BrandTest
package example;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import pojo.Brand;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class BrandTest {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.load(new FileInputStream("111/src/druid.properties"));
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
//修改
String brandName = "德鲁伊";
String companyName = "zhengys";
int ordered = 1000;
String description = "zhengys学习中";
int status = 1;
int id = 1;
String sqlIUpdate = "update tb_brand set " +
"brand_name = ?," +
"company_name = ?," +
"ordered = ?," +
"description = ?," +
"status = ? " +
"where id = ?";
System.out.println(sqlIUpdate);
Connection connection1 = dataSource.getConnection();
PreparedStatement preparedStatement = connection1.prepareStatement(sqlIUpdate);
preparedStatement.setString(1,brandName);
preparedStatement.setString(2,companyName);
preparedStatement.setInt(3,ordered);
preparedStatement.setString(4,description);
preparedStatement.setInt(5,status);
preparedStatement.setInt(6,id);
int i1 = preparedStatement.executeUpdate();
System.out.println(i1 > 0);
preparedStatement.close();
connection1.close();
//查询
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
String sqlSelect = "select * from tb_brand";
ResultSet resultSet = statement.executeQuery(sqlSelect);
List<Brand> brands = new ArrayList<Brand>();
while(resultSet.next()) {
Brand brand = new Brand();
brand.setId(resultSet.getInt("id"));
brand.setBrandName(resultSet.getString("brand_name"));
brand.setCompanyName(resultSet.getString("company_name"));
brand.setOrdered(resultSet.getInt("ordered"));
brand.setDescription(resultSet.getString("description"));
brand.setStatus(resultSet.getInt("status"));
brands.add(brand);
}
resultSet.close();
statement.close();
connection.close();
for(Brand brand:brands){
System.out.println(brand);
}
}
}
Brand
package pojo;
public class Brand {
// id 主键
private Integer id;
// 品牌名称
private String brandName;
// 企业名称
private String companyName;
// 排序字段
private Integer ordered;
// 描述信息
private String description;
// 状态:0:禁用 1:启用
private Integer 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 +
'}';
}
}
druid.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///tb_brand?useSSL=false&useServerPrepStmts=true
username=root
password=123456
initialSize=5
maxActive=10
maxWait=3000