使用JDBC连接到Oracle数据库的URL字符串格式 [英] URL string format for connecting to Oracle database with JDBC

查看:1519
本文介绍了使用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的回答,我从 Oracle的下载站点获取了ojdbc14.jar 并将其放在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";
...

成功了.

推荐答案

查找此处.

您的URL完全不正确.应该看起来像这样:

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.

这篇关于使用JDBC连接到Oracle数据库的URL字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆