JAVA-使用DbUtils增删改

package com.dxm.demo3;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;

import com.dxm.jdbcutil.JDBCUtilsConfig;

public class QueryRunnerDemo {
    static Connection con = JDBCUtilsConfig.getConnection();
    
    public static void insert() throws SQLException{
        QueryRunner qRunner= new QueryRunner();
        String sql = "insert into sort (sname,sprice,sdesc) values(?,?,?)";
        Object[] param = {"体育用品",289,"购买体育用品"};
        int row = qRunner.update(con, sql, param);
        System.out.println(row);
        DbUtils.closeQuietly(con);
    }
    
    public static void main(String[] args) throws SQLException {
        //insert();
        //update();
        delete();
    }
    
    public static void update() throws SQLException{
        QueryRunner qRunner= new QueryRunner();
        String sql = "update sort set sname=?,sprice=?,sdesc=? where sid=?";
        Object[] param = {"花卉",50,"请人节玫瑰花",4};
        int row = qRunner.update(con, sql, param);
        System.out.println(row);
        DbUtils.closeQuietly(con);
    }
    
    public static void delete() throws SQLException{
        QueryRunner qRunner= new QueryRunner();
        String sql = "delete from sort where sid=?";
        
        int row = qRunner.update(con, sql, 8);
        System.out.println(row);
        DbUtils.closeQuietly(con);
    }

 

你可能感兴趣的:(JAVA)