JDBC-使用preparedstatement实现增删改查crud

CRUD即为增删改查

增加的代码实现

public class updatreTest {
    public static void main(String[] args) 
            throws SQLException, IOException, ClassNotFoundException, ParseException {

        Connection conn = null;
        PreparedStatement  ps = null;
        try {//ctrl+alt+t 自动创建try-catch语句
            conn = getconnection();

            //预编译sql语句,返回preparedstatement实例,这里面插入的值用占位符表示
            String sql = "insert into customers(name,email,birth) values(?,?,?)";
            ps = conn.prepareStatement(sql);

            //填充占位符
            ps.setString(1,"哪吒");
            ps.setString(2,"nezha@gmail.com");

            SimpleDateFormat si = new SimpleDateFormat("yyyy-MM-dd");
            Date date = si.parse("1000-01-01");
            ps.setDate(3,new java.sql.Date(date.getTime()));

            //执行sql
            ps.execute();
        } 
        
        catch (Exception e) {
            e.printStackTrace();
        }  
        
        finally {
            //资源关闭
            try {
                if(ps!=null) {ps.close();}
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                if(conn!=null) {conn.close();}
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


    //获取连接
    public static Connection getconnection() throws IOException, ClassNotFoundException, SQLException {
        InputStream res = ClassLoader.getSystemClassLoader()   //获取系统类加载器
                .getResourceAsStream("jdbc.properties");

        Properties pros = new Properties();
        pros.load(res);

        String drivers = pros.getProperty("driverclass");
        String url = pros.getProperty("url");
        String user = pros.getProperty("user");
        String password =pros.getProperty("password");

        Class cl = Class.forName(drivers);

        Connection conn = DriverManager.getConnection(url, user, password);

        return conn;

    }
}

修改记录的代码实现:

public class updatreTest {
    public static void main(String[] args)
            throws SQLException, IOException, ClassNotFoundException, ParseException {


        PreparedStatement  ps = null;
        Connection conn = null;
        try {//ctrl+alt+t 自动创建try-catch语句
            conn = getconnection();

            //预编译sql语句,返回preparedstatement实例,这里面插入的值用占位符表示
            String sql = "update customers set name = ? where id = ?";
            ps = conn.prepareStatement(sql);

            //填充占位符,此处统一使用object
            ps.setObject(1,"莫扎特");
            ps.setObject(2,18);
            

            //执行sql
            ps.execute();
        }

        catch (Exception e) {
            e.printStackTrace();
        }

        finally {
            //资源关闭
            closeresource(conn, ps);
        }
    }


    //获取连接
    public static Connection getconnection() throws IOException, ClassNotFoundException, SQLException {
        InputStream res = ClassLoader.getSystemClassLoader()   //获取系统类加载器
                .getResourceAsStream("jdbc.properties");

        Properties pros = new Properties();
        pros.load(res);

        String drivers = pros.getProperty("driverclass");
        String url = pros.getProperty("url");
        String user = pros.getProperty("user");
        String password =pros.getProperty("password");

        Class cl = Class.forName(drivers);

        Connection conn = DriverManager.getConnection(url, user, password);

        return conn;

    }

    public static void closeresource(Connection conn, Statement ps){

        try{if(ps!=null) {ps.close();}
         } catch (SQLException throwables) {
        throwables.printStackTrace();
        }
            try {
        if(conn!=null) {conn.close();}
        } catch (SQLException throwables) {
        throwables.printStackTrace();
        }

    }
}

通用方法代码,即增删改

//通用的增改改操作的方法,即修改表
    //Object...args  表示可变个数的参数,类型统一为object,此处用来表示占位符,args表示一个数组
    public void update(String sql,Object...args) throws Exception {
        Connection conn = null;
        PreparedStatement ps = null;
        
        try {
            //1、获取连接
            conn = getconnection();
            //2、预编译
            ps = conn.prepareStatement(sql);
            //3、填充占位符
            for(int i =0;i<args.length;i++){
                ps.setObject(i+1,args[i]);//小心参数声明操作
            }

            //4、执行
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //5、关闭资源
            closeresource(conn, ps);
        } }
//获取连接
    public static Connection getconnection() throws IOException, ClassNotFoundException, SQLException {
        InputStream res = ClassLoader.getSystemClassLoader()   //获取系统类加载器
                .getResourceAsStream("jdbc.properties");

        Properties pros = new Properties();
        pros.load(res);

        String drivers = pros.getProperty("driverclass");
        String url = pros.getProperty("url");
        String user = pros.getProperty("user");
        String password =pros.getProperty("password");

        Class cl = Class.forName(drivers);

        Connection conn = DriverManager.getConnection(url, user, password);

        return conn;

    }

    public static void closeresource(Connection conn, Statement ps){

        try{if(ps!=null) {ps.close();}
         } catch (SQLException throwables) {
        throwables.printStackTrace();
        }
            try {
        if(conn!=null) {conn.close();}
        } catch (SQLException throwables) {
        throwables.printStackTrace();
        }

    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zero _s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值