使用 JDBC 连接 Oracle 数据库的 URL 字符串格式
我是 Java 相关 Web 开发的新手,我似乎无法使用 JDBC 工作的简单程序.我使用的是现成的 Oracle 10g XE 和 Eclipse EE IDE.从我目前检查过的书籍和网页来看,我已将问题缩小到错误编写的数据库 URL 或缺少 JAR 文件.我收到以下错误:
I'm a newbie to Java-related web development, and I can't seem to get a simple program with JDBC working. I'm using off-the-shelf Oracle 10g XE and the Eclipse EE IDE. From the books and web pages I've checked so far, I've narrowed the problem down to either an incorrectly written database URL or a missing JAR file. I'm getting the following error:
java.sql.SQLException: 找不到适合 jdbc:oracle://127.0.0.1:8080 的驱动程序
java.sql.SQLException: No suitable driver found for jdbc:oracle://127.0.0.1:8080
使用以下代码:
import java.sql.*;
public class DatabaseTestOne {
public static void main(String[] args) {
String url = "jdbc:oracle://127.0.0.1:8080";
String username = "HR";
String password = "samplepass";
String sql = "SELECT EMPLOYEE_ID FROM EMPLOYEES WHERE LAST_NAME='King'";
Connection connection;
try {
connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
System.out.println(statement.execute(sql));
connection.close();
} catch (SQLException e) {
System.err.println(e);
}
}
}
数据库 URL 的正确格式是什么?他们被提及了很多,但我一直找不到描述.
What is the proper format for a database URL, anyways? They're mentioned a lot but I haven't been able to find a description.
编辑(分辨率):
根据 duffymo 的回答,我从 ojdbc14.jar">Oracle 的下载站点 并将其放在 Eclipse 项目的参考库中.然后我把代码的开头改成
Based on duffymo's answer, I got ojdbc14.jar
from Oracle's download site and dropped it in the Eclipse project's Referenced Libraries. Then I changed the start of the code to
...
// jdbc:oracle:thin:@<hostname>:<port>:<sid>
String url = "jdbc:oracle:thin:@GalacticAC:1521:xe";
...
它奏效了.
推荐答案
看 这里.
您的网址非常不正确.应该是这样的:
Your URL is quite incorrect. Should look like this:
url="jdbc:oracle:thin:@localhost:1521:orcl"
您也不注册驱动程序类.你想下载瘦驱动JAR,把它放在你的CLASSPATH中,让你的代码看起来更像这个一>.
You don't register a driver class, either. You want to download the thin driver JAR, put it in your CLASSPATH, and make your code look more like this.
更新:ojdbc14.jar"中的14"代表JDK 1.4.您应该将您的驱动程序版本与您正在运行的 JDK 相匹配.我打赌这意味着 JDK 5 或 6.
UPDATE: The "14" in "ojdbc14.jar" stands for JDK 1.4. You should match your driver version with the JDK you're running. I'm betting that means JDK 5 or 6.
相关文章