Java利用QueryRunner类的update方法增加 删除 修改数据库中的数据
package queryRunnerTest;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import jdbcutil.JDBCutils;
public class Test {
private static Connection con = JDBCutils.getConnection();
public static void main(String[] args) throws SQLException {
// insert();
// set();
delete();
}
// 利用QueryRunner的update方法插入数据
public static void insert() throws SQLException {
QueryRunner qr = new QueryRunner();
String sql = "insert into sort (sname,sprice,sdesc) values(?,?,?)";
Object[] params = { "QQ飞车", 5000, "S车" };
int row = qr.update(con, sql, params);
System.out.println(row);
DbUtils.closeQuietly(con);
}
// 利用QueryRunner的update方法修改数据
public static void set() throws SQLException {
QueryRunner qr = new QueryRunner();
String sql = "update sort set sid=? where sid=?";
Object[] param = { 11, 14 };
int row = qr.update(con, sql, param);
System.out.println(row);
DbUtils.closeQuietly(con);
}
// 利用QueryRunner的update方法删除数据
public static void delete() throws SQLException {
QueryRunner qr = new QueryRunner();
String sql = "delete from sort where sid=?";
int param = 13;
int row = qr.update(con, sql, param);
System.out.println(row);
DbUtils.closeQuietly(con);
}
}