欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

model层之dao--SqlHelper

程序员文章站 2022-12-01 18:38:57
贴一下SqlHelper的代码:  001 /**  002  * 功能:对数据库的操作的封装  003  */ 004&...

贴一下SqlHelper的代码:

 001 /** 

002  * 功能:对数据库的操作的封装 

003  */

004   

005 package cn.zwh.util; 

006   

007 import java.sql.*; 

008 import java.util.*; 

009 import java.io.*; 

010   

011 public class SqlHelper { 

012     //操作数据库必备 

013     private static Connection ct = null; 

014     private static PreparedStatement ps = null; 

015     private static ResultSet rs = null; 

016       

017     //一些常量 

018     private static String url = ""; 

019     private static String username = ""; 

020     private static String password = ""; 

021     private static String driver = ""; 

022       

023     private static Properties pp = null; 

024     //private static FileInputStream fis = null; 

025     private static InputStream is = null; 

026       

027     //静态,只执行一次 

028     static{ 

029         try { 

030             pp = new Properties(); 

031             //默认读取的主目录是在tomcat的主目录的bin目录下; 

032             //fis = new FileInputStream("dbinfo.properties"); 

033             //默认读取的主目录是在src目录下; 

034             is = SqlHelper.class.getClassLoader().getResourceAsStream("dbinfo.properties"); 

035             pp.load(is); 

036             //把一些常量搞定一下 

037             url = pp.getProperty("url"); 

038             username = pp.getProperty("username"); 

039             password = pp.getProperty("password"); 

040             driver = pp.getProperty("driver"); 

041               

042             //加载驱动 

043             Class.forName(driver); 

044         } catch (Exception e) { 

045             e.printStackTrace(); 

046             // TODO: handle exception 

047         }finally{ 

048             //关闭资源 

049 //          if(fis != null){ 

050 //              try { 

051 //                  fis.close(); 

052 //              } catch (IOException e) { 

053 //                  // TODO Auto-generated catch block 

054 //                  e.printStackTrace(); 

055 //              } 

056 //              fis = null; 

057 //          } 

058             if(is != null){ 

059                 try { 

060                     is.close(); 

061                 } catch (IOException e) { 

062                     // TODO Auto-generated catch block 

063                     e.printStackTrace(); 

064                 } 

065                 is = null; 

066             } 

067         } 

068     } 

069       

070     //得到连接 

071     public static Connection getConnection() 

072     { 

073         try { 

074             ct = DriverManager.getConnection(url, username, password); 

075         } catch (Exception e) { 

076             e.printStackTrace(); 

077             // TODO: handle exception 

078         }finally{ 

079             //关闭资源 

080             //由调用者自己关闭 

081         } 

082         return ct; 

083     } 

084       

085     //提供查询 

086     public static ResultSet executeQuery(String sql, String parameters[]) 

087     { 

088         try { 

089             //得到连接 

090             ct = getConnection(); 

091             //执行查询语句 

092             ps = ct.prepareStatement(sql); 

093             if(parameters != null) 

094             { 

095                 for(int i = 0; i<parameters.length;i++) 

096                 { 

097                     ps.setString(i+1, parameters[i]); 

098                 } 

099             } 

100               

101             rs = ps.executeQuery(); 

102         } catch (Exception e) { 

103             e.printStackTrace(); 

104             // TODO: handle exception 

105         }finally{ 

106             //关闭资源 

107             //自己关闭 

108         } 

109         return rs; 

110     } 

111       

112     //提供简单的增,删,改 

113     public static void executeUpdate(String sql, String parameters[]) 

114     { 

115         try { 

116             ct = getConnection(); 

117             ps = ct.prepareStatement(sql); 

118             if(parameters != null) 

119             { 

120                 for(int i = 0; i<parameters.length;i++) 

121                 { 

122                     ps.setString(i+1, parameters[i]); 

123                 } 

124             } 

125             ps.executeUpdate(); 

126         } catch (Exception e) { 

127             e.printStackTrace(); 

128             // TODO: handle exception 

129         }finally{ 

130             //关闭资源 

131         } 

132     } 

133       

134     //提供关闭资源的方法 

135     public static void close(ResultSet rs, PreparedStatement ps, Connection ct) 

136     { 

137         if(rs != null) 

138         { 

139             try { 

140                 rs.close(); 

141             } catch (Exception e) { 

142                 // TODO Auto-generated catch block 

143                 e.printStackTrace(); 

144             } 

145             rs = null; 

146         } 

147           

148         if(ps != null) 

149         { 

150             try { 

151                 ps.close(); 

152             } catch (SQLException e) { 

153                 // TODO Auto-generated catch block 

154                 e.printStackTrace(); 

155             } 

156             ps = null; 

157         } 

158           

159         if(ct != null) 

160         { 

161             try { 

162                 ct.close(); 

163             } catch (SQLException e) { 

164                 // TODO Auto-generated catch block 

165                 e.printStackTrace(); 

166             } 

167             ct = null; 

168         } 

169     } 

170   

171     //得到这里面的ct, ps, rs 

172     public static Connection getCt() { 

173         return ct; 

174     } 

175   

176     public static void setCt(Connection ct) { 

177         SqlHelper.ct = ct; 

178     } 

179   

180     public static PreparedStatement getPs() { 

181         return ps; 

182     } 

183   

184     public static void setPs(PreparedStatement ps) { 

185         SqlHelper.ps = ps; 

186     } 

187   

188     public static ResultSet getRs() { 

189         return rs; 

190     } 

191   

192     public static void setRs(ResultSet rs) { 

193         SqlHelper.rs = rs; 

194     } 

195       

196       

197 }


dbinfo.properties文件的代码:

连接oracle的如下:  www.2cto.com

driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:oraclezw
username=*
password=*