GARANTIR DESCONTO

Fórum Exemplo de update e delete usando o JDBC 1 #567189

09/04/2009

0

Alguém poderia me dar um exemplo de update e delete usando o JDBC 1 ?
Flavio Barros

Flavio Barros

Responder

Posts

09/04/2009

Dalton

Boa noite caro amigo, você já deu uma olhada na nossa [url=http://www.javafree.com.br/home/modules.php?name=TopTut]sessão tutoriais[/url]? Veja a parte de banco de dados, especificamente nestes tutoriais que seguem: [url=http://www.javafree.com.br/forum/viewtopic.php?t=1356] » Acessando banco de dados em Java (PARTE 1)[/url] [url=http://www.javafree.com.br/forum/viewtopic.php?t=1357]» Acessando Bancos de Dados em Java (PARTE 2)[/url] [url=http://www.javafree.com.br/forum/viewtopic.php?t=1358]» Acessando Bancos de Dados em Java (PARTE 3) [/url]
Responder

Gostei + 0

09/04/2009

Flavio Barros

Já tinha visto esses tutorais. Mas não estou conseguindo fazer um DELETE.
Responder

Gostei + 0

09/04/2009

Dalton

Ok, seguem alguns exemplos retirados do site [url=http://javaalmanac.com]JavaAlmanac[/url] Conectando em uma base oracle:
    Connection connection = null;
    try {
        // Load the JDBC driver
        String driverName = "oracle.jdbc.driver.OracleDriver";
        Class.forName(driverName);
    
        // Create a connection to the database
        String serverName = "127.0.0.1";
        String portNumber = "1521";
        String sid = "mydatabase";
        String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
        String username = "username";
        String password = "password";
        connection = DriverManager.getConnection(url, username, password);
    } catch (ClassNotFoundException e) {
        // Could not find the database driver
    } catch (SQLException e) {
        // Could not connect to the database
    }
Conectando em uma base mysql:
    Connection connection = null;
    try {
        // Load the JDBC driver
        String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
        Class.forName(driverName);
    
        // Create a connection to the database
        String serverName = "localhost";
        String mydatabase = "mydatabase";
        String url = "jdbc:mysql://" + serverName +  "/" + mydatabase; // a JDBC url
        String username = "username";
        String password = "password";
        connection = DriverManager.getConnection(url, username, password);
    } catch (ClassNotFoundException e) {
        // Could not find the database driver
    } catch (SQLException e) {
        // Could not connect to the database
    }
Carregando o Driver JDBC:
    try {
        // Load the JDBC driver
        String driverName = "org.gjt.mm.mysql.Driver";
        Class.forName(driverName);
    } catch (ClassNotFoundException e) {
        // Could not find the driver
    }
Excluindo todos registros de uma tabela:
    try {
        Statement stmt = connection.createStatement();
    
        // Use TRUNCATE
        String sql = "TRUNCATE my_table";
    
        // Use DELETE
        sql = "DELETE FROM my_table";
    
        // Execute deletion
        stmt.executeUpdate(sql);
    } catch (SQLException e) {
    }
Excluindo apenas um registro:
   try {
        // Create a statement
        Statement stmt = connection.createStatement();
    
        // Prepare a statement to insert a record
        String sql = "DELETE FROM my_table WHERE col_string='a string'";
    
        // Execute the delete statement
        int deleteCount = stmt.executeUpdate(sql);
        // deleteCount contains the number of deleted rows
    
        // Use a prepared statement to delete
    
        // Prepare a statement to delete a record
        sql = "DELETE FROM my_table WHERE col_string=?";
        PreparedStatement pstmt = connection.prepareStatement(sql);
        // Set the value
        pstmt.setString(1, "a string");
        deleteCount = pstmt.executeUpdate();
        System.err.println(e.getMessage());
Alterando um registro:
    try {
        Statement stmt = connection.createStatement();
    
        // Prepare a statement to update a record
        String sql = "UPDATE my_table SET col_string='a new string' WHERE col_string = 'a string'";
    
        // Execute the insert statement
        int updateCount = stmt.executeUpdate(sql);
        // updateCount contains the number of updated rows
    } catch (SQLException e) {
    }
Inserindo um registro:
   try {
        Statement stmt = connection.createStatement();
    
        // Prepare a statement to insert a record
        String sql = "INSERT INTO my_table (col_string) VALUES('a string')";
    
        // Execute the insert statement
        stmt.executeUpdate(sql);
    } catch (SQLException e) {
    }
Cya!
Responder

Gostei + 0

09/04/2009

Flavio Barros

Muto obrigado, vou testar. Abraços,
Responder

Gostei + 0

16/02/2010

Robsonk

Obrigado amigo bastou eu acrescentar as aspas simples como você indicou, irei testar com o PreparedStatement que você indicou depois posto o resultado, e respondendo a pergunta estou setando o cod_cor pois ele será o campo que indicará a alteração do valor na tabela, mas acho que não tem sentido continuiar setando ele não é?Uma vez que ele não será modificado. Obrigado, logo posto o código com o resultado.
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar