Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.sql.*;
  2. import oracle.jdbc.pool.*;
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public class DBAccess{
  7.    
  8.     //The Connect String
  9.     static final String connect_string=
  10.                     "jdbc:oracle:thin:system/db_147@//localhost:1521/XE";
  11.     //The query we will execute
  12.      String query = "select 'Hello JDBC: ' || sysdate from dual";
  13.     //The Connection to the database
  14.     private Connection conn;
  15.    
  16.     public static void main(String args[]){
  17.         new DBAccess();
  18.     }
  19.    
  20.     public DBAccess(){
  21.         try{
  22.             //Seeif we need to open the connection to the database
  23.             if(conn==null){
  24.                 //Create a OracleDataSOurce instance and set URL
  25.                 OracleDataSource ods = new OracleDataSource();
  26.                 ods.setURL(connect_string);
  27.                 //Connect to the database
  28.                 System.out.println("Connecting to " + connect_string + "\n");
  29.                 conn=ods.getConnection();
  30.                 System.out.println("Connected\n");
  31.             }
  32.             //Create a statement
  33.             Statement stmt = conn.createStatement();
  34.             //Execute the query
  35.             System.out.println("Executing query" + query + "\n");
  36.             ResultSet rset = stmt.executeQuery(query);
  37.             //Dump the result
  38.             while(rset.next()){
  39.                 System.out.println(rset.getString (1) + "\n");
  40.                 //System.out.println(rset.getString("FName"));
  41.                 //System.out.println(rset.getString (" LName") + "\n");
  42.             }
  43.             //We're done
  44.             System.out.println("done.\n");
  45.         }
  46.         catch(Exception e){
  47.             System.out.println(e.getMessage() + "\n");
  48.         }
  49.     }
  50.    
  51.     public void addStudent(String No, String Fname, String Lname){
  52.         try{
  53.             Statement stmt = conn.createStatement();
  54.             query="insert into stu values("+No+",'"+Fname+"','"+Lname+"')";
  55.             System.out.println("Executing query" + query + "\n");
  56.             stmt.executeQuery(query);
  57.         }catch(Exception e){
  58.             System.out.println(e);
  59.         }
  60.     }
  61.    
  62.     public Student getStudent(){
  63.         try{
  64.             Statement stmt = conn.createStatement();
  65.             query="select ";
  66.             System.out.println("Executing query" + query + "\n");
  67.             stmt.executeQuery(query);
  68.         }catch(Exception e){
  69.             System.out.println(e);
  70.         }
  71.     }
  72.    
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement