![](/img/trans.png)
[英]java.sql.SQLException: Io exception: Got minus one from a read call during JDBC connection with oracle 11g
[英]how to resolve Got minus one from a read call in oracle 11g jdbc 7/14 jdk 1.7?
我在 1.7 中使用 netbeans 和 jdk 7 updt 9,以下是我的代碼。
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();
}
}
輸出是
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)
DriverManager 在嘗試使用無效 URL 獲取連接時會引發此錯誤。 確保您的 URL 有效。 結賬事項:
"jdbc:oracle:thin:@localhost:1521:YourDbName"
我認為錯誤出在這一行的端口號 1158 中:
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1158:ORCL","system", "system");
通常您使用端口 1521 連接到 Oracle 數據庫。嘗試將 1158 替換為 1521。
您的數據庫可能在端口 1158 上運行企業管理器。這是您訪問的 Web 應用程序,通常通過導航到https://localhost:1158/em 。 Oracle 數據庫本身通常會偵聽端口 1521。
使用 JDBC 時,您需要直接連接到數據庫,而不是連接到某些 Web 應用程序。 Oracle JDBC 驅動程序理解它用來與數據庫通信的專有二進制協議,但它不理解它從企業管理器 Web 應用程序返回的 HTTP。 因此,您會收到一個奇怪的網絡錯誤。 如果您嘗試將 JDBC 驅動程序連接到不是 Oracle 數據庫的其他東西,您可能會出現類似的隨機網絡錯誤。
oracle.jdbc.driver.OracleDriver
已棄用。 用oracle.jdbc.OracleDriver
替換它可能會取得更好的成功
請參閱: http : //www.oracle.com/technetwork/database/enterprise-edition/111070-readme-083278.html和Oracle jdbc 驅動程序類之間的區別?
試試這個代碼:
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));
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.