1.自定义异常类
package com.ownhome.exception; /** * 自定义异常类 * @author admin * */ public class MyException extends Exception { protected Throwable throwable; /*构造方法*/ public MyException(String msg){ super(msg); } /*构造方法*/ public MyException(String msg,Throwable throwable){ super(msg); this.throwable=throwable; } // /*得到异常*/ // public Throwable getEx(){ // return this.throwable; // } /*重写父类方法*/ @Override public void printStackTrace() { super.printStackTrace();/*调用父类的方法*/ if(throwable!=null){ throwable.printStackTrace();/*输出异常*/ } } }
2.db属性文件
driverClass=sun.jdbc.odbc.JdbcOdbcDriver
url=jdbc:odbc:driver={Sql Server};database=ownHome;server=.
uid=sa
pwd=abc
3. MyProperties类[单例模式]
package com.ownhome.common; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import com.ownhome.exception.MyException; /** * 用单模式实现从Properties文件里读取连接数据库的参数 * @author admin * */ public class MyProperties extends Properties{ /*静态属性*/ private static MyProperties myPro; /*构造方法*/ private MyProperties() throws MyException{ InputStream is=this.getClass().getResourceAsStream("db.properties"); try { this.load(is); } catch (IOException e) { throw new MyException("读取属性文件错误 !",e); } } /*公有的静态方法,得到MyProperties对象*/ public static MyProperties getPro(){ if(myPro!=null){ return myPro; } init(); return myPro; } /*实现同步机制,初始化内部属性*/ private synchronized static void init(){ if(myPro==null){ try{ myPro=new MyProperties(); }catch(MyException e){ e.printStackTrace(); } } } }
4.ConDB连接工厂类[工厂模式]
package com.ownhome.common; import java.sql.Connection; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import com.ownhome.exception.MyException; /** * 连接数据库类 * @author admin * */ public class ConDB { // /*静态方法,属性文件得到连接对象 */ // public static Connection getCon() throws MyException{ // // MyProperties myPro=MyProperties.getPro(); // try{ // Class.forName(myPro.getProperty("driverClass")); // return DriverManager.getConnection(myPro.getProperty("url")); // }catch(ClassNotFoundException e){ // throw new MyException("注册驱动错误!",e); // }catch(SQLException e){ // throw new MyException("得到Con连接对象错误",e); // } // } /*JNDI方式*/ public static Connection getCon() throws MyException{ try { Context ic=new InitialContext(); DataSource source=(DataSource)ic.lookup("java:comp/env/jdbc/ownhome"); return source.getConnection(); } catch (NamingException e) { throw new MyException("JNDI查找错误@!",e); } catch (SQLException e) { throw new MyException("得到Connection对象错误!",e); } } }
5. Dao类工具类
package com.ownhome.common; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.jsp.jstl.sql.Result; import javax.servlet.jsp.jstl.sql.ResultSupport; import com.ownhome.exception.MyException; /** * Dao类,对数据库进行操作 * @author admin * */ public class Dao { private Connection con; private String sql; private List values; public void setCon(Connection con) { this.con = con; } public void setSql(String sql) { this.sql = sql; } /** * 设置sql字符串中?的值 * @param values */ public void setValues(List values) { this.values = values; } /*为sql语句的所有的?设置值*/ private void setValues(PreparedStatement ps) throws MyException { if (values != null) { try { for (int i = 1; i <= values.size(); i++) { ps.setObject(i, values.get(i - 1)); } } catch (SQLException e) { throw new MyException("设置sql中参数的值?时出错", e); } } } /*决断必需的属性是否为空*/ private boolean isNull(){ if(con==null||sql==null||sql.equals("")){ return true; } return false; } /** * 查询 * @return */ public Result executeQuery() throws MyException{ ResultSet rs; Result result = null; if(isNull()){ throw new MyException("未初始化Con或Sql属性!"); } try{ PreparedStatement ps=con.prepareStatement(sql); this.setValues(ps); rs=ps.executeQuery(); result=ResultSupport.toResult(rs); con.close(); }catch(SQLException e){ throw new MyException("用PreparedStatement执行查询时出错",e); }catch(MyException e){ e.printStackTrace(); } return result; } /** * 修改,删除,增加 * @return */ public int executeUpdate() throws MyException{ int count=0; if(isNull()){ throw new MyException("未初始化Con或Sql属性"); } try{ PreparedStatement ps=con.prepareStatement(sql); this.setValues(ps); count = ps.executeUpdate(); con.close(); }catch(SQLException e){ throw new MyException("用PreparedStatement执行更新时出错!",e); }catch(MyException e){ e.printStackTrace(); } return count; } }