Fórum Exemplo de update e delete usando o JDBC 1 #567189
09/04/2009
0
Flavio Barros
Curtir tópico
+ 0Posts
09/04/2009
Dalton
Gostei + 0
09/04/2009
Flavio Barros
Gostei + 0
09/04/2009
Dalton
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
09/04/2009
Flavio Barros
Gostei + 0
16/02/2010
Robsonk
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)