Java.sql.SQLException: Io exception: Received minus one of the read call during JDBC connection with oracle

Hi, I am new to java when trying to connect to oracle with my sample Java code. I got the above exception.

My code

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(); } } } 

. The exception 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 figure this out

+6
java oracle jdbc
source share
7 answers

The connection url is invalid first. Message 8080 is commonly used by a web server such as Apache Tomcat. Oracle itself uses the default port 1521. Also see this Oracle JDBC documentation .

Next, you forgot to call ResultSet#next() . This will set the cursor to the next line in the result set. The result set is returned with the cursor before the first row. Any getXXX() calls to the ResultSet will fail if you do not move the cursor.

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

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

Or, if you expect only one line, you can also execute the if :

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

For more tips and examples on how to use basic JDBC correctly (also in JSP / Servlet), you can find this article . How you close an expression and a connection, for example, is subject to a resource leak. Also, loading a JDBC driver on a GET request is unnecessarily expensive. Just do this once during application startup or servlet initialization.

+10
source share

Normally, Oracle uses port 1521 to access the database, and instead you use port 8080. Make sure you specify the correct port.

+1
source share

One error I see is that you need to do rs.next (); This will get the first results.

eg

 while (!rs.next()){ //read rs.getString(1); } 
+1
source share
 package testing; import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; import java.sql.*; public class OracleJDBC { public static void main(String[] argv) { System.out.println("-------- Oracle JDBC Connection Testing ------"); try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { System.out.println("Where is your Oracle JDBC Driver?"); e.printStackTrace(); return; } System.out.println("Oracle driver registered"); Connection conn=null; try { conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orclh", "scott", "tiger"); Statement stmt= conn.createStatement(); ResultSet r=stmt.executeQuery("Select * from emp"); while(r.next()){ String str= r.getString("ename"); System.out.println (str); } } catch (SQLException e) { System.out.println("Connection Failed! Check output console"); e.printStackTrace(); return; } } } 
0
source share

solution 1:

I think this exception is due to a problem in the internal environment of the operating system.

I have the same problem with a Type 4 driver. But a Type 1 driver does not give this exception. So I'm currently using type 1 driver.

Check port number, sid at tnsnames.ora

C:\oraclexe\app\oracle\product\10.2.0\server\NETWORK\ADMIN\SAMPLE\tnsnames.ora

solution 2:

install vmware on your computer, install the OS, then the program will work with a type 4 driver.

0
source share

I know this thread is a bit outdated, but here's what worked for me.

test the connection using an oracle db client, such as sql developer, or something else to make sure the connection string is working and can connect to db with it. If you are using Oracle db Express Edition, which is XE, the SID is XE, and the port is 1521 regardless of where the web client is running. You can also check this from the web client settings and other places.

0
source share
 This is occur due to wrong connectivity with database connection. In your program you write Connection con = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:8080:orcl", "system","tiger"); Go to D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora 

Here you will find the file as follows:

 **abcd** = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = **1521**)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = abcd) ) ) 

Now you write your connection as follows:

  ("jdbc:oracle:thin:@localhost:1521:abcd","your_db_name","your_password") 

After that, you will not get an exception.

-one
source share

All Articles