/** * Execute transaction. * * @param config the Config object * @param transactionLevel the transaction level * @param atom the atom operation * @return true if transaction executing succeed otherwise false */ boolean tx(Config config, int transactionLevel, IAtom atom) { Connection conn = config.getThreadLocalConnection(); if (conn != null) { // Nested transaction support try { if (conn.getTransactionIsolation() < transactionLevel) conn.setTransactionIsolation(transactionLevel); boolean result = atom.run(); if (result) return true; throw new NestedTransactionHelpException( "Notice the outer transaction that the nested transaction return false"); // important:can not return false } catch (SQLException e) { throw new ActiveRecordException(e); } } Boolean autoCommit = null; try { conn = config.getConnection(); autoCommit = conn.getAutoCommit(); config.setThreadLocalConnection(conn); conn.setTransactionIsolation(transactionLevel); conn.setAutoCommit(false); boolean result = atom.run(); if (result) conn.commit(); else conn.rollback(); return result; } catch (NestedTransactionHelpException e) { if (conn != null) try { conn.rollback(); } catch (Exception e1) { LogKit.error(e1.getMessage(), e1); } LogKit.logNothing(e); return false; } catch (Throwable t) { if (conn != null) try { conn.rollback(); } catch (Exception e1) { LogKit.error(e1.getMessage(), e1); } throw t instanceof RuntimeException ? (RuntimeException) t : new ActiveRecordException(t); } finally { try { if (conn != null) { if (autoCommit != null) conn.setAutoCommit(autoCommit); conn.close(); } } catch (Throwable t) { LogKit.error( t.getMessage(), t); // can not throw exception here, otherwise the more important exception in previous // catch block can not be thrown } finally { config.removeThreadLocalConnection(); // prevent memory leak } } }
/** @see #query(String, String, Object...) */ public <T> List<T> query(String sql, Object... paras) { Connection conn = null; try { conn = config.getConnection(); return query(config, conn, sql, paras); } catch (Exception e) { throw new ActiveRecordException(e); } finally { config.close(conn); } }
/** * Execute callback. It is useful when all the API can not satisfy your requirement. * * @param config the Config object * @param callback the ICallback interface */ Object execute(Config config, ICallback callback) { Connection conn = null; try { conn = config.getConnection(); return callback.run(conn); } catch (Exception e) { throw new ActiveRecordException(e); } finally { config.close(conn); } }
/** * Update Record. * * @param tableName the table name of the Record save to * @param primaryKey the primary key of the table * @param record the Record object * @param true if update succeed otherwise false */ public boolean update(String tableName, String primaryKey, Record record) { Connection conn = null; try { conn = config.getConnection(); return update(config, conn, tableName, primaryKey, record); } catch (Exception e) { throw new ActiveRecordException(e); } finally { config.close(conn); } }
/** @see #paginate(String, int, int, String, String, Object...) */ public Page<Record> paginate( int pageNumber, int pageSize, String select, String sqlExceptSelect, Object... paras) { Connection conn = null; try { conn = config.getConnection(); return paginate(config, conn, pageNumber, pageSize, select, sqlExceptSelect, paras); } catch (Exception e) { throw new ActiveRecordException(e); } finally { config.close(conn); } }
/** * Execute a batch of SQL INSERT, UPDATE, or DELETE queries. Example: * * <pre> * int[] result = DbPro.use().batch("myConfig", sqlList, 500); * </pre> * * @param sqlList The SQL list to execute. * @param batchSize batch size. * @return The number of rows updated per statement */ public int[] batch(List<String> sqlList, int batchSize) { Connection conn = null; Boolean autoCommit = null; try { conn = config.getConnection(); autoCommit = conn.getAutoCommit(); conn.setAutoCommit(false); return batch(config, conn, sqlList, batchSize); } catch (Exception e) { throw new ActiveRecordException(e); } finally { if (autoCommit != null) try { conn.setAutoCommit(autoCommit); } catch (Exception e) { e.printStackTrace(); } config.close(conn); } }
/** * Execute a batch of SQL INSERT, UPDATE, or DELETE queries. * * <pre> * Example: * String sql = "insert into user(name, cash) values(?, ?)"; * int[] result = DbPro.use().batch(sql, new Object[][]{{"James", 888}, {"zhanjin", 888}}); * </pre> * * @param sql The SQL to execute. * @param paras An array of query replacement parameters. Each row in this array is one set of * batch replacement values. * @return The number of rows updated per statement */ public int[] batch(String sql, Object[][] paras, int batchSize) { Connection conn = null; Boolean autoCommit = null; try { conn = config.getConnection(); autoCommit = conn.getAutoCommit(); conn.setAutoCommit(false); return batch(config, conn, sql, paras, batchSize); } catch (Exception e) { throw new ActiveRecordException(e); } finally { if (autoCommit != null) try { conn.setAutoCommit(autoCommit); } catch (Exception e) { LogKit.error(e.getMessage(), e); } config.close(conn); } }
public static LinkedList<User> getAll() { String query = "SELECT username, password, email, number, isIT FROM tomcat_user"; LinkedList<User> items = new LinkedList<>(); try (Connection connection = Config.getConnection(); Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(query); ) { while (result.next()) { items.add( new User( result.getString("username"), result.getString("password"), result.getString("email"), result.getString("number"), query)); } } catch (SQLException e) { System.err.println("Couldn't get items from database."); } return items; }
public static List<Movie> getAllMovies() { String query = "SELECT * FROM movie"; List<Movie> movies = new LinkedList<>(); try (Connection connection = Config.getConnection(); // step 1 Statement statement = connection.createStatement(); // step 2 ResultSet result = statement.executeQuery(query); ) { // step 3 and 4 while (result.next()) { // step 5 Movie movie = new Movie(); // you should be validating the following, // this is just an example to get you started movie.setName(result.getString(1)); movie.setYear(result.getDate(2).getYear()); movie.setUrl(result.getString(3)); movies.add(movie); } } catch (SQLException e) { System.err.println(e.getMessage()); System.err.println(e.getStackTrace()); } return movies; }