java.sql.SQLException:Io 异常:在与 oracle 的 JDBC 连接期间从读取调用中得到减一
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2363680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.sql.SQLException: Io exception: Got minus one from a read call during JDBC connection with oracle
提问by Karthik
Hi I am new to java when I tried to connect oracle with my java sample code I got the above exception
嗨,当我尝试将 oracle 与我的 Java 示例代码连接时,我是 Java 新手,但出现上述异常
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
帮我解决这个问题
采纳答案by BalusC
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.
一是连接网址错误。Post 8080 通常由 Apache Tomcat 等网络服务器使用。Oracle 本身使用默认端口 1521。另请参阅此 Oracle JDBC 文档。
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 beforethe first row. Any getXXX()
calls on the ResultSet
will fail if you don't move the cursor.
此外你忘记打电话了ResultSet#next()
。这会将游标设置为结果集中的下一行。结果集与第一行之前的游标一起返回。如果您不移动光标,则getXXX()
对 的任何调用ResultSet
都将失败。
If you expect multiple rows in a result set, then you need to use while
loop:
如果您希望结果集中有多行,那么您需要使用while
循环:
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:
或者,如果您只期望一行,那么您也可以继续使用以下if
语句:
resultSet = statement.executeQuery();
if (resultSet.next()) {
String columnname = resultSet.getString("columnname");
// ...
}
For more hints and examples of using basicJDBC the right way (also in JSP/Servlet) you may find this articleuseful. 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.
有关以正确方式(也在 JSP/Servlet 中)使用基本JDBC 的更多提示和示例,您可能会发现本文很有用。例如,您关闭语句和连接的方式容易导致资源泄漏。在 GET 请求上加载 JDBC 驱动程序也不必要地昂贵。只需在应用程序启动或 servlet 初始化期间执行一次。
回答by a'r
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.
通常 Oracle 使用端口 1521 进行数据库访问,而您似乎使用端口 8080。您应该检查以确保您指定了正确的端口。
回答by Paul
One error i see is that you need to do a rs.next(); This will get tot he first resultset.
我看到的一个错误是你需要做一个 rs.next(); 这将得到第一个结果集。
for example
例如
while (!rs.next()){
//read rs.getString(1);
}
回答by Hassan
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;
}
}
}
回答by subhani
solution 1 :
解决方案1:
I think this exception is due to operating system internal environment problem.
我认为这个异常是由于操作系统内部环境问题。
I have got same problem with type 4driver. But type 1driver not giving this exception. So that currently I'm using type 1driver.
我对类型 4驱动程序有同样的问题。但是类型 1驱动程序没有给出这个例外。所以目前我正在使用类型 1驱动程序。
Check port number, sid
at tnsnames.ora
检查端口号,sid
在tnsnames.ora
C:\oraclexe\app\oracle\product\10.2.0\server\NETWORK\ADMIN\SAMPLE\tnsnames.ora
C:\oraclexe\app\oracle\product\10.2.0\server\NETWORK\ADMIN\SAMPLE\tnsnames.ora
solution 2 :
解决方案2:
install vmwareon your computer, install OS, then program will work with type 4driver.
在您的计算机上安装 vmware,安装操作系统,然后程序将使用类型 4驱动程序。
回答by user2407102
I know this thread is a bit old, but here is what worked for me.
我知道这个线程有点旧,但这是对我有用的。
check the connection using a oracle db client like sql developer or something else to make sure the connection string is working and able to connect to the db with it. If you are using Oracle db Express Edition which is XE, SID is XE and port is 1521 no matter where the web client is running. you can also check this from the web client settings and other places.
使用 oracle db 客户端(如 sql developer 或其他东西)检查连接,以确保连接字符串正常工作并能够使用它连接到 db。如果您使用的是 XE 的 Oracle db Express Edition,则无论 Web 客户端在哪里运行,SID 都是 XE,端口是 1521。您还可以从 Web 客户端设置和其他地方进行检查。
回答by Jatinder Kumar
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.2.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora
Here you find the file like this:
在这里你可以找到这样的文件:
**abcd** =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = **1521**))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = abcd)
)
)
Now you write your connection as follow:
现在你写你的连接如下:
("jdbc:oracle:thin:@localhost:1521:abcd","your_db_name","your_password")
After this you don't get the exception.
在此之后,你不会得到例外。