This discussion is archived
1 Reply Latest reply: Jan 26, 2013 6:16 AM by 987313 RSS

JDBC connection to Oracle 10g

987313 Newbie
Currently Being Moderated
Hi there, i m trying to do my final year project with Oracle 10g database for stock control system. As i need to connect the 10g with JDBC i could not do that. As i am really new here i am scared for that. i am posting below my code
package suru;
import java.sql.*;
import java.io.*;
import oracle.jdbc.pool.OracleDataSource;
class JdbcCheckup {
     public static void main (String args[])
     throws SQLException, IOException
     {
          System.out.println ("Please enter information to test connection to the database");
          String user;
          String password;
          String database;
          
          user = readEntry ("user: ");
          int slash_index = user.indexOf('/');
          if (slash_index != -1){
               
               password = user.substring(slash_index + 1);
               user = user.substring (0, slash_index);
          }
          else
               password = readEntry ("password: ");
          database = readEntry ("database (a TNSNAME entry, name-value pairs): ");
          
          OracleDataSource conn = new OracleDataSource();
          conn.setURL("jdbc:oracle:oci8:" + user + "/" + password + "@" + database);
          System.out.println("Connecting to the database...");
          System.out.flush();
          System.out.println("Connecting...");
          Connection conn = ods. getConnection();
          System.out.println("connected.");
          Statement stmt = conn.createStatement ();
          ResultSet rset = stmt.executeQuery("select 'Hello world'from dual");
          
          while (rset.next ())
               System.out.println (rset.getString(1));
               //conn.setAutoCommit (false);
               //Statement stmt = conn.createStatement ();
               //ResultSet rset = stmt.executeQuery ("select 'Hello wprld'from dual");
               while (rset.next ())
                    System.out.println (rset.getString (1));
               System.out.println("your JDBC installation is correct.");
               rset.close();
               stmt.close();
               conn.close();
     }
     
     static String readEntry (String promt)
     {
          try
          
          {
          StringBuffer buffer = new StringBuffer ();
          System.out.println(promt);
          System.out.flush();
          int c = System.in.read ();
          while (c!= '\n' && c!= -1)
          {
               buffer.append((char)c);
               c=System.in.read();
          }
          return buffer.toString ().trim ();
          }
          catch (IOException e)
          {
               return "";
               
          }
     }
}

i did it on Eclipse. But did not work. I also installed Oracle 10g on my laptop. Any solution would be appreciated. i need help........... :(

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points