栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDBC(增删改查)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JDBC(增删改查)

package org.oracle.jdbc.Goods;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BrandTest {
    public static void main(String[] args) throws SQLException {
        //sel();
        //add();
        //updata();
        del();

    }
    //查询
    public static void sel(){
        
        Connection conn  = null;
        Statement state = null;
        ResultSet rs = null;
        //加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //创建连接
             conn = DriverManager.getConnection("jdbc:mysql:///demo","root","123456");
            //statement 发送sql
             state = conn.createStatement();

             rs = state.executeQuery("SELECt * FROM tb_brand");
            //处理结果
            while (rs.next()){
              Integer id =  rs.getInt(1);
              String brand_name = rs.getString(2);
              String company_name = rs.getString(3);
              Integer ordered = rs.getInt(4);
              String description = rs.getString(5);
              Integer statuss = rs.getInt(6);
              Brand b = new Brand(id,brand_name,company_name,ordered,description,statuss);
                List list = new ArrayList<>();
                list.add(b);
                System.out.println(list);


            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{
                if(rs!=null){
                    rs.close();

                }
                if(state!=null){
                    state.close();
                }
                if(conn!=null){
                    conn.close();
                }

            }catch (Exception e){

            }
        }
    }
    //添加
    public  static void add() throws SQLException {
        
        //接受用户从前端传过来的参数
       String  brand_name  =" 香飘飘";
       String  company_name ="香飘飘官方旗舰店";
       Integer  ordered  =1;
       String  description= "好喝不上火";
       Integer  statuss = 1;
       PreparedStatement ps= null;
       Connection conn = null;
       //加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
             conn = DriverManager.getConnection("jdbc:mysql:///demo","root","123456");
              ps = conn.prepareStatement("INSERT INTO tb_brand(brand_name,company_name,ordered,description,statuss) VALUES(?,?,?,?,?);");
            ps.setString(1,brand_name);
            ps.setString(2,company_name);
            ps.setInt(3,ordered);
            ps.setString(4,description);
            ps.setInt(5,statuss);
           int row =  ps.executeUpdate();
            System.out.println(row>0);



        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            ps.close();
            conn.close();


        }


    }
    //修改
    public static void updata() throws SQLException {
        
        PreparedStatement state = null;
        Connection conn = null;
        try {


            
            //1、加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2、建立连接
             conn = DriverManager.getConnection("jdbc:mysql:///demo","root","123456");

            //3、发送sql
             state = conn.prepareStatement(" UPDATE tb_brand SET brand_name = ?,n" +
                    "                                company_name =?,n" +
                    "                                ordered = ?,n" +
                    "                                description  = ?,n" +
                    "                                statuss = ?n" +
                    "             WHERe id = ?;");
            state.setString(1,"香飘飘");
            state.setString(2,"香飘飘官方旗舰店");
            state.setInt(3,1000);
            state.setString(4,"好喝不上火");
            state.setInt(5,1);
            state.setInt(6,5);


            int row = state.executeUpdate();
            //4、处理结果
            System.out.println(row>0);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            state.close();
            conn.close();
        }

    }
    //删除
    public  static void del() throws SQLException {
        PreparedStatement pstate = null;
        Connection conn = null;
        //从用户页面接受id
        int id = 6;
        

        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //创建连接
             conn = DriverManager.getConnection("jdbc:mysql:///demo","root","123456");
            //发送sql statement
             pstate = conn.prepareStatement("DELETE FROM tb_brand WHERe id = ?;");
            pstate.setInt(1,id);

            int row = pstate.executeUpdate();
            //处理结果
            System.out.println(row>0);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //释放资源
            pstate.close();
            pstate.close();

        }
    }


    
}

为了减少代码量可将重复的代码封装在一个util包类

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678865.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号