Advertisement
Guest User

Untitled

a guest
Apr 5th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package pkgAsignment2;
  7. /**
  8.  * @author Andrew McCollam
  9.  */
  10.  
  11. //import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.Statement;
  15. import java.sql.SQLException;
  16. import java.util.Scanner;
  17.  
  18. public class Connection {
  19.    
  20.     /**
  21.      * @param args the command line arguments
  22.      */
  23.     public static void main(String[] args) {
  24.        
  25.       // the following statement loads the Oracle  jdbc driver
  26.       // make sure it is in the CLASSPATH
  27.  
  28.     try {                                              
  29.       Class.forName ("oracle.jdbc.driver.OracleDriver");
  30.     } catch (ClassNotFoundException e) {
  31.         System.out.println ("Could not load the driver");
  32.       }
  33.  
  34.     String user = "SYSTEM", pass = "Labuser1", host = "localhost", servicename;
  35.    
  36.     Scanner strInput = new Scanner(System.in);
  37.     System.out.println("Type userid, password, hostname or ipaddress: ");
  38. //    user = strInput.next();
  39. //    pass = strInput.next();
  40. //    host = strInput.next();
  41.     servicename = "orcl";
  42.     System.out.println(user+" "+pass+" "+host);
  43.  
  44.     //  userid, password and hostname are obtained from the console
  45.    
  46.     try (                                     //try-with-resources
  47.             java.sql.Connection conn = DriverManager.getConnection
  48.             ("jdbc:oracle:thin:@" + host + ":1521:" + servicename, user, pass)      
  49.            
  50.  
  51. //         Connection conn = DriverManager.getConnection
  52. //         ("jdbc:oracle:thin:"+user+"/"+pass+"@"+host+":1521/"+servicename)
  53.         )//try
  54.     {
  55.  
  56.      
  57.  
  58.     /* JDBC default is to commit each SQL statement as it is sent to the database.  
  59.        Setting autocommmit=false changes the default behaviour so that transactions
  60.        have to be committed explicity.
  61.      */
  62.     conn.setAutoCommit(false);
  63.            
  64.     // Creating a statement lets us issue commands against the connection.
  65.  
  66.     Statement s = conn.createStatement();
  67.    
  68.            
  69.      // Creating and populating Actor table
  70.  
  71.      s.executeUpdate("create table Actor( ActorName VARCHAR(20)  PRIMARY KEY,  Sex VARCHAR(6), Nationality VARCHAR(20), DOB DATE,  DebutFilmTitle VARCHAR(30))");
  72.      System.out.println("Created table Actor");
  73.            
  74.      s.executeUpdate("insert into Actor values('George Clooney', 'Male', 'American', Date'1965-12-04','Casualty')");
  75.  
  76.      conn.commit();
  77.      System.out.println("Inserted some actors");
  78.  
  79.            
  80.            
  81.      ResultSet result=s.executeQuery("Select * From Actor");
  82.      System.out.println("Results: ");
  83.      while(result.next()) {
  84.      System.out.println(result.getString(1) +"  "+ result.getString(2) + "  "+result.getString(3) +"  "+  result.getString(4)+"  "+ result.getString(5) );
  85.      }
  86.  
  87.  
  88.      // We end the transaction and the connection.
  89.  
  90.      conn.commit();
  91.  
  92.  
  93.      conn.close();
  94.          }
  95.     catch (SQLException se) {
  96.        
  97.         se.printStackTrace(System.err);
  98.        
  99.     }//catch
  100.   }//main
  101.    
  102. }//class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement