Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.sql.*;
- public class App implements Runnable {
- private final String DB_URL = "jdbc:mysql://localhost:3306/test?user=root&password=dev";
- public static void main(String[] args) {
- new App().run();
- }
- protected Connection createConnection() throws SQLException {
- return DriverManager.getConnection(DB_URL);
- }
- public void run() {
- Connection connection = null;
- try {
- connection = createConnection();
- System.out.println("--------------------------------------");
- System.out.println("connection.getAutoCommit() = " + connection.getAutoCommit());
- System.out.println("connection.getClientInfo() = " + connection.getClientInfo());
- System.out.println("connection.getMetaData() = " + connection.getMetaData());
- System.out.println("connection.getCatalog() = " + connection.getCatalog());
- System.out.println("connection.getTransactionIsolation() = " + connection.getTransactionIsolation());
- System.out.println("--------------------------------------");
- testUpdatableResultSet(connection);
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- try {
- if (connection != null)
- connection.close();
- } catch (SQLException e) {
- System.err.println(e);
- }
- }
- }
- private void testUpdatableResultSet(Connection connection) throws SQLException {
- Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
- statement.execute("select * from users");
- ResultSet resultSet = statement.getResultSet();
- while (resultSet.next()) {
- resultSet.updateString("firstName", resultSet.getString("lastName"));
- resultSet.updateRow();
- }
- if (resultSet.first()) {
- while (resultSet.next()) {
- System.out.println("resultSet.getString(\"firstName\") = " + resultSet.getString("firstName"));
- System.out.println("resultSet.getString(\"lastName\") = " + resultSet.getString("lastName"));
- }
- } else {
- System.err.println("Cannot set cursor position at first row");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement