Advertisement
Guest User

Untitled

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