// TODO Auto-generatedconstructor stub
}
public DaoException(String message) {
super(message);
// TODO Auto-generatedconstructor stub
}
public DaoException(Throwablecause) {
super(cause);
// TODO Auto-generatedconstructor stub
}
public DaoException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generatedconstructor stub
}
}
在使用时
catch(SQLException e){
//异常的处理,转化为运行时异常
throw new DaoException(e.getMessage(),e);
}
在sql中SQLException是编译时异常,所以要抛出,但是如果只是向上抛出的话,会导致业务层和数据访问层的关系复杂,后期维护不便;因此最好的解决方法是在数据访问层中将异常catch住,编写异常类进行处理,在运行时如果上层能够处理就处理。从而使业务层和数据访问层耦合度减少。
5. 一个类的实例化的过程中首先执行的是静态代码块,然后是构造函数。
6.
事务的处理问题:
[java]
/**
* 事务处理范例
* 从账户1转账到账户2十元
*
*/
static void test() throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Savepoint sp = null;
try {
conn = JdbcUtils.getConnection();
// 3.创建语句
Conn.setAutoCommit(false);//设置自动提交无效
st = conn.createStatement();
// 4.执行语句
String sql = "updateuser set money=money-10 where id=1";
st.executeUpdate(sql);
//设置回滚点
sp =conn.setSavepoint();
sql = "update userset money=money+10 where id=3";
st.executeUpdate(sql);
sql = "selectmoney from user where id=2";
rs = st.executeQuery(sql);
float money = 0.0f;
// 5.处理结果
if (rs.next()) {// 按行来遍历
}
if(money > 300){
throw new RuntimeException("已经达到最大值!");
}
sql = "update userset money=money+10 where id=2";
st.executeUpdate(sql);
conn.commit();
} catch(RuntimeException e){
if(conn != null && sp != null)
//回滚到设置的点
conn.rollback(sp);
//递交,使以上操作有效
conn.commit();
throw e;
}catch(SQLException e){
if(conn != null)
conn.rollback();
throwe;
}finally {
JdbcUtils.free(rs, st, conn);
}
}
7. 跨越多个数据源的事务(JTA)实现分布式的数据处理,事务处理步骤:
1) 打开事务
2) 提交事务
3) 回滚事务
8. 在数据库中编写组件(函数),通过ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);//能够得到数据库中的组件。这样通过组件可以在用户向数据库中添加数据时,在返回时可以用组件对结果进行标识,说明数据已经在数据库中得到更新。
9. 批处理,对sql语句的打包。
10.
根据用户的查询需求进行查询,并将结果封装为Map返回www.2cto.com
[java]
public classResultSetMetaDataTest {
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
List