Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. package common.database;
  2.  
  3. import com.sun.rowset.CachedRowSetImpl;
  4. import common.config.GlobalConfig;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7.  
  8. import javax.sql.DataSource;
  9. import javax.sql.rowset.CachedRowSet;
  10. import java.sql.*;
  11.  
  12. /**
  13. * 基础数据库类
  14. * Created by qijia on 2016/1/14.
  15. */
  16. public class Database {
  17.  
  18. private final Logger logger = LoggerFactory.getLogger(Database.class);
  19.  
  20. // 驱动注册被移动到tomcat启动监听器中
  21. // static {
  22. // try {
  23. // Class.forName("com.mysql.jdbc.Driver");
  24. // } catch (Exception e) {
  25. // logger.info("JDBC未注册!");
  26. // }
  27. // }
  28.  
  29. private static GlobalConfig config = GlobalConfig.getInstance();
  30.  
  31. // 数据库驱动名称
  32. private static String dburl = config.getString("jdbc.url");
  33. private static String dbusername = config.getString("jdbc.username");
  34. private static String dbpassword = config.getString("jdbc.password");
  35.  
  36. // private PreparedStatement ps;
  37. // private ResultSet rs;
  38.  
  39. /**
  40. * SQL语句在程序运行前已经进行了预编译,在程序运行时第一次操作数据库之前,SQL语句已经被数据库分析,
  41. * 编译和优化,对应的执行计划也会缓存下来并允许数据库已参数化的形式进行查询,当运行时动态地把参数传给
  42. * PreprareStatement时,即使参数里有敏感字符如 or ‘1=1’也数据库会作为一个参数一个字段的属性值来
  43. * 处理而不会作为一个SQL指令。
  44. */
  45.  
  46. private Connection con;
  47.  
  48. /**
  49. * 构造函数,连接数据库
  50. *
  51. * @throws Exception
  52. */
  53. public Database() throws SQLException {
  54.  
  55. this.con = DriverManager.getConnection(dburl, dbusername, dbpassword);
  56. }
  57.  
  58. /**
  59. * 构造函数,从连接池获取连接
  60. *
  61. * @throws Exception
  62. */
  63. // public Database() throws Exception {
  64. //
  65. // this.con = DatabasePool.getInstance().getTomcatDataSource().getConnection();
  66. // }
  67.  
  68.  
  69. /**
  70. * 构造函数,从连接池获取连接
  71. *
  72. * @throws Exception
  73. */
  74. public Database(DataSource dataSource) throws SQLException {
  75.  
  76. this.con = dataSource.getConnection();
  77. }
  78.  
  79. /**
  80. * 返回连接 INSECURITY
  81. *
  82. * @return
  83. * @throws SQLException
  84. */
  85. public Connection getCon() throws SQLException {
  86. return this.con;
  87. }
  88.  
  89. /**
  90. * 是否已经关闭
  91. *
  92. * @return
  93. * @throws SQLException
  94. */
  95. public boolean isClosed() {
  96. try {
  97. return this.con.isClosed();
  98. } catch (SQLException e) {
  99. return false;
  100. }
  101. }
  102.  
  103. /**
  104. * 执行查询
  105. *
  106. * @param sql
  107. * @param args
  108. * @return
  109. * @throws SQLException
  110. */
  111. public ResultSet query(String sql, Object... args) throws SQLException {
  112.  
  113. if (sql == null) {
  114. // closeQuietly(con);
  115. throw new SQLException("Null SQL statement");
  116. }
  117.  
  118. PreparedStatement ps = null;
  119. ResultSet rs = null;
  120. CachedRowSet rowSet = null;
  121.  
  122. try {
  123. ps = con.prepareStatement(sql);
  124. for (int i = 0; i < args.length; i++) {
  125. ps.setObject(i + 1, args[i]);
  126. }
  127. rs = ps.executeQuery();
  128. rowSet = new CachedRowSetImpl();
  129. rowSet.populate(rs);
  130. } catch (SQLException e) {
  131. throw e;
  132. } finally {
  133. //DONT uncomment
  134. // if (rowSet != null) {
  135. // rowSet.close();
  136. // }
  137. try {
  138. close(rs);
  139. } finally {
  140. if (ps != null) {
  141. close(ps);
  142. }
  143. // if (con != null) {
  144. // closeQuietly(con);
  145. // }
  146. }
  147. }
  148. return rowSet;
  149. }
  150.  
  151. /**
  152. * 执行插入 更新 删除
  153. *
  154. * @param sql
  155. * @param args
  156. * @return
  157. * @throws SQLException
  158. */
  159. public boolean update(String sql, Object... args) throws SQLException {
  160.  
  161. if (sql == null) {
  162. // close(con);
  163. throw new SQLException("Null SQL statement");
  164. }
  165.  
  166. PreparedStatement ps = null;
  167. try {
  168. ps = con.prepareStatement(sql);
  169. for (int i = 0; i < args.length; i++) {
  170. ps.setObject(i + 1, args[i]);
  171. }
  172. if (ps.executeUpdate() != 1) {
  173. return false;
  174. }
  175. } catch (SQLException e) {
  176. throw e;
  177. } finally {
  178. if (ps != null) {
  179. close(ps);
  180. }
  181. // if (con != null) {
  182. // close(con);
  183. // }
  184. }
  185. return true;
  186. }
  187.  
  188. /**
  189. * 执行插入,返回自增ID
  190. *
  191. * @param sql
  192. * @param args
  193. * @return
  194. * @throws SQLException
  195. */
  196. public long insertAutoIncrementLong(String sql, Object... args) throws SQLException {
  197. if (sql == null) {
  198. // close(con);
  199. throw new SQLException("Null SQL statement");
  200. }
  201.  
  202. long id = -1;
  203. PreparedStatement ps = null;
  204. ResultSet rs = null;
  205. try {
  206. ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
  207. for (int i = 0; i < args.length; i++) {
  208. ps.setObject(i + 1, args[i]);
  209. }
  210. if (ps.executeUpdate() != 1) {
  211. return id;
  212. }
  213. rs = ps.getGeneratedKeys();
  214. if (rs.next()) {
  215. id = rs.getLong(1);
  216. }
  217. } catch (SQLException e) {
  218. throw e;
  219. } finally {
  220. try {
  221. close(rs);
  222. } finally {
  223. if (ps != null) {
  224. close(ps);
  225. }
  226. // if (con != null) {
  227. // close(con);
  228. // }
  229. }
  230. }
  231. return id;
  232. }
  233.  
  234. /**
  235. * 执行插入,返回自增ID
  236. *
  237. * @param sql
  238. * @param args
  239. * @return
  240. * @throws SQLException
  241. */
  242. public int insertAutoIncrementInt(String sql, Object... args) throws SQLException {
  243. if (sql == null) {
  244. // close(con);
  245. throw new SQLException("Null SQL statement");
  246. }
  247.  
  248. int id = -1;
  249. PreparedStatement ps = null;
  250. ResultSet rs = null;
  251. try {
  252.  
  253. ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
  254. for (int i = 0; i < args.length; i++) {
  255. ps.setObject(i + 1, args[i]);
  256. }
  257. if (ps.executeUpdate() != 1) {
  258. return id;
  259. }
  260. rs = ps.getGeneratedKeys();
  261. if (rs.next()) {
  262. id = rs.getInt(1);
  263. }
  264. } catch (SQLException e) {
  265. throw e;
  266. } finally {
  267. try {
  268. close(rs);
  269. } finally {
  270. if (ps != null) {
  271. close(ps);
  272. }
  273. // if (con != null) {
  274. // close(con);
  275. // }
  276. }
  277. }
  278. return id;
  279. }
  280.  
  281. /**
  282. * 析构函数,中断数据库的链接
  283. * 不一定执行,发现未释放conn现象
  284. *
  285. * @throws SQLException
  286. */
  287. @Override
  288. protected void finalize() throws Throwable {
  289. try {
  290. if (con != null && !con.isClosed()) {
  291. con.close();
  292. }
  293. } finally {
  294. super.finalize();
  295. }
  296. }
  297.  
  298. /**
  299. * 手动中断数据库的链接
  300. * Database对象完成工作后必须调用
  301. */
  302. public void close() throws SQLException {
  303. if (con != null && !con.isClosed()) {
  304. con.close();
  305. }
  306. }
  307.  
  308. /**
  309. * Close a <code>Connection</code>, avoid closing if null and hide
  310. * any SQLExceptions that occur.
  311. */
  312. public void closeQuietly() {
  313. try {
  314. close(con);
  315. } catch (SQLException e) { // NOPMD
  316. // quiet
  317. }
  318. }
  319.  
  320. /**
  321. * Close a <code>Connection</code>, avoid closing if null.
  322. *
  323. * @param conn Connection to close.
  324. * @throws SQLException if a database access error occurs
  325. */
  326. protected void close(Connection conn) throws SQLException {
  327. if (conn != null) {
  328. conn.close();
  329. }
  330. }
  331.  
  332. /**
  333. * Close a <code>Statement</code>. This implementation avoids closing if
  334. * null and does <strong>not</strong> suppress any exceptions. Subclasses
  335. * can override to provide special handling like logging.
  336. *
  337. * @param stmt Statement to close
  338. * @throws SQLException if a database access error occurs
  339. * @since DbUtils 1.1
  340. */
  341. protected void close(Statement stmt) throws SQLException {
  342. if (stmt != null) {
  343. stmt.close();
  344. }
  345. }
  346.  
  347. /**
  348. * Close a <code>ResultSet</code>. This implementation avoids closing if
  349. * null and does <strong>not</strong> suppress any exceptions. Subclasses
  350. * can override to provide special handling like logging.
  351. *
  352. * @param rs ResultSet to close
  353. * @throws SQLException if a database access error occurs
  354. * @since DbUtils 1.1
  355. */
  356. protected void close(ResultSet rs) throws SQLException {
  357. if (rs != null) {
  358. rs.close();
  359. }
  360. }
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement