Advertisement
Maxim_Kuraksin

Oracle JDBC

May 27th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. package com.company;
  2. import oracle.jdbc.OracleDriver;
  3. import java.sql.*;
  4.  
  5. public class Main {
  6.     private static String SELECTSQL = "SELECT PERSON_ID AS Id, p.PERSON_DATA.FIRSTNAME AS fName, p.PERSON_DATA.LASTNAME AS lName, p.PERSON_DATA.AGE AS Age FROM PEOPLE p";
  7.     private static String INSERTSQL = "INSERT INTO PEOPLE (PERSON_ID, PERSON_DATA) VALUES (?, NEW PERSON (?, ?, ?))";
  8.  
  9.     private static Connection getDBConnection() {
  10.         Connection dbConnection = null;
  11.         try {
  12.             Class.forName("oracle.jdbc.driver.OracleDriver");
  13.         } catch (ClassNotFoundException e) {
  14.             System.out.println(e.getMessage());
  15.         }
  16.         try {
  17.             Driver driver = new OracleDriver();
  18.             DriverManager.registerDriver(driver);
  19.             dbConnection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:TestDB",
  20.                     "SYS as SYSDBA", "MySecretPassword2k18");
  21.             return dbConnection;
  22.         } catch (SQLException e) {
  23.             System.out.println(e.getMessage());
  24.         }
  25.         return dbConnection;
  26.     }
  27.  
  28.     private static void FillTable() {
  29.         try {
  30.             Connection dbConnection = getDBConnection();
  31.             PreparedStatement statement = dbConnection.prepareStatement(INSERTSQL);
  32.  
  33.             statement.setInt(1, 1);
  34.             statement.setString(2, "Maxim");
  35.             statement.setString(3, "Kuraksin");
  36.             statement.setInt(4, 20);
  37.             statement.execute();
  38.  
  39.             statement.setInt(1, 2);
  40.             statement.setString(2, "Andrey");
  41.             statement.setString(3, "Konovalov");
  42.             statement.setInt(4, 25);
  43.             statement.execute();
  44.  
  45.             statement.setInt(1, 3);
  46.             statement.setString(2, "Alexey");
  47.             statement.setString(3, "Stepanov");
  48.             statement.setInt(4, 19);
  49.             statement.execute();
  50.  
  51.             statement.setInt(1, 4);
  52.             statement.setString(2, "Igor");
  53.             statement.setString(3, "Kolesnikov");
  54.             statement.setInt(4, 20);
  55.             statement.execute();
  56.  
  57.             statement.setInt(1, 5);
  58.             statement.setString(2, "Olga");
  59.             statement.setString(3, "Volnova");
  60.             statement.setInt(4, 21);
  61.             statement.execute();
  62.         }
  63.         catch (SQLException e) {
  64.             System.out.println(e.getMessage());
  65.         }
  66.     }
  67.  
  68.     public static void main(String[] args) {
  69.         try {
  70.             FillTable();
  71.  
  72.             Connection dbConnection = getDBConnection();
  73.             PreparedStatement statement = dbConnection.prepareStatement(SELECTSQL);
  74.             ResultSet rs = statement.executeQuery();
  75.  
  76.             System.out.println(" Id  fName    lName     Age");
  77.             System.out.println("------------------------------");
  78.  
  79.             while (rs.next()) {
  80.                 String id = rs.getString("Id");
  81.                 String fName = rs.getString("fName");
  82.                 String lName = rs.getString("lName");
  83.                 String age = rs.getString("Age");
  84.  
  85.                 System.out.println(id + "\t " + fName + "\t " + lName + "\t " + age);
  86.             }
  87.  
  88.         } catch (SQLException e) {
  89.             System.out.println(e.getMessage());
  90.         }
  91.     }
  92. }
  93.  
  94.  
  95. OUTPUT:
  96.  Id  fName    lName     Age
  97. ------------------------------
  98. 1    Maxim   Kuraksin    20
  99. 2    Andrey  Konovalov   25
  100. 3    Alexey  Stepanov    19
  101. 4    Igor    Kolesnikov  20
  102. 5    Olga    Volnova     21
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement