所需包的类
import Bean.Brand; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Properties;
在这三个的操作里面,只需要进行一些代码修改与演示。
这是行进行链接数据库链接池,需要提前导入德鲁伊的包
Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties")); DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); Connection conn = dataSource.getConnection();
定义sql语句,获取ptmt对象,设置参数,执行生sql语句
返回处理的结果,释放资源
package Dutils; import Bean.Brand; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.FileInputStream; import java.io.FileNotFoundException; 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 text { /** * 查询所有 * 1.sql: select *from tb_brand; * 2.参数设置 * 3.结果:List<brand> */ public void TextSelectall() throws Exception { //1.获取connect,调用druid连接池 Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties")); DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); Connection conn = dataSource.getConnection(); //2.定义sql语句 String sql = "select *from tb_brand;"; //3.获取pstmt对象 PreparedStatement pstmt = conn.prepareStatement(sql); //4.设置参数 //查询全部内容不需要设置参数 //5.执行SQL ResultSet rs = pstmt.executeQuery(); //6.处理结果List<brand> 封装brand,并且把它装载到list这个集合里面去 Brand brand = null; List<Brand> brands = new ArrayList<>(); while(rs.next()){ //获取数据 int Id = rs.getInt("id"); String BrandName = rs.getString("brand_name"); String CompanyName = rs.getString("company_name"); String Ordered = rs.getString("ordered"); String Description = rs.getString("description"); int Status = rs.getInt("status"); //封装Brand brand = new Brand(); brand.setId(Id); brand.setBrand_name(BrandName); brand.setCompany_name(CompanyName); brand.setOrdered(Ordered); brand.setDescription(Description); brand.setStatus(Status); //装载集合 brands.add(brand); } System.out.println(brands); //7.释放资源 rs.close(); pstmt.close(); conn.close(); } /** * 添加 * 1.sql: insert into tb_brand (brand_name,company_name,ordered,decription,status) values (?,?,?,?,?); * 2.需要参数,除了id以外所有id * 3.结果:boolean * @throws Exception */ public void textSelectadd() throws Exception { //接受页面提交的参数 String brandName = "劳力士"; String companyName = "劳力士1"; String ordered = "劳力士2"; String decription = "劳力士3"; int status = 0; //1.获取connect,调用druid连接池 Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties")); DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); Connection conn = dataSource.getConnection(); //2.定义sql语句 String sql = "insert into tb_brand (brand_name, company_name, ordered, decription, status) values (?,?,?,?,?);"; //3.获取pstmt对象 PreparedStatement pstmt = conn.prepareStatement(sql); //4.设置参数 pstmt.setString(1,brandName); pstmt.setString(2,companyName); pstmt.setString(3,ordered); pstmt.setString(4,decription); pstmt.setInt(5,status); //5.执行SQL int count = pstmt.executeUpdate();//count 是影响的行数 //6.处理的结果 System.out.println(count > 0);//当被影响的行数大于0,则返回true显示成功修改数据 //7.释放资源 pstmt.close(); conn.close(); } /** * 修改 * 1.sql: update tb_brand set brand_name = ? company_name = ? ordered = ? decription = ? status = ? where id = ?; * 2.需要参数:所有的数 */ public void textSelectupdate() throws Exception { //接受页面提交的参数 String brandName = "小米666"; String companyName = "小米666"; String ordered = "小米666"; String decription = "小米666"; int status = 0; int id = 2; //1.获取connect,调用druid连接池 Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties")); DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); Connection conn = dataSource.getConnection(); //2.定义sql语句 String sql = "update tb_brand set brand_name = ?\n" + " company_name = ?\n" + " ordered = ?\n" + " decription = ?\n" + " status = ?\n" + " where id = ?;"; //3.获取pstmt对象 PreparedStatement pstmt = conn.prepareStatement(sql); //4.设置参数 pstmt.setString(1,brandName); pstmt.setString(2,companyName); pstmt.setString(3,ordered); pstmt.setString(4,decription); pstmt.setInt(5,status); pstmt.setInt(6,id); //5.执行SQL int count = pstmt.executeUpdate();//count 是影响的行数 //6.处理的结果 System.out.println(count > 0);//当被影响的行数大于0,则返回true显示成功修改数据 //7.释放资源 pstmt.close(); conn.close(); } /** * 修改 * 1.sql:DELETE from tb_brand where id = ?; * 2.需要参数:id * 结果:Boolean */ public void TextSelectDeleteByid() throws Exception { //接受页面提交的参数 int id = 3; //1.获取connect,调用druid连接池 Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties")); DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); Connection conn = dataSource.getConnection(); //2.定义sql语句 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();//count 是影响的行数 //6.处理的结果 System.out.println(count > 0);//当被影响的行数大于0,则返回true显示成功修改数据 //7.释放资源 pstmt.close(); conn.close(); } }
标签:JDBC,java,String,删除,brand,添加,sql,import,pstmt From: https://www.cnblogs.com/yzx-sir/p/17168997.html