Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. package test;
  2.  
  3. import java.sql.*;
  4. import static java.lang.System.out;
  5. import static java.lang.System.err;
  6.  
  7. public class App implements Runnable {
  8.  
  9.     private final String DB_URL  = "jdbc:mysql://localhost:3306/test?user=root&password=dev";
  10.  
  11.     public static void main(String[] args) {
  12.         new App().run();
  13.     }
  14.  
  15.     protected Connection createConnection() throws SQLException {
  16.         return DriverManager.getConnection(DB_URL);
  17.     }
  18.  
  19.     public void run() {
  20.         Connection connection = null;
  21.         try {
  22.             connection = createConnection();
  23.  
  24.             out.println("--------------------------------------");
  25.             out.println("connection.getAutoCommit() = " + connection.getAutoCommit());
  26.             out.println("connection.getClientInfo() = " + connection.getClientInfo());
  27.             out.println("connection.getMetaData() = " + connection.getMetaData());
  28.             out.println("connection.getCatalog() = " + connection.getCatalog());
  29.             out.println("connection.getTransactionIsolation() = " + connection.getTransactionIsolation());
  30.             out.println("--------------------------------------");
  31.  
  32.             testUpdatableResultSet(connection);
  33.         } catch (SQLException e) {
  34.             System.err.println(e);
  35.         } finally {
  36.             try {
  37.                 if (connection != null)
  38.                     connection.close();
  39.             } catch (SQLException e) {
  40.                 System.err.println(e);
  41.             }
  42.         }
  43.     }
  44.  
  45.     private void testUpdatableResultSet(Connection connection) throws SQLException {
  46.         Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
  47.         statement.execute("select * from users");
  48.         ResultSet resultSet = statement.getResultSet();
  49.  
  50.         while (resultSet.next()) {
  51.             resultSet.updateString("firstName", resultSet.getString("lastName"));
  52.             resultSet.updateRow();
  53.         }
  54.  
  55.         if (resultSet.first()) {
  56.             while (resultSet.next()) {
  57.                 out.println("resultSet.getString(\"firstName\") = " + resultSet.getString("firstName"));
  58.                 out.println("resultSet.getString(\"lastName\") = " + resultSet.getString("lastName"));
  59.             }
  60.         } else {
  61.             err.println("Cannot set cursor position at first row");
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement