dbutils入门
2013-06-07 23:37
120 查看
dbutils入门
DButils隶属于apache commons,对于一些基本的jdbc操作进行了封装,比之orm要小巧不小,当然功能上弱化很多。
简单demo看看dbutils使用(增删改查):
Java代码
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();
}
}
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表结构:
Java代码
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;
DROP TABLE IF EXISTS`test`.`aaa`;
CREATE TABLE `test`.`aaa` (
`id` int(10) unsigned NOT NULLAUTO_INCREMENT,
`term` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDBAUTO_INCREMENT=47 DEFAULT CHARSET=utf8;
除dbutils.jar外还需要mysql-java的驱动包。
DButils隶属于apache commons,对于一些基本的jdbc操作进行了封装,比之orm要小巧不小,当然功能上弱化很多。
简单demo看看dbutils使用(增删改查):
Java代码
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();
}
}
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表结构:
Java代码
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;
DROP TABLE IF EXISTS`test`.`aaa`;
CREATE TABLE `test`.`aaa` (
`id` int(10) unsigned NOT NULLAUTO_INCREMENT,
`term` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDBAUTO_INCREMENT=47 DEFAULT CHARSET=utf8;
除dbutils.jar外还需要mysql-java的驱动包。
相关文章推荐
- 简单的三层框架以及使用dbutils进行数据库操作(入门)
- 数据库实用小工具之-DBUtils简单入门
- 简单的三层框架以及使用dbutils进行数据库操作(入门)
- Dbutils入门及简单增删改查操作
- Apache DBUtils入门(一)
- DbUtils入门
- JDBC框架入门及DBUtils的入门
- Apache DBUtils入门(二)
- dbutils入门
- DbUtils入门
- DbUtils入门实例
- DBUtils超快速入门指南
- JavaWeb基础—dbutils的简单入门
- 简单的三层框架以及使用dbutils进行数据库操作(入门)
- DBUtils 入门
- Dbutils学习(介绍和入门)[转载]
- MySQL---数据库从入门走向大神系列(十五)-Apache的DBUtils框架使用
- Apache Commons DbUtils 入门
- JAVA 入门学习之路-apache-dbutils-BeanUtils使用详解。
- DBUtils学习(介绍和入门)