Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1.  
  2.         Connection con = null;
  3.         try {
  4.             Class.forName("org.postgresql.Driver");
  5.             con = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/", "kyz", "rahasia");
  6.         } catch (Exception e) {
  7.             System.err.println(e);
  8.             System.exit(0);
  9.         }
  10.         PreparedStatement pst = null;
  11.         ResultSet rs = null;
  12.         if (con != null) {
  13.             try {
  14.                 pst = con.prepareStatement("SELECT columnfoo FROM mytable WHERE columnfoo = ?");
  15.                 pst.setString(1, "test2");
  16.                 //pst.setInt(1, 21314);
  17.                 rs = pst.executeQuery();
  18.                 while (rs.next()) {
  19.                     System.out.print("Column 1 returned ");
  20.                     System.out.println(rs.getString(1));
  21.                 }
  22.                 rs.close();
  23.                 pst.close();
  24.  
  25.                 con.setAutoCommit(false);
  26.                 Statement st = con.createStatement();
  27.  
  28.                 st.setFetchSize(3);
  29.                 rs = st.executeQuery("SELECT * FROM mytable");
  30.                 while (rs.next()) {
  31.                     System.out.print("a row was returned.");
  32.                     System.out.println(rs.getString(1));
  33.                 }
  34.                 rs.close();
  35.  
  36.                 st.setFetchSize(0);
  37.                 rs = st.executeQuery("SELECT * FROM mytable");
  38.                 while (rs.next()) {
  39.                     System.out.print("many rows were returned.");
  40.                     System.out.println(rs.getString(1));
  41.                 }
  42.                 rs.close();
  43.  
  44.                 st.close();
  45.                 st = con.createStatement();
  46.                 st.executeUpdate("INSERT INTO test VALUES(1)");
  47.  
  48.  
  49.             } catch (Exception e) {
  50.                 System.err.println(e);
  51.                 System.exit(0);
  52.  
  53.             }
  54.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement