DbUtils类的添加,修改,删除

Posted 代我向西贝问好

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DbUtils类的添加,修改,删除相关的知识,希望对你有一定的参考价值。

package cn.jy.demo;

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

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

 

public class queryrunner {
private static Connection con=JDBCUtilsConfig.getConnection();
public static void main(String[] args) throws SQLException {
//insert();
//update();
//delete();
//insert1();
insert2();
}
public static void delete() throws SQLException{
QueryRunner qr=new QueryRunner();
String sql="DELETE FROM zhangwu1 WHERE id=?";


int row= qr.update(con,sql,14);
System.out.println(row);
DbUtils.close(con);
}

public static void update() throws SQLException{
QueryRunner qr=new QueryRunner();
String sql="UPDATE zhangwu1 SET flname=?,money=?,zhanghu=?,date=?,description=?WHERE id=?";
Object[] param={"杨修","5000","4402","996-2","太差劲","2"};
int row= qr.update(con,sql,param);
System.out.println(row);
DbUtils.close(con);
}
public static void insert() throws SQLException{
QueryRunner qr = new QueryRunner();
String sql="INSERT INTO zhangwu1(flname,money,zhanghu,DATE,description)VALUES(?,?,?,?,?)";
Object[] obt={"新垣结衣","90000","4401","1980-2","非常优秀"};
int row =qr.update(con,sql,obt);
System.out.println(row);
DbUtils.close(con);
}
public static void insert1() throws SQLException{
QueryRunner qr = new QueryRunner();
String sql="INSERT INTO zhangwu1(flname,money,zhanghu,date,description)values(?,?,?,?,?)";
Object[] obt={"石原里美","90000","4402","1980-9","非常优秀"};
int row= qr.update(con,sql,obt);
System.out.println(row);
DbUtils.close(con);
}
public static void insert2() throws SQLException{
QueryRunner qr = new QueryRunner();
String sql="INSERT INTO zhangwu1(flname,money,zhanghu,date,description)values(?,?,?,?,?)";
Object[] obt={"李世民","9000000","4402","880-9","及其优秀"};
int row= qr.update(con,sql,obt);
System.out.println(row);
DbUtils.close(con);
}
}

以上是关于DbUtils类的添加,修改,删除的主要内容,如果未能解决你的问题,请参考以下文章

DBUtils+druid基本使用

DBUtils框架的使用(下)

Dbutils工具类的使用

DBUtils和连接池

java学习日记(day30--dbutils)

dbutils基本使用