2011-08-30 05:13
189人阅读
评论(3)
收藏
举报
package heima.shawn.utils; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public class BaseDao<T> { public Object opreatorObj(Class<T> tClazz, String sql, Object... params) throws Exception { Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; try { List<T> list = new ArrayList<T>(); conn = DBUtils.getInstance().getConnection(); pst = conn.prepareStatement(sql); if (params != null) { for (int i = 1; i <= params.length; i++) { pst.setObject(i, params[i - 1]); } } if (pst.execute()) { rs = pst.getResultSet(); while (rs.next()) { T t = resultSet2Bean(rs, tClazz); list.add(t); } return list; } else { int rows = pst.getUpdateCount(); if (rows > 0) return true; return false; } } finally { DBUtils.getInstance().release(conn, pst, rs); } } public T resultSet2Bean(ResultSet rs, Class<T> clazz) throws Exception { T t = clazz.newInstance(); PropertyDescriptor[] props = Introspector.getBeanInfo(clazz) .getPropertyDescriptors(); for (int i = 0; i < props.length; i++) { if (props[i].getName().equals("class")) continue; props[i].getWriteMethod().invoke(t, rs.getObject(props[i].getName())); } return t; } }
package heima.shawn.utils; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import com.mysql.jdbc.Driver; public class DBUtils { private Properties prop = new Properties(); private DBUtils() { try { prop.load(DBUtils.class.getClassLoader().getResourceAsStream( "DB.properties")); } catch (IOException e) { throw new RuntimeException(e); } } private static DBUtils instance = new DBUtils(); public static DBUtils getInstance() { return instance; } public Connection getConnection() { Connection conn = null; try { Class.forName(prop.getProperty("driver")); //Driver driver = new Driver(); conn = DriverManager.getConnection(prop.getProperty("url"), prop .getProperty("user"), prop.getProperty("password")); } catch (Exception e) { throw new RuntimeException(e); } return conn; } public void release(Connection conn, Statement st, ResultSet rs) { if (rs != null) try { rs.close(); } catch (SQLException e) { throw new RuntimeException(e); } if (st != null) try { st.close(); } catch (SQLException e) { throw new RuntimeException(e); } if (conn != null) try { conn.close(); } catch (SQLException e) { throw new RuntimeException(e); } } }
package heima.shawn.utils; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public class BaseDao<T> { public Object opreatorObj(Class<T> tClazz, String sql, Object... params) throws Exception { Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; try { List<T> list = new ArrayList<T>(); conn = DBUtils.getInstance().getConnection(); pst = conn.prepareStatement(sql); if (params != null) { for (int i = 1; i <= params.length; i++) { pst.setObject(i, params[i - 1]); } } if (pst.execute()) { rs = pst.getResultSet(); while (rs.next()) { T t = resultSet2Bean(rs, tClazz); list.add(t); } return list; } else { int rows = pst.getUpdateCount(); if (rows > 0) return true; return false; } } finally { DBUtils.getInstance().release(conn, pst, rs); } } public T resultSet2Bean(ResultSet rs, Class<T> clazz) throws Exception { T t = clazz.newInstance(); PropertyDescriptor[] props = Introspector.getBeanInfo(clazz) .getPropertyDescriptors(); for (int i = 0; i < props.length; i++) { if (props[i].getName().equals("class")) continue; props[i].getWriteMethod().invoke(t, rs.getObject(props[i].getName())); } return t; } }
package heima.shawn.utils; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import com.mysql.jdbc.Driver; public class DBUtils { private Properties prop = new Properties(); private DBUtils() { try { prop.load(DBUtils.class.getClassLoader().getResourceAsStream( "DB.properties")); } catch (IOException e) { throw new RuntimeException(e); } } private static DBUtils instance = new DBUtils(); public static DBUtils getInstance() { return instance; } public Connection getConnection() { Connection conn = null; try { Class.forName(prop.getProperty("driver")); //Driver driver = new Driver(); conn = DriverManager.getConnection(prop.getProperty("url"), prop .getProperty("user"), prop.getProperty("password")); } catch (Exception e) { throw new RuntimeException(e); } return conn; } public void release(Connection conn, Statement st, ResultSet rs) { if (rs != null) try { rs.close(); } catch (SQLException e) { throw new RuntimeException(e); } if (st != null) try { st.close(); } catch (SQLException e) { throw new RuntimeException(e); } if (conn != null) try { conn.close(); } catch (SQLException e) { throw new RuntimeException(e); } } }
- packageheima.shawn.utils;
- importjava.beans.Introspector;
- importjava.beans.PropertyDescriptor;
- importjava.sql.Connection;
- importjava.sql.PreparedStatement;
- importjava.sql.ResultSet;
- importjava.util.ArrayList;
- importjava.util.List;
- publicclassBaseDao<T>{
- publicObjectopreatorObj(Class<T>tClazz,Stringsql,Object...params)
- throwsException{
- Connectionconn=null;
- PreparedStatementpst=null;
- ResultSetrs=null;
- try{
- List<T>list=newArrayList<T>();
- conn=DBUtils.getInstance().getConnection();
- pst=conn.prepareStatement(sql);
- if(params!=null){
- for(inti=1;i<=params.length;i++){
- pst.setObject(i,params[i-1]);
- }
- }
- if(pst.execute()){
- rs=pst.getResultSet();
- while(rs.next()){
- Tt=resultSet2Bean(rs,tClazz);
- list.add(t);
- }
- returnlist;
- }else{
- introws=pst.getUpdateCount();
- if(rows>0)
- returntrue;
- returnfalse;
- }
- }finally{
- DBUtils.getInstance().release(conn,pst,rs);
- }
- }
- publicTresultSet2Bean(ResultSetrs,Class<T>clazz)throwsException{
- Tt=clazz.newInstance();
- PropertyDescriptor[]props=Introspector.getBeanInfo(clazz)
- .getPropertyDescriptors();
- for(inti=0;i<props.length;i++){
- if(props[i].getName().equals("class"))
- continue;
- props[i].getWriteMethod().invoke(t,
- rs.getObject(props[i].getName()));
- }
- returnt;
- }
- }
- packageheima.shawn.utils;
- importjava.io.IOException;
- importjava.sql.Connection;
- importjava.sql.DriverManager;
- importjava.sql.ResultSet;
- importjava.sql.SQLException;
- importjava.sql.Statement;
- importjava.util.Properties;
- importcom.mysql.jdbc.Driver;
- publicclassDBUtils{
- privatePropertiesprop=newProperties();
- privateDBUtils(){
- try{
- prop.load(DBUtils.class.getClassLoader().getResourceAsStream(
- "DB.properties"));
- }catch(IOExceptione){
- thrownewRuntimeException(e);
- }
- }
- privatestaticDBUtilsinstance=newDBUtils();
- publicstaticDBUtilsgetInstance(){
- returninstance;
- }
- publicConnectiongetConnection(){
- Connectionconn=null;
- try{
- Class.forName(prop.getProperty("driver"));
- //Driverdriver=newDriver();
- conn=DriverManager.getConnection(prop.getProperty("url"),prop
- .getProperty("user"),prop.getProperty("password"));
- }catch(Exceptione){
- thrownewRuntimeException(e);
- }
- returnconn;
- }
- publicvoidrelease(Connectionconn,Statementst,ResultSetrs){
- if(rs!=null)
- try{
- rs.close();
- }catch(SQLExceptione){
- thrownewRuntimeException(e);
- }
- if(st!=null)
- try{
- st.close();
- }catch(SQLExceptione){
- thrownewRuntimeException(e);
- }
- if(conn!=null)
- try{
- conn.close();
- }catch(SQLExceptione){
- thrownewRuntimeException(e);
- }
- }
- }
还能改进,,,但是已经觉得比hibernate好用了!!!你懂的·········
- packageheima.shawn.utils;
- importjava.beans.Introspector;
- importjava.beans.PropertyDescriptor;
- importjava.sql.Connection;
- importjava.sql.PreparedStatement;
- importjava.sql.ResultSet;
- importjava.util.ArrayList;
- importjava.util.List;
- publicclassBaseDao<T>{
- publicObjectopreatorObj(Class<T>tClazz,Stringsql,Object...params)
- throwsException{
- Connectionconn=null;
- PreparedStatementpst=null;
- ResultSetrs=null;
- try{
- List<T>list=newArrayList<T>();
- conn=DBUtils.getInstance().getConnection();
- pst=conn.prepareStatement(sql);
- if(params!=null){
- for(inti=1;i<=params.length;i++){
- pst.setObject(i,params[i-1]);
- }
- }
- if(pst.execute()){
- rs=pst.getResultSet();
- while(rs.next()){
- Tt=resultSet2Bean(rs,tClazz);
- list.add(t);
- }
- returnlist;
- }else{
- introws=pst.getUpdateCount();
- if(rows>0)
- returntrue;
- returnfalse;
- }
- }finally{
- DBUtils.getInstance().release(conn,pst,rs);
- }
- }
- publicTresultSet2Bean(ResultSetrs,Class<T>clazz)throwsException{
- Tt=clazz.newInstance();
- PropertyDescriptor[]props=Introspector.getBeanInfo(clazz)
- .getPropertyDescriptors();
- for(inti=0;i<props.length;i++){
- if(props[i].getName().equals("class"))
- continue;
- props[i].getWriteMethod().invoke(t,
- rs.getObject(props[i].getName()));
- }
- returnt;
- }
- }
- packageheima.shawn.utils;
- importjava.io.IOException;
- importjava.sql.Connection;
- importjava.sql.DriverManager;
- importjava.sql.ResultSet;
- importjava.sql.SQLException;
- importjava.sql.Statement;
- importjava.util.Properties;
- importcom.mysql.jdbc.Driver;
- publicclassDBUtils{
- privatePropertiesprop=newProperties();
- privateDBUtils(){
- try{
- prop.load(DBUtils.class.getClassLoader().getResourceAsStream(
- "DB.properties"));
- }catch(IOExceptione){
- thrownewRuntimeException(e);
- }
- }
- privatestaticDBUtilsinstance=newDBUtils();
- publicstaticDBUtilsgetInstance(){
- returninstance;
- }
- publicConnectiongetConnection(){
- Connectionconn=null;
- try{
- Class.forName(prop.getProperty("driver"));
- //Driverdriver=newDriver();
- conn=DriverManager.getConnection(prop.getProperty("url"),prop
- .getProperty("user"),prop.getProperty("password"));
- }catch(Exceptione){
- thrownewRuntimeException(e);
- }
- returnconn;
- }
- publicvoidrelease(Connectionconn,Statementst,ResultSetrs){
- if(rs!=null)
- try{
- rs.close();
- }catch(SQLExceptione){
- thrownewRuntimeException(e);
- }
- if(st!=null)
- try{
- st.close();
- }catch(SQLExceptione){
- thrownewRuntimeException(e);
- }
- if(conn!=null)
- try{
- conn.close();
- }catch(SQLExceptione){
- thrownewRuntimeException(e);
- }
- }
- }
还能改进,,,但是已经觉得比hibernate好用了!!!你懂的·········