dbutils入门

DButils隶属于apache commons,对于一些基本的jdbc操作进行了封装,比之orm要小巧不小,当然功能上弱化很多。

简单demo看看dbutils使用(增删改查):

 

public class DB {
	private String dirverClassName = "com.mysql.jdbc.Driver";
	private String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8";
	private String user = "root";
	private String password = "admin";
	static Connection conn = null;
	QueryRunner runner = null;

	private void getConnection() {
		try {
			Class.forName(dirverClassName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection(url, user, password);
			runner = new QueryRunner();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void insert() throws SQLException {
		int n = runner.update(conn, "insert into aaa(term) values('你大爷')");
		System.out.println("插入" + n + "条数据!");
	}

	private void find() throws SQLException {
		List<Word> list = (List<Word>) runner.query(conn,
				"select id,term from aaa", new BeanListHandler(Word.class));
		for (Word user : list) {
			System.out.println(user);
		}
	}

	private void delete() throws Exception {
		runner.update(conn, "delete from aaa where id = ?", 10);
	}

	public void test() throws Exception {
		getConnection();
		// insert();
		find();
		DbUtils.closeQuietly(conn);
	}

	public static void main(String[] args) throws Exception {
		DB db = new DB();
		db.test();
	}
}

  

 

 

 

aaa表结构:

 

DROP TABLE IF EXISTS `test`.`aaa`;
CREATE TABLE  `test`.`aaa` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `term` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8;

  

 

dbutils.jar外还需要mysql-java的驱动包。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值