如何解决从 oracle 11g jdbc 7/14 jdk 1.7 中的读取调用中得到减一的问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31704622/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:58:05  来源:igfitidea点击:

how to resolve Got minus one from a read call in oracle 11g jdbc 7/14 jdk 1.7?

javaoraclejakarta-eejdbcoracle11g

提问by gms

please give me a exact solution with steps . i am using netbeans and jdk 7 updt 9 with 1.7 and following is my code.

请给我一个带有步骤的确切解决方案。我正在使用 netbeans 和 jdk 7 updt 9 和 1.7,以下是我的代码。

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)

回答by Luke Woodward

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

我认为错误出在这一行的端口号 1158 中:

    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.

通常您使用端口 1521 连接到 Oracle 数据库。尝试将 1158 替换为 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.

您的数据库可能在端口 1158 上运行企业管理器。这是您访问的 Web 应用程序,通常通过导航到https://localhost:1158/em。Oracle 数据库本身通常会侦听端口 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.

使用 JDBC 时,您需要直接连接到数据库,而不是连接到某些 Web 应用程序。Oracle JDBC 驱动程序理解它用来与数据库通信的专有二进制协议,但它不理解它从企业管理器 Web 应用程序返回的 HTTP。因此,您会收到一个奇怪的网络错误。如果您尝试将 JDBC 驱动程序连接到不是 Oracle 数据库的其他东西,您可能会出现类似的随机网络错误。

回答by Techflash

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

当 DriverManager 尝试使用无效 URL 获取连接时,会抛出此错误。确保您的 URL 有效。结账事项:

  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"
  1. 端口:Oracle db 在 1521 上运行。(注意:不要将自己与 oracle 的 webport 混淆,它可以是您的 URL 1158 中的任何其他端口)。
  2. DB 名称:从 Oracle Web UI 获取 db 名称:(home->adminIsration->aboutdatabase->settings)
  3. 网址格式: "jdbc:oracle:thin:@localhost:1521:YourDbName"

回答by Nirmala

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

    }   
  }
}

回答by JFPicard

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

oracle.jdbc.driver.OracleDriver已弃用。通过替换它,您可能会获得更好的成功oracle.jdbc.OracleDriver

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

请参阅:http: //www.oracle.com/technetwork/database/enterprise-edition/111070-readme-083278.htmlOracle jdbc 驱动程序类之间的区别?