import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DatabaseConnection { public Connection getConnection() throws SQLException { String url = "jdbc:oracle:thin:@localhost:1521:ORCL"; String username = "your_username"; String password = "your_password"; Connection connection = DriverManager.getConnection(url, username, password); return connection; } } import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DatabaseQuery { public void executeQuery(Connection connection) throws SQLException { String sql = "SELECT * FROM employees"; PreparedStatement statement = connection.prepareStatement(sql); ResultSet resultSet = statement.executeQuery(); while(resultSet.next()) { } } public void executeUpdate(Connection connection) throws SQLException { String sql = "UPDATE employees SET salary = salary + 1000 WHERE department = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, "IT"); int rowsAffected = statement.executeUpdate(); } }


上一篇:
下一篇:
切换中文