import java.sql.*;
public class OracleJDBC {
public static void main(String[] args) {
String url = "jdbc:oracle:thin:@localhost:1521:TestDB";
String user = "kdhong";
String pass = "kdhong";
Connection conn = null;
String sql = null;
String query = null;
int result;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("success!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.err.println("error1 = " + e.getMessage());
System.exit(1);
}
try {
conn = DriverManager.getConnection(url, user, pass);
System.out.println("success!");
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
sql = "create table testtable(" +
"id int)";
result = stmt.executeUpdate(sql);
if(result == 0)
System.out.println("table created!!");
conn.commit();
conn.setAutoCommit(true);
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我正在使用 Oracle DB 11g 并尝试将其与我的Java应用程序连接。
我在我的eclipse IDE中尝试了这个Java代码。我在 jre / lib / ext 文件夹中安装了 ojdbc6 ,并执行了该程序。
类DriverManager已成功创建。但我的代码挂在conn=DriverManager.getConnection(url, user, pass);
语句上。我打印了跟踪堆栈。
java.sql.SQLException: Socket read timed out
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:412)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:531)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:221)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:503)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at OracleJDBC.main(OracleJDBC.java:24)
Caused by: oracle.net.ns.NetException: Socket read timed out
at oracle.net.ns.Packet.receive(Packet.java:320)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:286)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1042)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:301)
... 7 more
Exception in thread "main" java.lang.NullPointerException
at OracleJDBC.main(OracleJDBC.java:31)
我已经检查过OracleOraDb11g_home1TNSListener服务正在服务中。那么什么样的其他问题会导致这种连接问题呢?