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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1054105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:58:43  来源:igfitidea点击:

URL string format for connecting to Oracle database with JDBC

oraclejdbcoracle-xe

提问by Pops

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 相关 Web 开发的新手,我似乎无法使用 JDBC 工作的简单程序。我使用的是现成的 Oracle 10g XE 和 Eclipse EE IDE。从我目前检查过的书籍和网页来看,我已经将问题缩小到错误编写的数据库 URL 或缺少 JAR 文件。我收到以下错误:

java.sql.SQLException: No suitable driver found for jdbc:oracle://127.0.0.1:8080

java.sql.SQLException: 找不到适合 jdbc:oracle://127.0.0.1:8080 的驱动程序

with the following code:

使用以下代码:

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);
        }
    }
}

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.

无论如何,数据库 URL 的正确格式是什么?他们被提及了很多,但我一直无法找到描述。

EDIT (the resolution):

编辑(决议):

Based on duffymo's answer, I got ojdbc14.jarfrom Oracle's download siteand dropped it in the Eclipse project's Referenced Libraries. Then I changed the start of the code to

根据 duffymo 的回答,我ojdbc14.jarOracle 的下载站点获得并将其放入 Eclipse 项目的参考库中。然后我将代码的开头更改为

...
// jdbc:oracle:thin:@<hostname>:<port>:<sid>
String url = "jdbc:oracle:thin:@GalacticAC:1521:xe";
...

and it worked.

它奏效了。

回答by Pops

There are two ways to set this up. If you have an SID, use this (older) format:

有两种设置方法。如果您有 SID,请使用此(旧)格式:

jdbc:oracle:thin:@[HOST][:PORT]:SID

If you have an Oracle service name, use this (newer) format:

如果您有 Oracle 服务名称,请使用以下(较新的)格式:

jdbc:oracle:thin:@//[HOST][:PORT]/SERVICE

Source: this OraFAQ page

来源:这个 OraFAQ 页面

The call to getConnection()is correct.

对 的调用getConnection()是正确的

Also, as duffymo said, make sure the actual driver code is present by including ojdbc6.jarin the classpath, where the number corresponds to the Java version you're using.

此外,正如 duffymo 所说,通过包含ojdbc6.jar在类路径中来确保存在实际的驱动程序代码,其中的数字对应于您正在使用的 Java 版本。

回答by duffymo

Look here.

这里

Your URL is quite incorrect. Should look like this:

您的网址非常不正确。应该是这样的:

url="jdbc:oracle:thin:@localhost:1521:orcl"

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.

您也不注册驱动程序类。你想下载瘦驱动JAR,把它放在你的CLASSPATH中,让你的代码看起来更像这样

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.

更新:“ojdbc14.jar”中的“14”代表 JDK 1.4。您应该将您的驱动程序版本与您正在运行的 JDK 相匹配。我打赌这意味着 JDK 5 或 6。

回答by Clara

The correct format for url can be one of the following formats:

url 的正确格式可以是以下格式之一:

jdbc:oracle:thin:@<hostName>:<portNumber>:<sid>;  (if you have sid)
jdbc:oracle:thin:@//<hostName>:<portNumber>/serviceName; (if you have oracle service name)

And don't put any space there. Try to use 1521 as port number. sid (database name) must be the same as the one which is in environment variables (if you are using windows).

并且不要在那里放置任何空间。尝试使用 1521 作为端口号。sid(数据库名称)必须与环境变量中的相同(如果您使用的是 Windows)。

回答by Ketan Vishwakarma

if you are using oracle 10g expree Edition then:
1. for loading class use DriverManager.registerDriver (new oracle.jdbc.OracleDriver());2. for connecting to database use Connection conn = DriverManager.getConnection("jdbc:oracle:thin:username/password@localhost:1521:xe");

如果您使用的是 oracle 10g 快速版,则:
1. 加载类使用 DriverManager.registerDriver (new oracle.jdbc.OracleDriver());2. 连接数据库使用 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:username/password@localhost:1521:xe");

回答by Pushkar Nayama

String host = <host name>
String port = <port>
String service = <service name>
String dbName = <db schema>+"."+service
String url = "jdbc:oracle:thin:@"+host+":"+"port"+"/"+dbName

回答by Steve Prior

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());         
connection = DriverManager.getConnection("jdbc:oracle:thin:@machinename:portnum:schemaname","userid","password");

回答by Crippledsmurf

I'm not a Java developer so unfortunatly I can't comment on your code directly however I found this in an Oracle FAQ regarding the form of a connection string

我不是 Java 开发人员,很遗憾我无法直接评论您的代码,但是我在 Oracle 常见问题解答中找到了有关连接字符串形式的信息

jdbc:oracle:<drivertype>:<username/password>@<database>

From the Oracle JDBC FAQ

来自Oracle JDBC 常见问题解答

http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#05_03

http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#05_03

Hope that helps

希望有帮助