当前位置:数据库 > SQLServer >>

model层之dao--SqlHelper

贴一下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            

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,