123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package com.xt.dsp.jdbc;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Date;
- import com.alibaba.fastjson.JSONObject;
- public class JDBCTools {
- private Connection conn;
- private Statement statement;
- private PreparedStatement ps;
- private ResultSet rs;
- private String driver = null;
- private String url = null;
- private String username = null;
- private String password = null;
- public JDBCTools(JSONObject connJson) {
- this.driver = connJson.getString("driver");
- this.url = connJson.getString("url");
- this.username = connJson.getString("username");
- this.password = connJson.getString("password");
- }
- public Connection getConnection() throws ClassNotFoundException, SQLException {
- Class.forName(this.driver);
- return conn = DriverManager.getConnection(url, username, password);
- }
- public void update(String sql, String condition) {
- try {
- conn = getConnection();
- ps = (PreparedStatement) conn.prepareStatement(sql);
- ps.setString(1, condition);
- ps.executeUpdate(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- releaseDB(conn, statement);
- }
- }
- public void update1(String sql) {
- try {
- statement = (Statement) getConnection().createStatement();
- statement.executeUpdate(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- releaseDB(conn, statement);
- }
- }
- public void testUpdete1() {
- String sql1 = "DELETE FROM Users WHERE userName='RORO'";
- update1(sql1);
- }
- public void releaseDB(Connection conn, Statement statement) {
- if (statement != null) {
- try {
- statement.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public void releaseDB(Connection conn, PreparedStatement ps) {
- if (ps != null) {
- try {
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public void releaseDB() {
- if (ps != null) {
- try {
- ps.close();
- } catch (Exception e) {
- }
- }
- if (rs != null) {
- try {
- rs.close();
- } catch (Exception e) {
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (Exception e) {
- }
- }
- }
- public ResultSet getResultSet(String sql) {
- try {
- conn = getConnection();
- statement = (Statement) conn.createStatement();
- rs = statement.executeQuery(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return rs;
- }
- public ResultSet getPreResultSet(String sql, String condition) {
- try {
- conn = getConnection();
- ps = (PreparedStatement) conn.prepareStatement(sql);
- ps.setString(1, condition);
- rs = ps.executeQuery();
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return rs;
- }
- }
|