// Hàm hỗ trợ Insert xuống CSDL. // INSERT INTO TableName (columnName...) VALUES (column Values...); public boolean Insert(String TableName, HashMap<String, Object> ColumnValues) throws Exception { // Khai báo biến StringBuilder để tạo chuỗi Select. StringBuilder query = new StringBuilder("INSERT INTO " + TableName); // Khai báo biến StringBuilder để tạo chuỗi Column Values. StringBuilder valueInsert = new StringBuilder(); query.append("("); // Duyệt và đưa thông tin tên cột và giá trị của cột vào câu query for (String key : ColumnValues.keySet()) { query.append(key).append(","); valueInsert.append("'").append(ColumnValues.get(key).toString()).append("' ,"); } // Cắt bớt ký tự ',' cuối câu query = query.delete(query.length() - 1, query.length()); valueInsert = valueInsert.delete(valueInsert.length() - 1, valueInsert.length()); // Đưa giá trị của cột vào câu query query.append(") VALUES(").append(valueInsert.toString()).append(")"); // Chèn ký tự ';' vào cuối dòng lệnh của để cách các câu lệnh. query.append(";"); // Thực thi câu query và trả kết quả ra ngoài return this._connect.executeUpdate(query.toString()) > 0; }
// Hàm hỗ trợ Update CSDL // UPDATE TableName SET ColumnName = ColumnValue WHERE Condiotion; public boolean Update(String TableName, HashMap<String, Object> ColumnValues, String Condition) throws Exception { // Khai báo biến StringBuilder để tạo chuỗi Select. StringBuilder query = new StringBuilder("UPDATE " + TableName + " SET "); // Duyệt và đưa thông tin tên cột và giá trị của cột vào câu query for (String key : ColumnValues.keySet()) { query.append(key).append(" = '").append(ColumnValues.get(key).toString()).append("',"); } // Cắt bớt ký tự ',' cuối câu query = query.delete(query.length() - 1, query.length()); // Đưa câu lệnh điều kiện vào trong câu query this.AddCondition(query, Condition); // Chèn ký tự ';' vào cuối dòng lệnh của để cách các câu lệnh. query.append(";"); // Thực thi câu query và trả kết quả ra ngoài return this._connect.executeUpdate(query.toString()) > 0; }
public String makeWholeUnionCall(String question) { List<String> letters = new ArrayList<String>(); letters.add("a"); letters.add("b"); letters.add("c"); letters.add("d"); letters.add("e"); letters.add("f"); StringBuilder wholeSelect = new StringBuilder(); wholeSelect.append("COPY ("); for (String letter : letters) { wholeSelect.append(makeSelectForUnion(question, letter)); wholeSelect.append(" UNION "); } wholeSelect.delete(wholeSelect.length() - 6, wholeSelect.length()); wholeSelect.append(") TO '/tmp/p_").append(question).append(".csv' WITH CSV;"); return wholeSelect.toString(); }
private static final void mysqlFind( final Plugin plugin, final String playerName, final Location location, final int radius, final WorldManager manager, final ArrayList<Player> players) { BBPlayerInfo hunted = BBUsersTable.getInstance().getUserByName(playerName); PreparedStatement ps = null; ResultSet rs = null; HashMap<Integer, Integer> creations = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> destructions = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> explosions = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> burns = new HashMap<Integer, Integer>(); Connection conn = null; try { conn = ConnectionManager.getConnection(); if (conn == null) return; // TODO maybe more customizable actions? String actionString = "action IN('" + Action.BLOCK_BROKEN.ordinal() + "', '" + Action.BLOCK_PLACED.ordinal() + "', '" + Action.LEAF_DECAY.ordinal() + "', '" + Action.TNT_EXPLOSION.ordinal() + "', '" + Action.CREEPER_EXPLOSION.ordinal() + "', '" + Action.MISC_EXPLOSION.ordinal() + "', '" + Action.LAVA_FLOW.ordinal() + "', '" + Action.BLOCK_BURN.ordinal() + "')"; ps = conn.prepareStatement( "SELECT action, type FROM " + BBDataTable.getInstance().getTableName() + " WHERE " + actionString + " AND rbacked = 0 AND x < ? AND x > ? AND y < ? AND y > ? AND z < ? AND z > ? AND player = ? AND world = ? order by date desc"); ps.setInt(1, location.getBlockX() + radius); ps.setInt(2, location.getBlockX() - radius); ps.setInt(3, location.getBlockY() + radius); ps.setInt(4, location.getBlockY() - radius); ps.setInt(5, location.getBlockZ() + radius); ps.setInt(6, location.getBlockZ() - radius); ps.setInt(7, hunted.getID()); ps.setInt(8, manager.getWorld(location.getWorld().getName())); rs = ps.executeQuery(); conn.commit(); int size = 0; while (rs.next()) { Action action = Action.values()[rs.getInt("action")]; int type = rs.getInt("type"); switch (action) { case BLOCK_BROKEN: case LEAF_DECAY: if (destructions.containsKey(type)) { destructions.put(type, destructions.get(type) + 1); size++; } else { destructions.put(type, 1); size++; } break; case BLOCK_PLACED: if (creations.containsKey(type)) { creations.put(type, creations.get(type) + 1); size++; } else { creations.put(type, 1); size++; } break; case TNT_EXPLOSION: case CREEPER_EXPLOSION: case MISC_EXPLOSION: if (explosions.containsKey(type)) { explosions.put(type, explosions.get(type) + 1); size++; } else { explosions.put(type, 1); size++; } case BLOCK_BURN: if (burns.containsKey(type)) { burns.put(type, burns.get(type) + 1); size++; } else { burns.put(type, 1); size++; } break; case LAVA_FLOW: if (creations.containsKey(type)) { creations.put(type, creations.get(type) + 1); size++; } else { creations.put(type, 1); size++; } break; } } if (size > 0) { StringBuilder creationList = new StringBuilder(); creationList.append(ChatColor.AQUA.toString()); creationList.append("Placed Blocks: "); creationList.append(ChatColor.WHITE.toString()); for (Entry<Integer, Integer> entry : creations.entrySet()) { creationList.append(Material.getMaterial(entry.getKey())); creationList.append(" ("); creationList.append(entry.getValue()); creationList.append("), "); } if (creationList.toString().contains(",")) { creationList.delete(creationList.lastIndexOf(","), creationList.length()); } StringBuilder brokenList = new StringBuilder(); brokenList.append(ChatColor.RED.toString()); brokenList.append("Broken Blocks: "); brokenList.append(ChatColor.WHITE.toString()); for (Entry<Integer, Integer> entry : destructions.entrySet()) { brokenList.append(Material.getMaterial(entry.getKey())); brokenList.append(" ("); brokenList.append(entry.getValue()); brokenList.append("), "); } if (brokenList.toString().contains(",")) { brokenList.delete(brokenList.lastIndexOf(","), brokenList.length()); } StringBuilder explodeList = new StringBuilder(); explodeList.append(ChatColor.RED.toString()); explodeList.append("Exploded Blocks: "); explodeList.append(ChatColor.WHITE.toString()); for (Entry<Integer, Integer> entry : explosions.entrySet()) { explodeList.append(Material.getMaterial(entry.getKey())); explodeList.append(" ("); explodeList.append(entry.getValue()); explodeList.append("), "); } if (explodeList.toString().contains(",")) { explodeList.delete(explodeList.lastIndexOf(","), explodeList.length()); } StringBuilder burnList = new StringBuilder(); burnList.append(ChatColor.RED.toString()); burnList.append("Burned Blocks: "); burnList.append(ChatColor.WHITE.toString()); for (Entry<Integer, Integer> entry : burns.entrySet()) { burnList.append(Material.getMaterial(entry.getKey())); burnList.append(" ("); burnList.append(entry.getValue()); burnList.append("), "); } if (burnList.toString().contains(",")) { burnList.delete(burnList.lastIndexOf(","), burnList.length()); } for (Player player : players) { player.sendMessage( BigBrother.premessage + playerName + " has made " + size + " modifications"); if (creations.entrySet().size() > 0) { player.sendMessage(creationList.toString()); } if (destructions.entrySet().size() > 0) { player.sendMessage(brokenList.toString()); } if (explosions.entrySet().size() > 0) { player.sendMessage(explodeList.toString()); } if (burns.entrySet().size() > 0) { player.sendMessage(burnList.toString()); } } } else { for (Player player : players) { player.sendMessage( BigBrother.premessage + playerName + " has no modifications in this area."); } } } catch (SQLException ex) { BBLogging.severe("Find SQL Exception", ex); } finally { ConnectionManager.cleanup("Find SQL", conn, ps, rs); } }
private static final void mysqlFind( final Plugin plugin, final Location location, final int radius, final WorldManager manager, final ArrayList<Player> players) { PreparedStatement ps = null; ResultSet rs = null; Connection conn = null; HashMap<BBPlayerInfo, Integer> modifications = new HashMap<BBPlayerInfo, Integer>(); try { conn = ConnectionManager.getConnection(); if (conn == null) return; // TODO maybe more customizable actions? String actionString = "action IN('" + Action.BLOCK_BROKEN.ordinal() + "', '" + Action.BLOCK_PLACED.ordinal() + "', '" + Action.LEAF_DECAY.ordinal() + "', '" + Action.TNT_EXPLOSION.ordinal() + "', '" + Action.CREEPER_EXPLOSION.ordinal() + "', '" + Action.MISC_EXPLOSION.ordinal() + "', '" + Action.LAVA_FLOW.ordinal() + "', '" + Action.BLOCK_BURN.ordinal() + "')"; /* * org.h2.jdbc.JdbcSQLException: Column "ID" must be in the GROUP BY * list; SQL statement: */ if (BBSettings.usingDBMS(DBMS.H2) || BBSettings.usingDBMS(DBMS.POSTGRES)) { ps = conn.prepareStatement( "SELECT player, count(player) AS modifications FROM " + BBDataTable.getInstance().getTableName() + " WHERE " + actionString + " AND rbacked = '0' AND x < ? AND x > ? AND y < ? AND y > ? AND z < ? AND z > ? AND world = ? GROUP BY id,player ORDER BY id DESC"); } else { ps = conn.prepareStatement( "SELECT player, count(player) AS modifications FROM " + BBDataTable.getInstance().getTableName() + " WHERE " + actionString + " AND rbacked = '0' AND x < ? AND x > ? AND y < ? AND y > ? AND z < ? AND z > ? AND world = ? GROUP BY player ORDER BY id DESC"); } ps.setInt(1, location.getBlockX() + radius); ps.setInt(2, location.getBlockX() - radius); ps.setInt(3, location.getBlockY() + radius); ps.setInt(4, location.getBlockY() - radius); ps.setInt(5, location.getBlockZ() + radius); ps.setInt(6, location.getBlockZ() - radius); ps.setInt(7, manager.getWorld(location.getWorld().getName())); rs = ps.executeQuery(); conn.commit(); int size = 0; while (rs.next()) { BBPlayerInfo player = BBUsersTable.getInstance().getUserByID(rs.getInt("player")); int mods = rs.getInt("modifications"); modifications.put(player, mods); size++; } if (size > 0) { StringBuilder playerList = new StringBuilder(); for (Entry<BBPlayerInfo, Integer> entry : modifications.entrySet()) { if (entry.getKey() != null) { playerList.append(entry.getKey().getName()); playerList.append(" ("); playerList.append(entry.getValue()); playerList.append("), "); } } if (playerList.indexOf(",") != -1) { playerList.delete(playerList.lastIndexOf(","), playerList.length()); } // TODO Put into sync'd runnable for (Player player : players) { player.sendMessage( BigBrother.premessage + playerList.length() + " player(s) have modified this area:"); player.sendMessage(playerList.toString()); } } else { for (Player player : players) { player.sendMessage(BigBrother.premessage + "No modifications in this area."); } } } catch (SQLException ex) { BBLogging.severe("Find SQL Exception", ex); } finally { ConnectionManager.cleanup("Find SQL", conn, ps, rs); } }
// TODO use IN(1,2,3) private void mysqlFind(boolean sqlite) { PreparedStatement ps = null; ResultSet rs = null; Connection conn = null; HashMap<String, Integer> modifications = new HashMap<String, Integer>(); try { conn = ConnectionManager.getConnection(); // TODO maybe more customizable actions? String actionString = "action IN('" + Action.BLOCK_BROKEN.ordinal() + "', '" + Action.BLOCK_PLACED.ordinal() + "', '" + Action.LEAF_DECAY.ordinal() + "', '" + Action.TNT_EXPLOSION.ordinal() + "', '" + Action.CREEPER_EXPLOSION.ordinal() + "', '" + Action.MISC_EXPLOSION.ordinal() + "', '" + Action.BLOCK_BURN.ordinal() + "')"; ps = conn.prepareStatement( "SELECT player, count(player) AS modifications FROM " + BBDataBlock.BBDATA_NAME + " WHERE " + actionString + " AND rbacked = '0' AND x < ? AND x > ? AND y < ? AND y > ? AND z < ? AND z > ? AND world = ? GROUP BY player ORDER BY id DESC"); ps.setInt(1, location.getBlockX() + radius); ps.setInt(2, location.getBlockX() - radius); ps.setInt(3, location.getBlockY() + radius); ps.setInt(4, location.getBlockY() - radius); ps.setInt(5, location.getBlockZ() + radius); ps.setInt(6, location.getBlockZ() - radius); ps.setInt(7, worlds.indexOf(location.getWorld())); rs = ps.executeQuery(); conn.commit(); int size = 0; while (rs.next()) { String player = rs.getString("player"); int mods = rs.getInt("modifications"); modifications.put(player, mods); size++; } if (size > 0) { StringBuilder playerList = new StringBuilder(); for (Entry<String, Integer> entry : modifications.entrySet()) { playerList.append(entry.getKey()); playerList.append(" ("); playerList.append(entry.getValue()); playerList.append("), "); } playerList.delete(playerList.lastIndexOf(","), playerList.length()); for (Player player : players) { player.sendMessage(BigBrother.premessage + size + " player(s) have modified this area:"); player.sendMessage(playerList.toString()); } } else { for (Player player : players) { player.sendMessage(BigBrother.premessage + "No modifications in this area."); } } } catch (SQLException ex) { BigBrother.log.log(Level.SEVERE, "[BBROTHER]: Find SQL Exception", ex); } finally { try { if (rs != null) rs.close(); if (ps != null) ps.close(); if (conn != null) conn.close(); } catch (SQLException ex) { BigBrother.log.log(Level.SEVERE, "[BBROTHER]: Find SQL Exception (on close)"); } } }
private void mysqlFind(boolean sqlite, String playerName) { PreparedStatement ps = null; ResultSet rs = null; HashMap<Integer, Integer> creations = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> destructions = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> explosions = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> burns = new HashMap<Integer, Integer>(); Connection conn = null; try { conn = ConnectionManager.getConnection(); // TODO maybe more customizable actions? String actionString = "action IN('" + Action.BLOCK_BROKEN.ordinal() + "', '" + Action.BLOCK_PLACED.ordinal() + "', '" + Action.LEAF_DECAY.ordinal() + "', '" + Action.TNT_EXPLOSION.ordinal() + "', '" + Action.CREEPER_EXPLOSION.ordinal() + "', '" + Action.MISC_EXPLOSION.ordinal() + "', '" + Action.BLOCK_BURN.ordinal() + "')"; ps = conn.prepareStatement( "SELECT action, type FROM " + BBDataBlock.BBDATA_NAME + " WHERE " + actionString + " AND rbacked = 0 AND x < ? AND x > ? AND y < ? AND y > ? AND z < ? AND z > ? AND player = ? AND world = ? order by date desc"); ps.setInt(1, location.getBlockX() + radius); ps.setInt(2, location.getBlockX() - radius); ps.setInt(3, location.getBlockY() + radius); ps.setInt(4, location.getBlockY() - radius); ps.setInt(5, location.getBlockZ() + radius); ps.setInt(6, location.getBlockZ() - radius); ps.setString(7, playerName); ps.setInt(8, worlds.indexOf(location.getWorld())); rs = ps.executeQuery(); conn.commit(); int size = 0; while (rs.next()) { Action action = Action.values()[rs.getInt("action")]; int type = rs.getInt("type"); switch (action) { case BLOCK_BROKEN: case LEAF_DECAY: if (destructions.containsKey(type)) { destructions.put(type, destructions.get(type) + 1); size++; } else { destructions.put(type, 1); size++; } break; case BLOCK_PLACED: if (creations.containsKey(type)) { creations.put(type, creations.get(type) + 1); size++; } else { creations.put(type, 1); size++; } break; case TNT_EXPLOSION: case CREEPER_EXPLOSION: case MISC_EXPLOSION: if (explosions.containsKey(type)) { explosions.put(type, explosions.get(type) + 1); size++; } else { explosions.put(type, 1); size++; } case BLOCK_BURN: if (burns.containsKey(type)) { burns.put(type, burns.get(type) + 1); size++; } else { burns.put(type, 1); size++; } break; } } if (size > 0) { StringBuilder creationList = new StringBuilder(); // creationList.append(Color.AQUA); creationList.append("Placed Blocks: "); // creationList.append(Color.WHITE); for (Entry<Integer, Integer> entry : creations.entrySet()) { creationList.append(Material.getMaterial(entry.getKey())); creationList.append(" ("); creationList.append(entry.getValue()); creationList.append("), "); } if (creationList.toString().contains(",")) creationList.delete(creationList.lastIndexOf(","), creationList.length()); StringBuilder brokenList = new StringBuilder(); // brokenList.append(Color.RED); brokenList.append("Broken Blocks: "); // brokenList.append(Color.WHITE); for (Entry<Integer, Integer> entry : destructions.entrySet()) { brokenList.append(Material.getMaterial(entry.getKey())); brokenList.append(" ("); brokenList.append(entry.getValue()); brokenList.append("), "); } if (brokenList.toString().contains(",")) brokenList.delete(brokenList.lastIndexOf(","), brokenList.length()); StringBuilder explodeList = new StringBuilder(); // brokenList.append(Color.RED); explodeList.append("Exploded Blocks: "); // brokenList.append(Color.WHITE); for (Entry<Integer, Integer> entry : explosions.entrySet()) { explodeList.append(Material.getMaterial(entry.getKey())); explodeList.append(" ("); explodeList.append(entry.getValue()); explodeList.append("), "); } if (explodeList.toString().contains(",")) explodeList.delete(explodeList.lastIndexOf(","), explodeList.length()); StringBuilder burnList = new StringBuilder(); // brokenList.append(Color.RED); burnList.append("Burned Blocks: "); // brokenList.append(Color.WHITE); for (Entry<Integer, Integer> entry : burns.entrySet()) { burnList.append(Material.getMaterial(entry.getKey())); burnList.append(" ("); burnList.append(entry.getValue()); burnList.append("), "); } if (burnList.toString().contains(",")) burnList.delete(burnList.lastIndexOf(","), burnList.length()); for (Player player : players) { player.sendMessage( BigBrother.premessage + playerName + " has made " + size + " modifications"); if (creations.entrySet().size() > 0) player.sendMessage(creationList.toString()); if (destructions.entrySet().size() > 0) player.sendMessage(brokenList.toString()); if (explosions.entrySet().size() > 0) player.sendMessage(explodeList.toString()); if (burns.entrySet().size() > 0) player.sendMessage(burnList.toString()); } } else { for (Player player : players) { player.sendMessage( BigBrother.premessage + playerName + " has no modifications in this area."); } } } catch (SQLException ex) { BigBrother.log.log(Level.SEVERE, "[BBROTHER]: Find SQL Exception", ex); } finally { try { if (rs != null) rs.close(); if (ps != null) ps.close(); if (conn != null) conn.close(); } catch (SQLException ex) { BigBrother.log.log(Level.SEVERE, "[BBROTHER]: Find SQL Exception (on close)"); } } }