使用 JDBC 连接 Oracle 数据库的 URL 字符串格式

URL string format for connecting to Oracle database with JDBC(使用 JDBC 连接 Oracle 数据库的 URL 字符串格式)
本文介绍了使用 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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

SQL to Generate Periodic Snapshots from Transactions Table(用于从事务表生成定期快照的SQL)
MyBatis support for multiple databases(MyBatis支持多个数据库)
Oracle 12c SQL: Missing column Headers in result(Oracle 12c SQL:结果中缺少列标题)
SQL query to find the number of customers who shopped for 3 consecutive days in month of January 2020(查询2020年1月连续购物3天的客户数量)
How to get top 10 data weekly (This week, Previous week, Last month, 2 months ago, 3 month ago)(如何每周获取前十大数据(本周、前一周、上个月、2个月前、3个月前))
Select the latest record for an Id per day - Oracle pl sql(选择每天ID的最新记录-Oracle pl SQL)