JdbcUtil.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package com.uas.util;
  2. import org.codehaus.jackson.map.ObjectMapper;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.sql.*;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Properties;
  10. public class JdbcUtil {
  11. public static Map<String,Object> ftpConfigs;
  12. public static Map<String,Object> dbConfigs;
  13. public static Connection connection;
  14. /**
  15. * 从ftpconfigs配置文件中获取ftp站点配置信息
  16. * @return Map<String,Object>
  17. */
  18. @SuppressWarnings("unchecked")
  19. public static Map<String,Object> getFtpConfigs(){
  20. if(ftpConfigs==null){
  21. try {
  22. ftpConfigs = new ObjectMapper().readValue(JdbcUtil.class.getResourceAsStream("/properties/ftpconfig.properties"), HashMap.class);
  23. }catch (Exception e) {
  24. BaseUtil.getLogger().error(e.toString());
  25. e.printStackTrace();
  26. }
  27. }
  28. return ftpConfigs;
  29. }
  30. /**
  31. * 从dbconfig配置文件中获取数据库配置信息
  32. * @return Map<String,Object>
  33. */
  34. @SuppressWarnings("unchecked")
  35. public static Map<String,Object> getDBConfigs(){
  36. if(dbConfigs==null){
  37. try {
  38. dbConfigs = new ObjectMapper().readValue(JdbcUtil.class.getResourceAsStream("/properties/dbconfig.properties"), HashMap.class);
  39. }catch (Exception e) {
  40. BaseUtil.getLogger().error(e.toString());
  41. e.printStackTrace();
  42. }
  43. }
  44. return dbConfigs;
  45. }
  46. /**
  47. * 连接到数据库
  48. * @param url 地址
  49. * @param user 用户名
  50. * @param password 密码
  51. * @return 返回一个连接
  52. * @throws Exception
  53. */
  54. public static Connection getConnect(String url,String user,String password) throws Exception{
  55. Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
  56. connection = DriverManager.getConnection(url, user, password);// 获取连接
  57. return connection;
  58. }
  59. /**
  60. * 从dbconfig配置文件中获取数据库连接
  61. * @return 返回数据库连接
  62. */
  63. public static Connection getConnect(){
  64. if(connection==null){
  65. try{
  66. InputStream dbinput = JdbcUtil.class.getResourceAsStream("/properties/dbconfig.properties");
  67. Properties prop = new Properties();
  68. prop.load(dbinput);
  69. connection = JdbcUtil.getConnect(prop.getProperty("url"), prop.getProperty("user"), prop.getProperty("password"));
  70. }catch (Exception e){
  71. try {
  72. System.in.read();
  73. } catch (IOException e2) {
  74. // TODO Auto-generated catch block
  75. e2.printStackTrace();
  76. }
  77. BaseUtil.getLogger().error(e.toString());
  78. try {
  79. if(connection!=null){
  80. connection.close();
  81. }
  82. } catch (SQLException e1) {
  83. BaseUtil.getLogger().error(e1.toString());
  84. e1.printStackTrace();
  85. }finally{
  86. connection = null;
  87. }
  88. e.printStackTrace();
  89. }
  90. }
  91. return connection;
  92. }
  93. /**
  94. * 从dbconfig配置文件中获取数据库连接
  95. * @return 返回数据库连接
  96. */
  97. @SuppressWarnings("unchecked")
  98. public static Connection getConnectBySob(String sob){
  99. Map<String,Object> dbServMaps = getDBConfigs();
  100. Map<String,Object> dbServMap = (Map<String,Object>)dbServMaps.get(sob);
  101. //先把当前的连接关闭
  102. try {
  103. if(connection!=null){
  104. connection.close();
  105. }
  106. } catch (SQLException e) {
  107. // TODO Auto-generated catch block
  108. e.printStackTrace();
  109. }finally{
  110. connection = null;
  111. }
  112. try{
  113. connection = JdbcUtil.getConnect(dbServMap.get("url").toString(), dbServMap.get("user").toString(), dbServMap.get("password").toString());
  114. }catch (Exception e){
  115. try {
  116. System.in.read();
  117. } catch (IOException e2) {
  118. // TODO Auto-generated catch block
  119. e2.printStackTrace();
  120. }
  121. BaseUtil.getLogger().error(e.toString());
  122. try {
  123. if(connection!=null){
  124. connection.close();
  125. }
  126. } catch (SQLException e1) {
  127. BaseUtil.getLogger().error(e1.toString());
  128. e1.printStackTrace();
  129. }finally{
  130. connection = null;
  131. }
  132. e.printStackTrace();
  133. }
  134. return connection;
  135. }
  136. /**
  137. * 关闭连接
  138. * @param con
  139. * @param pre
  140. * @param result
  141. * @return
  142. */
  143. public static boolean closeCn(Connection con,PreparedStatement pre,ResultSet result){
  144. try{
  145. if (result != null) {
  146. result.close();
  147. result = null;
  148. }
  149. if (pre != null) {
  150. pre.close();
  151. pre = null;
  152. }
  153. if (con != null) {
  154. con.close();
  155. }
  156. }
  157. catch (Exception e){
  158. BaseUtil.getLogger().error(e.toString());
  159. e.printStackTrace();
  160. }
  161. return true;
  162. }
  163. /**
  164. * 执行多条sql语句
  165. * @param connection 连接
  166. * @param str 多条sqls语句
  167. * @return
  168. */
  169. @SuppressWarnings("finally")
  170. public static boolean executeSqls(Connection connection,List<String> str){
  171. Statement statement = null;
  172. boolean bol = true;
  173. try{
  174. statement = connection.createStatement();
  175. for(String sql:str){
  176. statement.addBatch(sql);
  177. }
  178. try {
  179. statement.setQueryTimeout(180);
  180. statement.executeBatch();
  181. } catch (SQLException e) {
  182. bol = false;
  183. BaseUtil.getLogger().error(e.toString());
  184. try {
  185. statement.close();
  186. } catch (SQLException e1) {
  187. BaseUtil.getLogger().error(e1.toString());
  188. e1.printStackTrace();
  189. }
  190. e.printStackTrace();
  191. }
  192. statement.close();
  193. }catch(Exception e){
  194. bol = false;
  195. BaseUtil.getLogger().error(e.toString());
  196. try {
  197. if(connection!=null){
  198. connection.rollback();
  199. }
  200. } catch (SQLException e1) {
  201. BaseUtil.getLogger().error(e1.toString());
  202. e1.printStackTrace();
  203. }
  204. e.printStackTrace();
  205. }finally{
  206. statement = null;
  207. return bol;
  208. }
  209. }
  210. @SuppressWarnings("finally")
  211. public static int getIdBySeq(String seq){
  212. connection = getConnect();
  213. Statement statement = null;
  214. int id = 0;
  215. if(seq!=null&!"".equals(seq)){
  216. try{
  217. statement = connection.createStatement();
  218. ResultSet rs = statement.executeQuery("select " + seq + ".nextval id from dual");
  219. if(rs.next()){
  220. id = rs.getInt("id");
  221. }
  222. rs.close();
  223. statement.close();
  224. }catch(Exception e){
  225. BaseUtil.getLogger().error(e.toString());
  226. try {
  227. if(connection!=null){
  228. connection.rollback();
  229. }
  230. } catch (SQLException e1) {
  231. BaseUtil.getLogger().error(e1.toString());
  232. e1.printStackTrace();
  233. }
  234. e.printStackTrace();
  235. }finally{
  236. statement = null;
  237. return id;
  238. }
  239. }
  240. return id;
  241. }
  242. }