您好我正在尝试使用 JDBC 建立与 Oracle 的连接。
我在 Win 7 x86 上,运行 Oracle 10g R2 XE、Eclipse Java EE Juno 和 Java SE v1.7。我已经在 WebContent/WEB-INF/lib 中包含了 classes.12.jar 和 ojdbc5.jar,因为之前使用 ojdbc.jar 在连接数据库时出现错误。所以我很不确定我到底要包括哪个驱动程序。
我的代码是:
Connection conn = null;
String driverName = "oracle.jdbc.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";
Class.forName(driverName);
conn = DriverManager.getConnection(url, "system", "mypassword"); //exception occurs here
它在最后一行给出以下异常
java.lang.ArrayIndexOutOfBoundsException: 7
at oracle.security.o3logon.C0.r(C0)
at oracle.security.o3logon.C0.l(C0)
at oracle.security.o3logon.C1.c(C1)
at oracle.security.o3logon.O3LoginClientHelper.getEPasswd(O3LoginClientHelper)
at oracle.jdbc.ttc7.O3log.<init>(O3log.java:289)
at oracle.jdbc.ttc7.TTC7Protocol.logon(TTC7Protocol.java:251)
at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:246)
at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:365)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:260)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ConnectionManager.getConnection(ConnectionManager.java:16)
at ConnectionManager.main(ConnectionManager.java:24)
山姆做错了什么,或者我仍然使用错误的驱动程序。我应该包括哪一个?
编辑
我也试过网址:
String url = "jdbc:oracle:thin:@//localhost:1521/xe";
它给了我以下例外:
java.sql.SQLException: Io exception: Invalid connection string format, a valid format is: "host:port:sid"
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:168)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:210)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:323)
at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:260)
at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:365)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:260)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ConnectionManager.getConnection(ConnectionManager.java:17)
at ConnectionManager.main(ConnectionManager.java:25)
我还尝试将驱动程序名称更改为:
String driverName = "oracle.jdbc.driver.OracleDriver";
没用。
并且同时使用了ojdbc5.jar、ojdbc6.jar,但是没有用。:'(