3

I am using netbeans and jdk 7 updt 9 with 1.7 and following is my code.

public class jd {
    public static void main(String[] args) throws ClassNotFoundException, SQLException 
{
        
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1158:ORCL","system", "system");
        System.out.println("Connection successful");
       // Statement s = con.createStatement();
       
    }
    
}

output is

 run:
    Exception in thread "main" java.sql.SQLRecoverableException: IO Error: Got minus one from a read call
        at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
        at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:715)
        at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:385)
        at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
        at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:564)
        at java.sql.DriverManager.getConnection(DriverManager.java:571)
        at java.sql.DriverManager.getConnection(DriverManager.java:215)
        at jd.main(jd.java:22)
    Caused by: oracle.net.ns.NetException: Got minus one from a read call
        at oracle.net.ns.Packet.receive(Packet.java:314)
        at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:153)
        at oracle.net.ns.NSProtocol.connect(NSProtocol.java:263)
        at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1360)
        at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
        ... 7 more

Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
RubioRic
  • 2,442
  • 4
  • 28
  • 35
gms
  • 51
  • 1
  • 1
  • 3

4 Answers4

3

DriverManager throws this error when it tries to get a connection with invalid URL. Make sure your URL is valid. Things to checkout:

  1. Port: Oracle db runs on 1521. (note: don't confuse yourself with webport of oracle, which can be any other port as in your URL 1158).
  2. DB name: Get the db name from Oracle Web UI: (home->adminIstration->aboutdatabase->settings)
  3. URL format: "jdbc:oracle:thin:@localhost:1521:YourDbName"
Aimee Borda
  • 842
  • 2
  • 11
  • 22
Techflash
  • 707
  • 7
  • 15
2

I think the error is in the port number 1158 in this line:

    Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1158:ORCL","system", "system");

Normally you connect to an Oracle database using port 1521. Try replacing 1158 with 1521.

Your database may be running the Enterprise Manager on port 1158. That's a web application that you access, often by navigating to https://localhost:1158/em. The Oracle database itself will typically be listening on port 1521.

When using JDBC you need to connect direct to the database, not to some web application instead. The Oracle JDBC driver understands the proprietary binary protocol it uses to communicate with the database, but it doesn't understand the HTTP it gets back from the Enterprise Manager web application. Hence you get an odd network error. You can expect similar random network errors if you attempt to connect the JDBC driver to something else that isn't an Oracle database either.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • nothing is working, i tried all , its not getting connected using db services , oracle thin , using same setup , mysql connector driver connection is successfull , but oracle sql 11g is not working. – gms Jul 30 '15 at 16:32
  • 2
    'Nothing is working, I tried all'. Evidently you didn't, as if you did try everything your connection to Oracle would be working by now. So what happened when you changed the port number, as I suggested? What error message did you get? What else have you tried, and what was the outcome in each case (full error messages in each case, please, none of this "it's not working" vagueness). Edit your question to the details of everything you have tried and the results in each case. – Luke Woodward Jul 31 '15 at 19:00
1

oracle.jdbc.driver.OracleDriver is deprecated. You will probably have better success by replacing it with oracle.jdbc.OracleDriver

See: http://www.oracle.com/technetwork/database/enterprise-edition/111070-readme-083278.html and Difference between Oracle jdbc driver classes?

Community
  • 1
  • 1
JFPicard
  • 5,029
  • 3
  • 19
  • 43
  • nothing is working, i tried all , its not getting connected using db services , oracle thin , using same setup , mysql connector driver connection is successfull , but oracle sql 11g is not working. – gms Jul 30 '15 at 16:32
0

Try this code:

OracleDataSource ods = new OracleDataSource();    
ods.setUser(DB_USER));    
ods.setPassword(DB_PASSWORD);    
ods.setURL(jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename))));    
// New AutoClosable syntax applicable to connection. This syntax will    
// close the connection automatically    
try (OracleConnection connection = (OracleConnection) (ods.getConnection())) {      
 // Statement and ResultSet are AutoClosable by this syntax   
 try (Statement statement = connection.createStatement()) {      
    try (ResultSet resultSet = statement.executeQuery("select sysdate from dual")) {        
      while (resultSet.next())          
           System.out.print("Today's Date is: " + resultSet.getString(1));

    }   
  }
}
Nirmala
  • 1,278
  • 1
  • 10
  • 11