java.sql.SQLException: Io exception: Got minus one from a read call during JDBC connection with oracle

后端 未结 7 1176
故里飘歌
故里飘歌 2020-12-19 07:52

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.IO         


        
7条回答
  •  Happy的楠姐
    2020-12-19 08:52

    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.

提交回复
热议问题