SHARE
TWEET

Untitled

a guest Jun 15th, 2017 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. /**
  9.  * Created by Dmytro.Sylaiev on 6/15/2017.
  10.  */
  11. public class Main {
  12.  
  13.     private static Connection connection;
  14.    
  15.     private static final String connectionUrl = "";
  16.  
  17.     private final static String MASTER_USER = "";
  18.  
  19.     private final static String MASTER_USER_PASSWORD = "";
  20.  
  21.     public static void main(String[] args) throws Exception {
  22.         java.lang.Class.forName("com.amazon.redshift.jdbc42.Driver");
  23.  
  24.         connection = DriverManager.getConnection(connectionUrl,MASTER_USER,MASTER_USER_PASSWORD);
  25.  
  26.         dropTable();
  27.  
  28.         createTable();
  29.  
  30.         fillTable(5);
  31.  
  32.         printTableContent();
  33.     }
  34.  
  35.     private static void dropTable() throws SQLException {
  36.         PreparedStatement st = connection.prepareStatement("DROP TABLE test_table");
  37.         st.execute();
  38.     }
  39.  
  40.     private static void createTable() throws SQLException {
  41.         PreparedStatement st1 = connection.prepareStatement("CREATE TABLE IF NOT EXISTS \"public\".\"TEST_table\""
  42.                 + "(\"tic_id\" BIGINT NOT NULL identity(1, 1),\"tic_table\" VARCHAR(30) NOT NULL); ");
  43.  
  44.         st1.execute();
  45.     }
  46.  
  47.     private static void fillTable(int numberOfRecord) throws SQLException {
  48.         PreparedStatement st3 = connection.prepareStatement("INSERT INTO test_table (tic_table) VALUES (?)");
  49.  
  50.         for (int i = 0; i < numberOfRecord; i++) {
  51.             st3.setString(1, "test");
  52.             st3.execute();
  53.         }
  54.     }
  55.  
  56.     private static void printTableContent() throws SQLException{
  57.         PreparedStatement st2 = connection.prepareStatement("SELECT * FROM test_table");
  58.  
  59.         ResultSet rs = st2.executeQuery();
  60.  
  61.         while (rs.next()) {
  62.             System.out.println(rs.getInt(1) + " | " + rs.getString(2)) ;
  63.         }
  64.     }
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top