请给我一个精确的步骤解决方案。 我正在使用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();
}
}
输出
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)
答案 0 :(得分:1)
我认为错误在此行的端口号1158中:
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1158:ORCL","system", "system");
通常使用端口1521连接到Oracle数据库。尝试用1521替换1158。
您的数据库可能正在端口1158上运行企业管理器。这是您访问的Web应用程序,通常是导航到https://localhost:1158/em。 Oracle数据库本身通常会侦听端口1521。
使用JDBC时,您需要直接连接到数据库,而不是连接到某些Web应用程序。 Oracle JDBC驱动程序了解它用于与数据库通信的专有二进制协议,但它不了解从企业管理器Web应用程序返回的HTTP。因此,您会收到奇怪的网络错误。如果您尝试将JDBC驱动程序连接到不属于Oracle数据库的其他内容,则可能会出现类似的随机网络错误。
答案 1 :(得分:1)
当DriverManager尝试获取无效URL连接时会抛出此错误。确保您的网址有效。 要结账的事情:
"jdbc:oracle:thin:@localhost:1521:YourDbName"
答案 2 :(得分:0)
oracle.jdbc.driver.OracleDriver
已弃用。通过将其替换为oracle.jdbc.OracleDriver
请参阅:http://www.oracle.com/technetwork/database/enterprise-edition/111070-readme-083278.html 和 Difference between Oracle jdbc driver classes?
答案 3 :(得分:0)
试试这段代码:
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));
}
}
}