Advertisement
Guest User

asgas

a guest
May 25th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1. package pl.edu.pw.elka.bd.lab6;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DatabaseMetaData;
  5. //import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. //import java.util.Scanner;
  10.  
  11. import oracle.jdbc.pool.OracleDataSource;
  12.  
  13. /**
  14.  * BD.A lab 6 JDBC Showcase.
  15.  *
  16.  * @author B.Twardowski <B.Twardowski@ii.pw.edu.pl>
  17.  *
  18.  */
  19. public class JDBCShowcase {
  20.  
  21.     // const's
  22.     private static String JDBC_URL = "jdbc:oracle:thin:kromasev/kromasev@ora3.elka.pw.edu.pl:1521:ora3inf";
  23.  
  24.     private OracleDataSource ods;
  25.     private Connection conn;
  26.     //private Scanner inputScanner = new Scanner(System.in);
  27.  
  28.     /**
  29.      * Initializing DB connection.
  30.      *
  31.      * @throws SQLException
  32.      */
  33.     private void init() throws SQLException {
  34.         ods = new OracleDataSource();
  35.         ods.setURL(JDBC_URL);
  36.         conn = ods.getConnection();
  37.         DatabaseMetaData meta = conn.getMetaData();
  38.         System.out.println("Successfully connected to DB. JDBC driver version is " + meta.getDriverVersion());
  39.  
  40.     }
  41.  
  42.     /**
  43.      * Closing DB connection.
  44.      *
  45.      * @throws SQLException
  46.      */
  47.     private void close() throws SQLException {
  48.         conn.close();
  49.         System.out.println();
  50.         System.out.println("Database connection successfully closed.");
  51.     }
  52.  
  53.     /**
  54.      * Showcase.
  55.      *
  56.      * @throws SQLException
  57.      */
  58.     public void doShowcase() throws SQLException {
  59.         init();
  60.         insert();
  61.         delete();
  62.         transaction1();
  63.         transaction2();
  64.         select();
  65.         close();
  66.     }
  67.  
  68.     /**
  69.      * Simple select statement.
  70.      *
  71.      * @throws SQLException
  72.      */
  73.     private void select() throws SQLException {
  74.  
  75.         // Create a statement
  76.         Statement stmt = conn.createStatement();
  77.  
  78.         // Execute SQL
  79.         ResultSet rset = stmt.executeQuery("Select nazwa,cena from zamowienie where cena < 17");
  80.  
  81.         System.out.println();
  82.         System.out.println("Select nazwa,cena from zamowienie: ");
  83.  
  84.         int i = 1;
  85.         while (rset.next()) {
  86.             System.out.println("[" + i + "]:" + rset.getString(1) + " " + rset.getString(2));
  87.             i++;
  88.         }
  89.        
  90.         rset = stmt.executeQuery("Select nazwisko,imie from uzytkownik where nazwisko = 'Kruk'");
  91.  
  92.         System.out.println();
  93.         System.out.println("Select nazwisko,imie from uzytkownik where nazwisko = 'Kruk': ");
  94.  
  95.         i = 1;
  96.         while (rset.next()) {
  97.             System.out.println("[" + i + "]:" + rset.getString(1) + " " + rset.getString(2));
  98.             i++;
  99.         }
  100.  
  101.         // close the result set, the statement and connect
  102.         rset.close();
  103.         stmt.close();
  104.     }
  105.    
  106.     /**
  107.      * Simple select statement.
  108.      *
  109.      * @throws SQLException
  110.      */
  111.     private void insert() throws SQLException {
  112.  
  113.         // Create a statement      
  114.         Statement stmt = conn.createStatement();
  115.  
  116.         // insert the data
  117.         stmt.executeUpdate("INSERT INTO skladnik " + "VALUES (11, 'Pomarancze', 'Biedronka', 4, 0)");
  118.  
  119.         System.out.println();
  120.         System.out.println("Inserted into skladnik (11, Pomarancze, Biedronka, 4, 0)");
  121.  
  122.         stmt.close();
  123.     }
  124.    
  125.     private void delete() throws SQLException {
  126.  
  127.         // Create a statement      
  128.         Statement stmt = conn.createStatement();
  129.  
  130.         // delete the data
  131.         stmt.executeUpdate("DELETE FROM skladnik WHERE id = 10");
  132.  
  133.         System.out.println();
  134.         System.out.println("Deleted from skladnik where ID = 10 ");
  135.  
  136.         stmt.close();
  137.     }
  138.  
  139.  
  140.  
  141.     /**
  142.      * Transaction showcase.
  143.      *
  144.      * @throws SQLException
  145.      */
  146.     private void transaction1() throws SQLException {
  147.  
  148.         System.out.println();
  149.         System.out.println("Transaction 1 ");
  150.  
  151.         conn.setAutoCommit(false);
  152.         Statement stmt = conn.createStatement();
  153.        
  154.         String SQL = "Update uzytkownik SET Nazwisko = 'Kruk' WHERE Nazwisko = 'Cook'";
  155.         stmt.executeUpdate(SQL);
  156.        
  157.         SQL = "Update uzytkownik SET Nazwisko = 'Bialy' WHERE Nazwisko = 'White'";
  158.         stmt.executeUpdate(SQL);
  159.        
  160.         SQL = "Update uzytkownik SET Nazwisko = 'Mlody' WHERE Nazwisko = 'Young'";
  161.         stmt.executeUpdate(SQL);
  162.        
  163.         conn.commit();
  164.         System.out.println("Zmieniono nazwiska: Cook->Kruk, White->Bialy, Young->Mlody ");
  165.         stmt.close();
  166.     }
  167.    
  168.     private void transaction2() throws SQLException {
  169.  
  170.         System.out.println();
  171.         System.out.println("Transaction 2 ");
  172.  
  173.         conn.setAutoCommit(false);
  174.         Statement stmt = conn.createStatement();
  175.        
  176.         String SQL = "UPDATE Skladnik SET Dostepnosc = 1 WHERE Cena < 7";
  177.         stmt.executeUpdate(SQL);
  178.        
  179.         SQL = "UPDATE Skladnik SET Dostepnosc = 1 WHERE Producent = 'Twimm'";
  180.         stmt.executeUpdate(SQL);
  181.        
  182.         conn.commit();
  183.         System.out.println("Zmieniono Dostepnosc skladnikow gdzie producent to Twimm i cena < 7");
  184.         stmt.close();
  185.     }
  186.  
  187.     /**
  188.      * Run showcase.
  189.      *
  190.      * @param args
  191.      */
  192.     public static void main(String[] args) throws SQLException {
  193.         JDBCShowcase showcase = new JDBCShowcase();
  194.         showcase.doShowcase();
  195.     }
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement