Exemplo de update e delete usando o JDBC 1
Alguém poderia me dar um exemplo de update e delete usando o JDBC 1 ?
Flavio Barros
Curtidas 0
Respostas
Dalton
09/04/2009
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]
GOSTEI 0
Flavio Barros
09/04/2009
Já tinha visto esses tutorais. Mas não estou conseguindo fazer um DELETE.
GOSTEI 0
Dalton
09/04/2009
Ok, seguem alguns exemplos retirados do site [url=http://javaalmanac.com]JavaAlmanac[/url]
Conectando em uma base oracle:
Conectando em uma base mysql:
Carregando o Driver JDBC:
Excluindo todos registros de uma tabela:
Excluindo apenas um registro:
Alterando um registro:
Inserindo um registro:
Cya!
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
} 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
} try {
// Load the JDBC driver
String driverName = "org.gjt.mm.mysql.Driver";
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// Could not find the driver
} 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) {
} 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()); 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) {
} 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) {
}GOSTEI 0
Flavio Barros
09/04/2009
Muto obrigado, vou testar.
Abraços,
GOSTEI 0
Robsonk
09/04/2009
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.
GOSTEI 0