框架
框架分为:daomian、dao、utils、c3p0描述文件;(由于时间问题还未完毕,待更新!)
Domain层:
package com.hbsi.domain;
import java.util.Date;
public class LoadUser {
private String id;
//上传文件的名称,文件的uuid名
private String savename;
//上传文件的真实名称
private String realName;
//记住文件的位置
private String savepath;
//文件的上传时间
private Date uptime;
//文件的描述
private String description;
//上传人
private String username;
public LoadUser() {
super();
}
public LoadUser(String id, String savename, String realName,
String savepath, Date uptime, String description, String username) {
super();
this.id = id;
this.savename = savename;
this.realName = realName;
this.savepath = savepath;
this.uptime = uptime;
this.description = description;
this.username = username;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSavename() {
return savename;
}
public void setSavename(String savename) {
this.savename = savename;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getSavepath() {
return savepath;
}
public void setSavepath(String savepath) {
this.savepath = savepath;
}
public Date getUptime() {
return uptime;
}
public void setUptime(Date uptime) {
this.uptime = uptime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "LoadUser [id=" + id + ", savename=" + savename + ", realName="
+ realName + ", savepath=" + savepath + ", uptime=" + uptime
+ ", description=" + description + ", username=" + username
+ "]";
}
}
Dao层:
package com.hbsi.dao;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.hbsi.domain.User;
import com.hbsi.utils.DBManager_c3p0;
public class UserDaoImpl {
//用户插入一条记录
public void insert(){
QueryRunner runner=new QueryRunner(DBManager_c3p0.getDataSource());
String sql="insert into users(name,password) values('pp','1111')";
try {
runner.update(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void update() throws SQLException {
QueryRunner runner=new QueryRunner(DBManager_c3p0.getDataSource());
String sql="update users set name=?,password=?,email=?,where id=?";
Object[] param={"鸭蛋儿","222","1137659183@qq.com",1};
int i=runner.update(sql,param);
if(i>0){
System.out.println("修改成功");
}
}
//删,改、查询
public void find() throws Exception{
QueryRunner runner=new QueryRunner(DBManager_c3p0.getDataSource());
String sql="select * from users where is=?";
User user=runner.query(sql,new BeanHandler(User.class),1);
System.out.println(user.getId()+" "+user.getName());
}
//批处理
public void batch() throws SQLException{
QueryRunner runner=new QueryRunner(DBManager_c3p0.getDataSource());
String sql="insert into users(name,password) values (?,?)";
Object[][] params={{"111","111"},{"222","222"},{"333","333"}};
runner.batch(sql,params);
}
public void findall() throws SQLException{
QueryRunner runner=new QueryRunner(DBManager_c3p0.getDataSource());
String sql="select * from users";
List<User> list=runner.query(sql,new BeanListHandler(User.class));
System.out.println(Arrays.asList(list));
}
public static void main(String[] args){
try {
new UserDaoImpl().update();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Utils:
package com.hbsi.utils;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DBManager_c3p0 {
private static ComboPooledDataSource ds=null;
static {
ds=new ComboPooledDataSource("mysql");
}
public static DataSource getDataSource(){
return ds;
}
public static Connection getConnection() throws SQLException{
return ds.getConnection();
}
}
c3p0描述文件:
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property>
<property name="user">root</property>
<property name="password">042181</property>
<property name="automaticTestTable">con_test</property>
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<!-- This app is massive! -->
<named-config name="mysql">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property>
<property name="user">root</property>
<property name="password">042181</property>
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property>
</named-config>
</c3p0-config>