你好,我试图连接到Oracle 11g数据库使用ojdbc14 jar在eclipseKepler与java 8在windows 7操作系统。但当我运行的代码,我得到以下错误。这里是我的错误相应。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
public class JDBCExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter your databse details");
System.out.println("user name");
String uName = sc.next();
System.out.println("password");
String pWord = sc.next();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
try {
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
// jdbc:oracle:thin:@server:1521:xe
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null) {
System.out.println("Successfully connected to DB");
} else {
System.out.println("Failed to connect to DB");
}
}
}
错误如下。java.sql.SQLException: Io exception: The Network Adapter could not establish the connection at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255) at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387) at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414) at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165) at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35) at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at JDBCExample.main(JDBCExample.java:23)
3条答案
按热度按时间a14dhokn1#
从我们的对话中,你可以从命令提示符下telnet &连接到数据库。在浏览了jdbcurl之后,我发现了错误。
如果xe是数据库的SID,则localhost:1521/xe应为localhost:1521:xe。
将此代码从
结束日期
看看这个article
3zwtqj6y2#
从services.msc启动oracle监听器服务。
vof42yt13#
缺少导入oracle.jdbc.驱动程序。OracleDriver