从 Java 程序到 SQL Developer 数据库的连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16666407/
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
Connection from a Java program to a SQL Developer database
提问by ponponke
I would like to access to a Oracle database (SQL Developer) from a Java program. I never used JDBC before. Here is what i wrote:
我想从 Java 程序访问 Oracle 数据库(SQL Developer)。我以前从未使用过 JDBC。这是我写的:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:host_name:port:database_name";
Connection con = DriverManager.getConnection(url, login, passwd);
I got an error:
我有一个错误:
[Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified
Host name, port, DB name and logins are good. Is this driver OK to communicate with SQL Developer ?
主机名、端口、数据库名和登录名都很好。此驱动程序是否可以与 SQL Developer 通信?
I don't know what to do, thanks for helping !
我不知道该怎么办,谢谢你的帮助!
采纳答案by Vineet Singla
Try this
试试这个
Class.forName ("oracle.jdbc.driver.OracleDriver");
回答by Roy
for Oracle you can use ojdbc
对于 Oracle,您可以使用 ojdbc
Class.forName("oracle.jdbc.driver.OracleDriver");
for SQL Server u can use jtds
对于 SQL Server,您可以使用 jtds
Class.forName("net.sourceforge.jtds.jdbc.Driver");
回答by Nick Holt
The JDBC driver sun.jdbc.odbc.JdbcOdbcDriver
is bridgedriver that wraps an ODBC driver as described here.
JDBC驱动程序sun.jdbc.odbc.JdbcOdbcDriver
是桥所描述的一个包装的ODBC驱动程序驱动程序在这里。
SQL Developer is an Oracle tool that acts as an IDE against the Oracle database.
SQL Developer 是一个 Oracle 工具,它充当针对 Oracle 数据库的 IDE。
To connect Java to an Oracle database you should obtain the Oracle JDBC driver and ensure the jar is on your classpath (as described in the documentation for java.sql.DriverManager
, forcing the class to be loaded is no longer necessary).
要将 Java 连接到 Oracle 数据库,您应该获取 Oracle JDBC 驱动程序并确保 jar 位于您的类路径中(如 文档中所述java.sql.DriverManager
,不再需要强制加载类)。
The important bit is the connection string, which in its simplest form for Oracle should follow the structure:
重要的一点是连接字符串,对于 Oracle 来说,它最简单的形式应该遵循以下结构:
jdbc:oracle:thin:@//host:port/service
Where:
在哪里:
- host: the hostname of the machine running Oracle
- port: the port that Oracle is listening for connections on
- service: the database instance to connect to
- 主机:运行 Oracle 的机器的主机名
- 端口:Oracle 正在侦听连接的端口
- 服务:要连接的数据库实例
The full docs are here.
完整的文档在这里。