0 Replies Latest reply: Jan 30, 2007 7:21 AM by 807596 RSS

    Hi Having Problem With the Oracle SQL and JDBC Connection

    807596
      Hi friends This is my program for connecting to the Oracle Database using JDBC:Please Help me regarding this problem i was working on the problem for two days i couldnt manage a connection so please help me:
      This is the first time i am using the Oracle SQL with JDBC i used MSaccess till now so please let me know hoe to solve this; I have imported the classes12.jar and odbc14.jar and i am using "jdk1.5" with
      "Oracle 8i"
      Here is the connection program:
      package com.java.jdbc.util;

      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.SQLException;

      public class EmployeeUtil {
           public EmployeeUtil()
           {
                
           }
           public static Connection getconnection()
           {
                Connection con=null;
                try {
                     Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
                     //DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
                     con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORANT","system","manager");
                // Class.forName("sun.jdbc.odbc.OracleDriver").newInstance();
                // con = DriverManager.getConnection("jdbc:odbc:Javatut","System","manager");
                } catch (ClassNotFoundException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                } catch (SQLException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                } catch (InstantiationException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                } catch (IllegalAccessException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
           
                return con;
           }

      }

      Now here is the DB SQL program:

      package com.java.jdbc.DB;

      import java.sql.Connection;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;

      import com.java.jdbc.info.EmployeeInfo;
      import com.java.jdbc.util.EmployeeUtil;

      public class EmployeeDB {
           Connection connection=null;
           public EmployeeDB()
           {
                //this.connection=EmployeeUtil.getconnection();
           }
           
           public void select(EmployeeInfo empinfo)
           {
                this.connection=EmployeeUtil.getconnection();
                String stb = "select * from employee";
           try {
                     Statement stmt = connection.createStatement();
                     ResultSet rs = stmt.executeQuery(stb);
                     System.out.println(rs);
                     //EmployeeInfo empinfo = new EmployeeInfo();
                     while(rs.next())
                     {
                          empinfo.setEmpId(rs.getString(1));
                          empinfo.setEmpfName(rs.getString(2));
                          empinfo.setEmplName(rs.getString(3));
                          empinfo.setEmpAge(rs.getString(4));
                          empinfo.setEmpSalary(rs.getString(5));
                          System.out.println("The EmployeeID is : "+empinfo.getEmpfName());
                          System.out.println("The Employee First Name is : "+empinfo.getEmpfName());
                          System.out.println("The Employee Last Name is : "+empinfo.getEmplName());
                          System.out.println("The Employee Age is : "+empinfo.getEmpAge());
                          System.out.println("The Employee Salary is : "+empinfo.getEmpSalary());
                     }
                     connection.commit();
                } catch (SQLException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }     
           }

      }

      Here is the Info object:
      package com.java.jdbc.info;

      public class EmployeeInfo {
           private String empId;
           private String empfName;
           private String emplName;
           private String empAge;
           private String empSalary;
           public String getEmpAge() {
                return empAge;
           }
           public void setEmpAge(String empAge) {
                this.empAge = empAge;
           }
           public String getEmpfName() {
                return empfName;
           }
           public void setEmpfName(String empfName) {
                this.empfName = empfName;
           }
           public String getEmpId() {
                return empId;
           }
           public void setEmpId(String empId) {
                this.empId = empId;
           }
           public String getEmplName() {
                return emplName;
           }
           public void setEmplName(String emplName) {
                this.emplName = emplName;
           }
           public String getEmpSalary() {
                return empSalary;
           }
           public void setEmpSalary(String empSalary) {
                this.empSalary = empSalary;
           }

      }

      Here is the Controller Program:

      package com.java.jdbc.controller;

      import com.java.jdbc.DB.EmployeeDB;
      import com.java.jdbc.info.EmployeeInfo;

      public class EmployeeController {
           public static void main(String args[])
           {
                EmployeeDB empdb = new EmployeeDB();
                EmployeeInfo emp = new EmployeeInfo();
                empdb.select(emp);     
           }

      }

      I got the Following Errors: So please tell me whats Wrong:
      java.sql.SQLException: Io exception: Got minus one from a read call
           at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:114)
           at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:156)
           at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:269)
           at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:210)
           at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:251)
           at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:224)
           at java.sql.DriverManager.getConnection(Unknown Source)
           at java.sql.DriverManager.getConnection(Unknown Source)
           at com.java.jdbc.util.EmployeeUtil.getconnection(EmployeeUtil.java:18)
           at com.java.jdbc.DB.EmployeeDB.select(EmployeeDB.java:20)
           at com.java.jdbc.controller.EmployeeController.main(EmployeeController.java:11)
      Exception in thread "main" java.lang.NullPointerException
           at com.java.jdbc.DB.EmployeeDB.select(EmployeeDB.java:23)
           at com.java.jdbc.controller.EmployeeController.main(EmployeeController.java:11)

      Please let me know whats wrong in the program