Java Code Examples for java.sql.PreparedStatement.isClosed()

The following are Jave code examples for showing how to use isClosed() of the java.sql.PreparedStatement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: AeroStory   File: NPCConversationManager.java   Source Code and License Vote up 6 votes
public void disbandAlliance(MapleClient c, int allianceId) {
    PreparedStatement ps = null;
    try {
        ps = DatabaseConnection.getConnection().prepareStatement("DELETE FROM `alliance` WHERE id = ?");
        ps.setInt(1, allianceId);
        ps.executeUpdate();
        ps.close();
        Server.getInstance().allianceMessage(c.getPlayer().getGuild().getAllianceId(), MaplePacketCreator.disbandAlliance(allianceId), -1, -1);
        Server.getInstance().disbandAlliance(allianceId);
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    } finally {
        try {
            if (ps != null && !ps.isClosed()) {
                ps.close();
            }
        } catch (SQLException ex) {
        }
    }
}
 
Example 2
Project: Entrega-Final   File: DaoProduto.java   Source Code and License Vote up 6 votes
public static void excluirProduto(long id) throws SQLException, ClassNotFoundException {
    String sql = "DELETE FROM produto WHERE (produto.id=?)";

    Connection connection = null;

    PreparedStatement preparedStatement = null;
    try {
        connection = ConnectionUtils.getConnection();

        preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setLong(1, id);

        preparedStatement.execute();
    } finally {
        if (preparedStatement != null && !preparedStatement.isClosed()) {
            preparedStatement.close();
        }
        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
    }
}
 
Example 3
Project: my-diploma-work   File: ConnectionDB.java   Source Code and License Vote up 5 votes
public void closeConnection(Connection con, PreparedStatement ps, ResultSet rs) throws SQLException {
    if (con.isClosed() == false) {
        con.close();
    }
    if (ps.isClosed() == false) {
        ps.close();
    }
    if (rs.isClosed() == false) {
        rs.close();
    }
}
 
Example 4
Project: AeroStory   File: PortalPlayerInteraction.java   Source Code and License Vote up 5 votes
public boolean hasLevel30Character() {
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        ps = DatabaseConnection.getConnection().prepareStatement("SELECT `level` FROM `characters` WHERE accountid = ?");
        ps.setInt(1, getPlayer().getAccountID());
        rs = ps.executeQuery();
        while (rs.next()) {
            if (rs.getInt("level") >= 30) {
                return true;
            }
        }
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    } finally {
        try {
            if (ps != null && !ps.isClosed()) {
                ps.close();
            }
            if (rs != null && !rs.isClosed()) {
                rs.close();
            }
        } catch (SQLException ex) {
        }
    }
    return false;
}
 
Example 5
Project: AeroStory   File: MapleClient.java   Source Code and License Vote up 5 votes
public void updateMacs(String macData) {
    macs.addAll(Arrays.asList(macData.split(", ")));
    StringBuilder newMacData = new StringBuilder();
    Iterator<String> iter = macs.iterator();
    PreparedStatement ps = null;
    while (iter.hasNext()) {
        String cur = iter.next();
        newMacData.append(cur);
        if (iter.hasNext()) {
            newMacData.append(", ");
        }
    }
    try {
        ps = DatabaseConnection.getConnection().prepareStatement("UPDATE accounts SET macs = ? WHERE id = ?");
        ps.setString(1, newMacData.toString());
        ps.setInt(2, accId);
        ps.executeUpdate();
        ps.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (ps != null && !ps.isClosed()) {
                ps.close();
            }
        } catch (SQLException ex) {
        }
    }
}
 
Example 6
Project: Entrega-Final   File: DaoProduto.java   Source Code and License Vote up 5 votes
public static void inserirProduto(Produto produto) throws SQLException, ClassNotFoundException {
    String sql = "INSERT INTO produto (nome, descricao, "
            + "vl_compra, vl_venda, categoria, dt_cadastro)"
            + " VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)";

    Connection connection = null;

    PreparedStatement preparedStatement = null;
    try {
        connection = ConnectionUtils.getConnection();

        preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setString(1, produto.getNome());
        preparedStatement.setString(2, produto.getDescricao());
        preparedStatement.setBigDecimal(3, produto.getValorCompra());
        preparedStatement.setBigDecimal(4, produto.getValorVenda());
        preparedStatement.setString(5, produto.getCategoria());

        preparedStatement.execute();
    } finally {
        if (preparedStatement != null && !preparedStatement.isClosed()) {
            preparedStatement.close();
        }
        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
    }
}
 
Example 7
Project: Entrega-Final   File: DaoProduto.java   Source Code and License Vote up 5 votes
public static void atualizarProduto(Produto produto) throws SQLException, ClassNotFoundException {
    String sql = "UPDATE produto SET nome=?, descricao=?, vl_compra=?,"
            + " vl_venda=?, categoria=? WHERE (produto.id=?)";

    Connection connection = null;

    PreparedStatement preparedStatement = null;
    try {
        connection = ConnectionUtils.getConnection();

        preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setString(1, produto.getNome());
        preparedStatement.setString(2, produto.getDescricao());
        preparedStatement.setBigDecimal(3, produto.getValorCompra());
        preparedStatement.setBigDecimal(4, produto.getValorVenda());
        preparedStatement.setString(5, produto.getCategoria());
        preparedStatement.setLong(6, produto.getId());

        preparedStatement.execute();
    } finally {
        if (preparedStatement != null && !preparedStatement.isClosed()) {
            preparedStatement.close();
        }
        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
    }
}
 
Example 8
Project: AeroStory   File: MapleCharacter.java   Source Code and License Vote up 4 votes
public static boolean ban(String id, String reason, boolean accountId) {
    PreparedStatement ps = null;
    try {
        Connection con = DatabaseConnection.getConnection();
        if (id.matches("/[0-9]{1,3}\\..*")) {
            ps = con.prepareStatement("INSERT INTO ipbans VALUES (DEFAULT, ?)");
            ps.setString(1, id);
            ps.executeUpdate();
            ps.close();
            return true;
        }
        if (accountId) {
            ps = con.prepareStatement("SELECT id FROM accounts WHERE name = ?");
        } else {
            ps = con.prepareStatement("SELECT accountid FROM characters WHERE name = ?");
        }

        boolean ret = false;
        ps.setString(1, id);
        try (ResultSet rs = ps.executeQuery()) {
            if (rs.next()) {
                try (PreparedStatement psb = DatabaseConnection.getConnection().prepareStatement("UPDATE accounts SET banned = 1, banreason = ? WHERE id = ?")) {
                    psb.setString(1, reason);
                    psb.setInt(2, rs.getInt(1));
                    psb.executeUpdate();
                }
                ret = true;
            }
        }
        ps.close();
        return ret;
    } catch (SQLException ex) {
    } finally {
        try {
            if (ps != null && !ps.isClosed()) {
                ps.close();
            }
        } catch (SQLException e) {
        }
    }
    return false;
}
 
Example 9
Project: Lucid2.0   File: MapleCharacter.java   Source Code and License Vote up 4 votes
public static boolean ban(String id, String reason, boolean accountId) {
    PreparedStatement ps = null;
    try {
        Connection con = DatabaseConnection.getConnection();
        if (id.matches("/[0-9]{1,3}\\..*")) {
            ps = con.prepareStatement("INSERT INTO ipbans VALUES (DEFAULT, ?)");
            ps.setString(1, id);
            ps.executeUpdate();
            ps.close();
            return true;
        }
        if (accountId) {
            ps = con.prepareStatement("SELECT id FROM accounts WHERE name = ?");
        } else {
            ps = con.prepareStatement("SELECT accountid FROM characters WHERE name = ?");
        }
        boolean ret = false;
        ps.setString(1, id);
        try (ResultSet rs = ps.executeQuery()) {
            if (rs.next()) {
                try (PreparedStatement psb = DatabaseConnection.getConnection().prepareStatement("UPDATE accounts SET banned = 1, banreason = ? WHERE id = ?")) {
                    psb.setString(1, reason);
                    psb.setInt(2, rs.getInt(1));
                    psb.executeUpdate();
                }
                ret = true;
            }
        }
        ps.close();
        return ret;
    } catch (SQLException ex) {
    } finally {
        try {
            if (ps != null && !ps.isClosed()) {
                ps.close();
            }
        } catch (SQLException e) {
        }
    }
    return false;
}
 
Example 10
Project: Entrega-Final   File: DaoProduto.java   Source Code and License Vote up 4 votes
public static ArrayList<Produto> procurarProduto(String nomeProduto) throws SQLException, ClassNotFoundException {
    String sql = "SELECT * FROM produto WHERE (produto.nome LIKE(?))";

    ArrayList<Produto> listaProdutos = null;

    Connection connection = null;

    PreparedStatement preparedStatement = null;

    ResultSet result = null;
    try {
        connection = ConnectionUtils.getConnection();

        preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setString(1, "%" + nomeProduto + "%");

        result = preparedStatement.executeQuery();

        while (result.next()) {
            if (listaProdutos == null) {
                listaProdutos = new ArrayList<>();
            }
            Produto produto = new Produto();
            produto.setId(result.getLong("id"));
            produto.setNome(result.getString("nome"));
            produto.setDescricao(result.getString("descricao"));
            produto.setValorCompra(result.getBigDecimal("vl_compra"));
            produto.setValorVenda(result.getBigDecimal("vl_venda"));
            produto.setCategoria(result.getString("categoria"));
            produto.setDtCadastro(result.getDate("dt_cadastro"));

            listaProdutos.add(produto);
        }
    } finally {
        if (result != null && !result.isClosed()) {
            result.close();
        }

        if (preparedStatement != null && !preparedStatement.isClosed()) {
            preparedStatement.close();
        }

        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
    }

    return listaProdutos;
}
 
Example 11
Project: Entrega-Final   File: DaoProduto.java   Source Code and License Vote up 4 votes
public static Produto obterProduto(long id) throws SQLException, ClassNotFoundException {
    String sql = "SELECT * FROM produto WHERE (produto.id=?)";

    Connection connection = null;

    PreparedStatement preparedStatement = null;

    ResultSet result = null;
    try {
        connection = ConnectionUtils.getConnection();

        preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setLong(1, id);

        result = preparedStatement.executeQuery();
        if (result.next()) {
            Produto produto = new Produto();
            produto.setId(result.getLong("id"));
            produto.setNome(result.getString("nome"));
            produto.setDescricao(result.getString("descricao"));
            produto.setValorCompra(result.getBigDecimal("vl_compra"));
            produto.setValorVenda(result.getBigDecimal("vl_venda"));
            produto.setCategoria(result.getString("categoria"));
            produto.setDtCadastro(result.getDate("dt_cadastro"));

            return produto;
        }
    } finally {
        if (result != null && !result.isClosed()) {
            result.close();
        }
        if (preparedStatement != null && !preparedStatement.isClosed()) {
            preparedStatement.close();
        }
        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
    }

    return null;
}