Model layer dao -- SqlHelper

Source: Internet
Author: User

Paste the SqlHelper code:

001 /**

002 * function: encapsulate database operations

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 // database operations required

013 private static Connection ct = null;

014 private static PreparedStatement ps = null;

015 private static ResultSet rs = null;

016

017 // some constants

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 FCM = null;

025 private static InputStream is = null;

026

027 // static, only once

028 static {

029 try {

030 pp = new Properties ();

031 // by default, the main directory read is in the bin directory of the main directory of tomcat;

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

033 // The main directory read by default is under the src directory;

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

035 pp. load (is );

036 // fix some constants

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

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

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

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

041

042 // load the driver

043 Class. forName (driver );

044} catch (Exception e ){

045 e. printStackTrace ();

046 // TODO: handle exception

047} finally {

048 // close the resource

049 // if (FS! = Null ){

050 // try {

051 // FCM. close ();

052 //} catch (IOException e ){

053 // TODO Auto-generated catch block

054 // e. printStackTrace ();

055 //}

056 // FS = 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 // get the connection

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 // close the resource

080 // disabled by the caller

081}

082 return ct;

083}

084

085 // provide query

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

087 {

088 try {

089 // get the connection

090 ct = getConnection ();

091 // execute the query statement

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.exe cuteQuery ();

102} catch (Exception e ){

103 e. printStackTrace ();

104 // TODO: handle exception

105} finally {

106 // close the resource

107 // close it by yourself

108}

109 return rs;

110}

111

112 // simple addition, deletion, and modification

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.exe cuteUpdate ();

126} catch (Exception e ){

127 e. printStackTrace ();

128 // TODO: handle exception

129} finally {

130 // close the resource

131}

132}

133

134 // provides a method to disable Resources

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 // obtain 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 file code:

The connection to oracle is as follows: www.2cto.com

Driver = oracle. jdbc. driver. OracleDriver
Url = jdbc \: oracle \: thin \: @ fig \: 1521 \: oraclezw
Username = *
Password = *

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.