List of usage examples for java.sql SQLException printStackTrace
public void printStackTrace()
From source file:common.DB.java
public static JSONArray createJson(String query, HashMap<String, String> extra) { //Get the formating of lable JSONArray jsonArray = new JSONArray(); try {// w w w . ja va2s . co m DB.getConnection(); statement = connection.createStatement(); resultSet = statement.executeQuery(query); rsmd = resultSet.getMetaData(); int total_cols = resultSet.getMetaData().getColumnCount(); while (resultSet.next()) { JSONObject obj = new JSONObject(); HashMap<String, String> temp = new HashMap(extra); for (int i = 0; i < total_cols; i++) { String colName = rsmd.getColumnLabel(i + 1); for (Map.Entry<String, String> entry : temp.entrySet()) { String value = entry.getValue(); value = value.replaceAll("\\[" + colName + "\\]", resultSet.getString(colName)); entry.setValue(value); } obj.put(colName, resultSet.getObject(i + 1)); } for (Map.Entry<String, String> entry : temp.entrySet()) { String key = entry.getKey(); String val = entry.getValue(); obj.put(key, val); } jsonArray.put(obj); } } catch (SQLException e) { e.printStackTrace(); } catch (JSONException ex) { ex.printStackTrace(); } close(); return jsonArray; }
From source file:com.srotya.tau.api.Utils.java
public static void createDatabase(String dbConnectionString, String dbName, String user, String pass, String driver) throws Exception { Connection conn = null;//from w ww. j av a 2s .c o m Statement stmt = null; try { // STEP 2: Register JDBC driver Class.forName(driver); // STEP 3: Open a connection System.out.println("Connecting to database with connection string:" + dbConnectionString); conn = DriverManager.getConnection(dbConnectionString, user, pass); // STEP 4: Execute a query System.out.println("Creating database..."); stmt = conn.createStatement(); String sql = "CREATE DATABASE IF NOT EXISTS " + dbName; stmt.executeUpdate(sql); System.out.println("Database created successfully..."); } finally { // finally block used to close resources try { if (stmt != null) stmt.close(); } catch (SQLException se2) { } // nothing we can do try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } // end finally try } // end try }
From source file:com.paladin.sys.db.QueryHelper.java
/** * set autoCommit// w w w . j a v a2s . c o m * * @param _auto */ public static void setAutoCommit(boolean _auto) { try { getConnection().setAutoCommit(_auto); } catch (SQLException e) { e.printStackTrace(); } }
From source file:com.paladin.sys.db.QueryHelper.java
/** * commit conn// w w w . ja v a 2 s. co m */ public static void commit() { try { getConnection().commit(); } catch (SQLException e) { e.printStackTrace(); } }
From source file:com.paladin.sys.db.QueryHelper.java
/** * rollback/* www . jav a 2 s. c om*/ */ public static void rollback() { try { getConnection().rollback(); } catch (SQLException e) { e.printStackTrace(); } }
From source file:com.paladin.sys.db.QueryHelper.java
/** * INSERT/UPDATE/DELETE ?//from w w w .j ava 2s . c om * * @param sql * @param params * @return */ public static int update(String sql, Object... params) { try { return RUNNER.update(getConnection(), sql, params); } catch (SQLException e) { e.printStackTrace(); } finally { DBManager.closeConnection(); } return -1; }
From source file:com.paladin.sys.db.QueryHelper.java
/** * ? SQL ?/*from w ww. ja v a 2 s . c o m*/ * * @param sql * @param params * @return */ public static int[] batch(String sql, Object[][] params) { try { return RUNNER.batch(getConnection(), sql, params); } catch (SQLException e) { e.printStackTrace(); } finally { DBManager.closeConnection(); } return null; }
From source file:com.paladin.sys.db.QueryHelper.java
/** * ? ? /*w ww . ja v a 2s . co m*/ * * @param sql * @param params * @return */ public static <T> T read(Class<T> beanClass, String sql, Object... params) { try { return (T) RUNNER.query(getConnection(), sql, IS_PRIMITIVE(beanClass) ? scalar_handler : new BeanHandler(beanClass), params); } catch (SQLException e) { e.printStackTrace(); } finally { DBManager.closeConnection(); } return null; }
From source file:com.paladin.sys.db.QueryHelper.java
/** * //from w w w . ja v a2 s .c o m * * @param <T> * @param beanClass * @param sql * @param params * @return */ public static <T> List<T> query(Class<T> beanClass, String sql, Object... params) { try { return (List<T>) RUNNER.query(getConnection(), sql, IS_PRIMITIVE(beanClass) ? COLUMN_LIST_HANDLER : new BeanListHandler(beanClass), params); } catch (SQLException e) { e.printStackTrace(); } finally { DBManager.closeConnection(); } return null; }
From source file:com.paladin.sys.db.QueryHelper.java
/** * ???//from w w w . ja v a 2 s .co m * * @param sql * @param params * @return */ public static long stat(String sql, Object... params) { try { Number num = (Number) RUNNER.query(getConnection(), sql, scalar_handler, params); return (num != null) ? num.longValue() : -1; } catch (SQLException e) { e.printStackTrace(); return -1; } finally { DBManager.closeConnection(); } }