[英]how to resolve Got minus one from a read call in oracle 11g jdbc 7/14 jdk 1.7?
I am using netbeans and jdk 7 updt 9 with 1.7 and following is my code.我在 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();
}
}
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)
DriverManager throws this error when it tries to get a connection with invalid URL. DriverManager 在尝试使用无效 URL 获取连接时会引发此错误。 Make sure your URL is valid.
确保您的 URL 有效。 Things to checkout:
结账事项:
"jdbc:oracle:thin:@localhost:1521:YourDbName"
"jdbc:oracle:thin:@localhost:1521:YourDbName"
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 .您的数据库可能在端口 1158 上运行企业管理器。这是您访问的 Web 应用程序,通常通过导航到https://localhost:1158/em 。 The Oracle database itself will typically be listening on port 1521.
Oracle 数据库本身通常会侦听端口 1521。
When using JDBC you need to connect direct to the database, not to some web application instead.使用 JDBC 时,您需要直接连接到数据库,而不是连接到某些 Web 应用程序。 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.
Oracle JDBC 驱动程序理解它用来与数据库通信的专有二进制协议,但它不理解它从企业管理器 Web 应用程序返回的 HTTP。 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 驱动程序连接到不是 Oracle 数据库的其他东西,您可能会出现类似的随机网络错误。
oracle.jdbc.driver.OracleDriver
is deprecated. oracle.jdbc.driver.OracleDriver
已弃用。 You will probably have better success by replacing it with oracle.jdbc.OracleDriver
用
oracle.jdbc.OracleDriver
替换它可能会取得更好的成功
See: http://www.oracle.com/technetwork/database/enterprise-edition/111070-readme-083278.html and Difference between Oracle jdbc driver classes?请参阅: http : //www.oracle.com/technetwork/database/enterprise-edition/111070-readme-083278.html和Oracle jdbc 驱动程序类之间的区别?
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));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.