A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 王贺 于 2013-11-23 16:50 编辑

  1. <DIV id=cnblogs_post_body style="WORD-SPACING: 0px; FONT: 14px/28px 'ms shell dlg'; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); WORD-WRAP: break-word; orphans: auto; widows: auto; webkit-text-stroke-width: 0px">
  2. <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">package sqlHelper;</P>
  3. <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">import java.sql.*;
  4. import java.util.ArrayList;</P>
  5. <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">public class JdbcSqlHelper {
  6. </P>
  7. <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">private String odbcStr;//Odbc字符串

  8. //定义odbcStr写入输出
  9. public String getOdbcStr() {
  10. return odbcStr;
  11. }</P>
  12. <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">public void setOdbcStr(String odbcStr) {
  13. this.odbcStr = odbcStr;
  14. }



  15. private Connection conn=null;//定义连接变量
  16. private Statement stat=null;//定义Statement 变量


  17. //定义函数用于连接数据库
  18. void connSql()
  19. {
  20. try {
  21. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  22. try {
  23. conn=DriverManager.getConnection("jdbc:odbc:"+odbcStr);


  24. } catch (SQLException e) {
  25. // TODO Auto-generated catch block
  26. e.printStackTrace();
  27. }
  28. } catch (ClassNotFoundException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }

  32. }



  33. /// <summary>
  34. /// 关闭数据库连接
  35. /// </summary>
  36. void closeSqlconnAndStatement()
  37. {

  38. try {
  39. if(conn.isClosed()==false)
  40. conn.close();
  41. } catch (SQLException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. try {
  46. stat.close();
  47. } catch (SQLException e) {
  48. // TODO Auto-generated catch block
  49. e.printStackTrace();
  50. }
  51. }



  52. //一般数据操作方法



  53. /// <summary>
  54. /// 一般查询函数,返回值ResultSet类型
  55. /// </summary>
  56. /// <param name="sqlStr">执行字符串</param>
  57. public ResultSet selectData(String sqlStr)//查询数据,反回数据表数据
  58. {
  59. connSql();


  60. ResultSet rs = null;
  61. try {
  62. if(conn.isClosed()==false){

  63. stat=conn.createStatement();
  64. rs=stat.executeQuery(sqlStr);

  65. }

  66. } catch (SQLException e) {
  67. // TODO Auto-generated catch block
  68. e.printStackTrace();
  69. }

  70. return rs;


  71. }
  72. /// <summary>
  73. /// 一般插入函数,返回值为影响的行数,调用update_insert_delete_Function函数
  74. /// </summary>
  75. /// <param name="sqlStr">执行字符串</param>
  76. public int insertData(String sqlStr)// 插入数据操作,返回插入行数
  77. {
  78. return update_insert_delete_Function(sqlStr);

  79. }
  80. /// <summary>
  81. /// 一般更新函数,返回值为影响的行数,调用update_insert_delete_Function函数
  82. /// </summary>
  83. /// <param name="sqlStr">执行字符串</param>
  84. public int updateDate(String sqlStr)//更新数据操作,返回更新行数
  85. {
  86. return update_insert_delete_Function(sqlStr);
  87. }
  88. /// <summary>
  89. /// 一般删除函数,返回值为影响的行数,调用update_insert_delete_Function函数
  90. /// </summary>
  91. /// <param name="sqlStr">执行字符串</param>
  92. public int deleteDate(String sqlStr)//删除数据操作,返回删除行数
  93. {
  94. return update_insert_delete_Function(sqlStr);
  95. }
  96. /// <summary>
  97. /// 为一般增删改提供公用方法
  98. /// </summary>
  99. /// <param name="sqlStr">执行字符串</param>
  100. int update_insert_delete_Function(String sqlStr)//更新、插入、删除公用方法
  101. {
  102. connSql();
  103. int i=0;
  104. try {
  105. stat=conn.createStatement();
  106. i=stat.executeUpdate(sqlStr);
  107. closeSqlconnAndStatement();
  108. } catch (SQLException e) {
  109. // TODO Auto-generated catch block
  110. e.printStackTrace();
  111. }
  112. return i;
  113. }</P>
  114. <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">//.....................................................................................................
  115. //......................................................................................................
  116. //........................................................................................................

  117. //存储过程对数据操作
  118. CallableStatement cs=null;//定义存储过程的执行字符串连接
  119. /// <summary>
  120. /// 关闭数据库连接和执行字符串连接,
  121. /// 在执行完selectStoreData函数,
  122. /// 然后调用此函数以断开连接;
  123. /// </summary>
  124. public void closeConnAndCallableStatement()
  125. {
  126. try {
  127. cs.close();
  128. conn.close();
  129. } catch (SQLException e) {
  130. // TODO Auto-generated catch block
  131. e.printStackTrace();
  132. }
  133. }
  134. /// <summary>
  135. /// 存储查询函数
  136. /// </summary>
  137. /// <param name="storeSqlStr">存储过程字符串</param>
  138. /// <param name="al">存储过程的参数集合</param></P>
  139. <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">public ResultSet selectStoreData(String storeSqlStr,ArrayList<Object> al) throws SQLException
  140. {
  141. connSql();
  142. cs=conn.prepareCall(storeSqlStr);
  143. checkType(al);



  144. ResultSet rs=cs.executeQuery();
  145. return rs;





  146. }

  147. /// <summary>
  148. /// 判断参数集合的数据类型
  149. /// </summary>
  150. /// <param name="al">存储过程的参数集合</param>
  151. void checkType(ArrayList<Object> al) throws NumberFormatException, SQLException
  152. {<SPAN class=Apple-converted-space> </SPAN>


  153. for(int i=0;i<al.size();i++)
  154. {
  155. if(al.get(i) instanceof Integer){
  156. cs.setInt(i+1, Integer.parseInt((String) al.get(i)));
  157. }
  158. else if(al.get(i) instanceof String){

  159. cs.setString(i+1,(String) al.get(i));
  160. }
  161. else if(al.get(i) instanceof Float){
  162. cs.setFloat(i+1, Float.parseFloat((String)al.get(i)));
  163. }
  164. else if(al.get(i) instanceof Double){
  165. cs.setDouble(i+1, Double.parseDouble((String)al.get(i)));
  166. }
  167. else if(al.get(i) instanceof Long){
  168. cs.setLong(i+1, Long.parseLong((String)al.get(i)));
  169. }
  170. else if(al.get(i) instanceof Date){
  171. cs.setDate(i+1, Date.valueOf((String)al.get(i)));
  172. }
  173. else if(al.get(i) instanceof Time){
  174. cs.setTime(i+1, Time.valueOf((String)al.get(i)));
  175. }
  176. else if(al.get(i) instanceof Timestamp)
  177. {
  178. cs.setTimestamp(i+1, Timestamp.valueOf((String)al.get(i)));
  179. }
  180. else
  181. {
  182. cs.setObject(i+1, al.get(i));
  183. }


  184. }




  185. }

  186. /// <summary>
  187. /// 存储插入函数
  188. /// </summary>
  189. /// <param name="storeSqlStr">存储过程字符串</param>
  190. /// <param name="al">存储过程的参数集合</param>
  191. public int insertStoreData(String storeSqlStr,ArrayList<Object> al) throws SQLException
  192. {

  193. return update_insert_delete_Store_Function(storeSqlStr, al);

  194. }
  195. /// <summary>
  196. /// 存储更新函数
  197. /// </summary>
  198. /// <param name="storeSqlStr">存储过程字符串</param>
  199. /// <param name="al">存储过程的参数集合</param>
  200. public int updateStoreData(String storeSqlStr,ArrayList<Object> al) throws SQLException
  201. {

  202. return update_insert_delete_Store_Function(storeSqlStr, al);

  203. }
  204. /// <summary>
  205. /// 存储删除函数
  206. /// </summary>
  207. /// <param name="storeSqlStr">存储过程字符串</param>
  208. /// <param name="al">存储过程的参数集合</param>
  209. public int deleteStoreData(String storeSqlStr,ArrayList<Object> al) throws SQLException
  210. {

  211. return update_insert_delete_Store_Function(storeSqlStr, al);

  212. }
  213. /// <summary>
  214. /// 更新、插入、删除公用方法
  215. /// </summary>
  216. /// <param name="storeSqlStr">存储过程字符串</param>
  217. /// <param name="al">存储过程的参数集合</param>
  218. int update_insert_delete_Store_Function(String storeSqlStr,ArrayList<Object> al)
  219. {
  220. connSql();
  221. try {
  222. cs=conn.prepareCall(storeSqlStr);
  223. } catch (SQLException e) {
  224. // TODO Auto-generated catch block
  225. e.printStackTrace();
  226. }
  227. try {
  228. checkType(al);//调用判断参数类型
  229. } catch (NumberFormatException e) {
  230. // TODO Auto-generated catch block
  231. e.printStackTrace();
  232. } catch (SQLException e) {
  233. // TODO Auto-generated catch block
  234. e.printStackTrace();
  235. }

  236. int i = 0;
  237. try {
  238. i = cs.executeUpdate();
  239. } catch (SQLException e) {
  240. // TODO Auto-generated catch block
  241. e.printStackTrace();
  242. }
  243. closeConnAndCallableStatement();//关闭数据库连接
  244. return i;
  245. }

  246. </P>
  247. <P style="MARGIN: 10px auto; TEXT-INDENT: 0px; WORD-WRAP: break-word">

  248. }</P></DIV>
  249. <DIV id=MySignature style="MARGIN-TOP: 10px; WORD-SPACING: 0px; FONT: 14px/28px 'ms shell dlg'; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); WORD-WRAP: break-word; orphans: auto; widows: auto; webkit-text-stroke-width: 0px"></DIV>
  250. <DIV class=clear style="CLEAR: both; WORD-SPACING: 0px; FONT: 14px/28px 'ms shell dlg'; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); WORD-WRAP: break-word; orphans: auto; widows: auto; webkit-text-stroke-width: 0px"></DIV>
  251. <DIV id=blog_post_info_block style="MARGIN-TOP: 20px; WORD-SPACING: 0px; FONT: 14px/28px 'ms shell dlg'; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); WORD-WRAP: break-word; orphans: auto; widows: auto; webkit-text-stroke-width: 0px">
  252. <DIV id=blog_post_info style="WORD-WRAP: break-word">

  253. </DIV></DIV>
复制代码

1 个回复

倒序浏览
额 发帖发错了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马