tags:

views:

2026

answers:

3

Hi I am new to java when I tried to connect oracle with my java sample code I got the above exception

My Code is

import java.sql.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class DbConnectivity extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
       try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:8080:orcl", "system", "tiger");\\ The Exception thrown here
        Statement stmt = con.createStatement();
        ResultSet rst = stmt.executeQuery("select * from users");
        System.out.println(rst.getString(1));
        stmt.close();
        con.close();
    } catch (ClassNotFoundException e) 
    {
        e.printStackTrace();
    } catch (SQLException e) 
    {
        e.printStackTrace();
    }
    }

}

and The exception thrown is

java.sql.SQLException: Io exception: Got minus one from a read call
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:441)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.wipro.connnection.DbConnectivity.doGet(DbConnectivity.java:16)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

Help me to sort out this

A: 

Typically Oracle uses port 1521 for database access and you appear to be using port 8080 instead. You should check to make sure you have specified the correct port.

ar
I tried with 1521 also but in that case it refused to connect with database and ora 12505 error was occured
A: 

One error i see is that you need to do a rs.next(); This will get tot he first resultset.

for example

while (!rs.next()){
  //read rs.getString(1);
}
Paul
+1  A: 

First, the connection URL is wrong. Post 8080 is normally used by a webserver like Apache Tomcat. Oracle itself uses a default port of 1521. Also see this Oracle JDBC documentation.

Further you forgot to call ResultSet#next(). This will set the cursor to the next row in the result set. The result set is returned with the cursor before the first row. Any getXXX() calls on the ResultSet will fail if you don't move the cursor.

If you expect multiple rows in a result set, then you need to use while loop:

resultSet = statement.executeQuery();
while (resultSet.next()) {
    String columnname = resultSet.getString("columnname");
    // ...
}

Or if you expect only one row, then you can also go ahead with an if statement:

resultSet = statement.executeQuery();
if (resultSet.next()) {
    String columnname = resultSet.getString("columnname");
    // ...
}

For more hints and examples of using basic JDBC the right way (also in JSP/Servlet) you may find this article useful. The way you closed the statement and connection for example is prone to resource leaking. Also loading the JDBC driver on GET request is unnecessarily expensive. Just do it once during application's startup or servlet's initialization.

BalusC
When I debug the program I found that the exception thrown at Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:8080:orcl", "system", "tiger");Can you please go through this line
Fix the connection URL first. Oracle certainly doesn't listen on port 8080. It's usually Tomcat who listens on 8080 and that's not a DB server. Oracle uses by default 1521.
BalusC
I see in the other comment that you got an ORA 12505 when using 1521, in that case just check ora-code.com: http://ora-12505.ora-code.com/
BalusC