Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pkgAsignment2;
- /**
- * @author Andrew McCollam
- */
- //import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.sql.SQLException;
- import java.util.Scanner;
- public class Connection {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // the following statement loads the Oracle jdbc driver
- // make sure it is in the CLASSPATH
- try {
- Class.forName ("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e) {
- System.out.println ("Could not load the driver");
- }
- String user = "SYSTEM", pass = "Labuser1", host = "localhost", servicename;
- Scanner strInput = new Scanner(System.in);
- System.out.println("Type userid, password, hostname or ipaddress: ");
- // user = strInput.next();
- // pass = strInput.next();
- // host = strInput.next();
- servicename = "orcl";
- System.out.println(user+" "+pass+" "+host);
- // userid, password and hostname are obtained from the console
- try ( //try-with-resources
- java.sql.Connection conn = DriverManager.getConnection
- ("jdbc:oracle:thin:@" + host + ":1521:" + servicename, user, pass)
- // Connection conn = DriverManager.getConnection
- // ("jdbc:oracle:thin:"+user+"/"+pass+"@"+host+":1521/"+servicename)
- )//try
- {
- /* JDBC default is to commit each SQL statement as it is sent to the database.
- Setting autocommmit=false changes the default behaviour so that transactions
- have to be committed explicity.
- */
- conn.setAutoCommit(false);
- // Creating a statement lets us issue commands against the connection.
- Statement s = conn.createStatement();
- // Creating and populating Actor table
- s.executeUpdate("create table Actor( ActorName VARCHAR(20) PRIMARY KEY, Sex VARCHAR(6), Nationality VARCHAR(20), DOB DATE, DebutFilmTitle VARCHAR(30))");
- System.out.println("Created table Actor");
- s.executeUpdate("insert into Actor values('George Clooney', 'Male', 'American', Date'1965-12-04','Casualty')");
- conn.commit();
- System.out.println("Inserted some actors");
- ResultSet result=s.executeQuery("Select * From Actor");
- System.out.println("Results: ");
- while(result.next()) {
- System.out.println(result.getString(1) +" "+ result.getString(2) + " "+result.getString(3) +" "+ result.getString(4)+" "+ result.getString(5) );
- }
- // We end the transaction and the connection.
- conn.commit();
- conn.close();
- }
- catch (SQLException se) {
- se.printStackTrace(System.err);
- }//catch
- }//main
- }//class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement