dbUtil的更新和查询
一、代码设计:
******************************实体类设计********************************
package dbutil;
/*
* 实体类设计
*/
public class Admin {
//属性
private int id;
private String userName;
private String pwd;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the pwd
*/
public String getPwd() {
return pwd;
}
/**
* @param pwd the pwd to set
*/
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "admin[id = "+id+",userName = "+userName+",pwd = "+pwd+"]";
}
}
/*
* 实体类设计
*/
public class Admin {
//属性
private int id;
private String userName;
private String pwd;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the pwd
*/
public String getPwd() {
return pwd;
}
/**
* @param pwd the pwd to set
*/
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "admin[id = "+id+",userName = "+userName+",pwd = "+pwd+"]";
}
}
***********************************dbUtil组件**********************************************
package dbutil;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;
public class App {
/* 使用dbutil组件更新*/
@Test
public void testUpdate() throws Exception{
//1.语句
String sql = "delete from admin where id = 27";
//2.连接数据库
Connection conn = JdbcUtil.getConnection();
//3.dbutil核心工具类
QueryRunner qr = new QueryRunner();
//4.执行
qr.update(conn, sql);
}
/*
* 使用dbutil组件查询
*/
@Test
public void testQuery() throws Exception{
String sql = "select * from admin";
//1.连接数据库
Connection conn = JdbcUtil.getConnection();
//2.创建核心工具类
QueryRunner qr = new QueryRunner();
//3.执行
List<Admin> list = qr.query(conn, sql, new BeanListHandler<Admin>(Admin.class));
//4.输出结果
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
conn.close();
}
}
/* 使用dbutil组件更新*/
@Test
public void testUpdate() throws Exception{
//1.语句
String sql = "delete from admin where id = 27";
//2.连接数据库
Connection conn = JdbcUtil.getConnection();
//3.dbutil核心工具类
QueryRunner qr = new QueryRunner();
//4.执行
qr.update(conn, sql);
}
/*
* 使用dbutil组件查询
*/
@Test
public void testQuery() throws Exception{
String sql = "select * from admin";
//1.连接数据库
Connection conn = JdbcUtil.getConnection();
//2.创建核心工具类
QueryRunner qr = new QueryRunner();
//3.执行
List<Admin> list = qr.query(conn, sql, new BeanListHandler<Admin>(Admin.class));
//4.输出结果
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
conn.close();
}
}
**********************************************jdbc**********************************************
package dbutil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtil {
//第一种方法: private String url = "jdbc:mysql://localhost:3306/day17";
private static String url = "jdbc:mysql:///day17";//第二种方法
private static String user = "root";
private static String password = "root";
//写成静态方法,用类名.getConnection()获取该连接对象
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(url, user, password);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 关闭
* 关闭原则:后开先关
*/
public static void closeAll(Connection con, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
rs = null; // 建议释放资源
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (con != null && !con.isClosed()) {
con.close();
con = null;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
* 关闭
* 关闭原则:后开先关
*/
public static void closeAll(Connection con, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
rs = null; // 建议释放资源
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (con != null && !con.isClosed()) {
con.close();
con = null;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
二、运行结果(1.删除数据库day17admin 表中id为27的数据 2.把数据库day17中的admin表中的数据全部输出):