1. 配置tomcat下的conf下的context.xml文件,在<Context> </Context>之间添加连接池配置:
<Context>
<Resource name="jdbc/lhy" <--对应web.xml <res-ref-name> -->
auth="Container"
type="javax.sql.DataSource" <--对应web.xml <res-type> -->
maxActive="20"
maxIdel="10"
maxWait="1000"
username="root"
password="123456"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/lhy" >
</Resource>
</Context>
在项目的WebRoot->WEB-INF下的web.xml中<web-app>
之间配置
<web-app>
<resource-ref>
<description>DBConnection</description>
<res-ref-name>jdbc/lhy</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
配置dbutils
注意EmpBean里面定义的属性要和数据库表中的名字一样,
这样才会自动把数据库中的列匹配给相应的bean属性
数据库
实体类EmpBean
package bean;
public class EmpBean {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public voidsetName(String name) {
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age) {
this.age = age;
}
}
封装update,query方法代码类DbHelper
package until;
importjava.sql.SQLException;
importjavax.naming.Context;
importjavax.naming.InitialContext;
importjavax.naming.NamingException;
importjavax.sql.DataSource;
importorg.apache.commons.dbutils.QueryRunner;
importorg.apache.commons.dbutils.ResultSetHandler;
public class DbHelper{
public QueryRunnergetQueryRunner() {
QueryRunner qr = null;
try {
Context context = newInitialContext();
DataSource ds = (DataSource) context
.lookup("java:/comp/env/jdbc/lhy");
qr = newQueryRunner(ds);
context.close();
} catch(NamingException e) {
e.printStackTrace();
}
return qr;
}
public intupdate(String sql) {
int result =0;
QueryRunner qr = this.getQueryRunner();
try {
result = qr.update(sql);
} catch(SQLException e) {
e.printStackTrace();
}
return result;
}
public intupdate(String sql,Object [] params) {
int result =0;
QueryRunner qr = this.getQueryRunner();
try {
result = qr.update(sql, params);
} catch(SQLException e) {
e.printStackTrace();
}
return result;
}
public Object query(Stringsql,ResultSetHandler rsh) {
Object list = null;
QueryRunner qr = this.getQueryRunner();
try {
list = qr.query(sql, rsh);
} catch (SQLExceptione) {
e.printStackTrace();
}
return list;
}
public Object query(Stringsql,Object [] params ,ResultSetHandler rsh) {
Object list = null;
QueryRunner qr = this.getQueryRunner();
try {
list = qr.query(sql,params, rsh);
} catch(SQLException e) {
e.printStackTrace();
}
return list;
}
}
数据库操作EmpDao
packagedao;
importjava.util.List;
importorg.apache.commons.dbutils.handlers.BeanListHandler;
importuntil.DbHelper;
importbean.EmpBean;
publicclass EmpDao {
String SEL_SQL="select * from empwhere id=? and age=?";
String INS_SQL="insert into emp(name,age) values(?,?)";
DbHelper db=new DbHelper();
public List <EmpBean> sel(intid,int age)
{
Object params[] = {id,age};
List<EmpBean>list=(List<EmpBean>) db.query(SEL_SQL, params, newBeanListHandler(EmpBean.class));
return list;
}
public int insertEmp(EmpBean emp)
{
Object params[] ={emp.getName(),emp.getAge()};
return db.update(INS_SQL, params);
}
}
测试类Test
importbean.EmpBean;
importdao.EmpDao;
publicclass Test {
public static void main(String[] args) {
EmpDao emp=new EmpDao();
// emp.sel(1, 20);
EmpBean eb=new EmpBean();
eb.setAge(20);
eb.setName("刘会要");
int i=emp.insertEmp(eb);
if(i>0)
System.out.println("添加成功");
else
System.out.println("添加失败");
}
}