JavaWeb- (JDBC编程进阶2)

作者:湖北诺贝特科技有限公司  来源:www.nbt158.com未知  发布时间:2017-09-06 09:41:03
JavaWeb- (JDBC编程进阶2) JDBC编程进阶2 一、自定义框架

习惯:搞工具类尽量写多些注释

开始制定框架

项目架构:

c3p0-config.xml

com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/day15 root root 5 10 5 20 com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/day15 root root 5 10 5 20 com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/day16 root root 5 10 5 20

Account.java

package com.itheima.domain; import java.io.Serializable; public class Account implements Serializable { private int id; private String name ; private float money; public Account(int id, String name, float money) { super(); this.id = id; this.name = name; this.money = money; } public Account() { super(); } @Override public String toString() { return Account [id= + id + , name= + name + , money= + money + ]; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getMoney() { return money; } public void setMoney(float money) { this.money = money; } }

AccountDao.java

package com.itheima.dao; import java.util.List; import com.itheima.domain.Account; public interface AccountDao { public void addAccount(Account ac); public Account getAccountById(int id); public List findAll(); }

AccountDaoImpl.java

package com.itheima.dao.impl; import java.util.List; import com.itheima.dao.AccountDao; import com.itheima.dbassist.BeanHandler; import com.itheima.dbassist.BeanListHandler; import com.itheima.dbassist.DBAssist; import com.itheima.domain.Account; import com.itheima.utils.C3P0Util; public class AccountDaoImpl implements AccountDao { private DBAssist db = new DBAssist(C3P0Util.getDataSource()); public void addAccount(Account ac) { db.update(insert into account values(?,?,?),new Object[]{ac.getId(),ac.getName(),ac.getMoney()}); } @Override public Account getAccountById(int id) { return (Account)db.query(select * from account where id=?, new BeanHandler(Account.class), new Object[]{id}); } @Override public List findAll() { return (List)db.query(select * from account, new BeanListHandler(Account.class), null); } }

C3P0Util.java

package com.itheima.utils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0Util { private static DataSource ds = new ComboPooledDataSource(mysql); /** * 获取连接 * @return * @throws Exception */ public static Connection getConnection() throws Exception{ return ds.getConnection(); } /** * 获取数据源 * @return */ public static DataSource getDataSource(){ return ds; } /** * 关闭资源 * * @param rs * @param st * @param con */ public static void release(ResultSet rs, Statement st, Connection con) { // 6.关闭资源 if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } if (st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } st = null; } if (con != null) { try { con.close();//不用担心了,肯定还回池中 } catch (SQLException e) { e.printStackTrace(); } con = null;//线程池中也要回收 } } }

DBAssist.java

package com.itheima.dbassist; import java.sql.Connection; import java.sql.ParameterMetaData; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.sql.DataSource; public class DBAssist { private DataSource ds; public DBAssist(DataSource ds) { super(); this.ds = ds; } /** * CUD操作 * @param sql * @param params */ public void update(String sql,Object []params){ Connection con = null; PreparedStatement st = null; try { con = ds.getConnection(); st = con.prepareStatement(sql); //1.得到参数的元数据 ParameterMetaData pmd =st.getParameterMetaData(); //2.得到参数的个数,?的个数 int count = pmd.getParameterCount(); if(count>0){ //说明有?号 if(params==null || params.length==0){ throw new IllegalArgumentException(参数不能为空); }else if(count!=params.length){ throw new IllegalArgumentException(参数个数不匹配); } //3.?号赋值 for (int i = 0; i < count; i++) { st.setObject(i+1, params[i]); } } st.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } public Object query(String sql,ResultSetHandler handler,Object params[]){ Connection con = null; PreparedStatement st = null; ResultSet rs = null; try { con = ds.getConnection(); st = con.prepareStatement(sql); //1.得到参数的元数据 ParameterMetaData pmd =st.getParameterMetaData(); //2.得到参数的个数,?的个数 int count = pmd.getParameterCount(); if(count>0){ //说明有?号 if(params==null || params.length==0){ throw new IllegalArgumentException(参数不能为空); }else if(count!=params.length){ throw new IllegalArgumentException(参数个数不匹配); } //3.?号赋值 for (int i = 0; i < count; i++) { st.setObject(i+1, params[i]); } } //4.执行查询 rs = st.executeQuery(); //5.分而治之思想 return handler.handle(rs); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(查询有误); } } }

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:咸宁网站建设 http://xianning.45qun.com


上一篇:apk签名命令总结
下一篇:最后一页