如何使用 JDBC API 在 Java 中建立数据库连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2710303/
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
How to make database connectivity in Java using JDBC API?
提问by Wangge
How to create a jdbc connection in Java?
如何在 Java 中创建 jdbc 连接?
回答by Tom
See sun's jdbc tutorial for begginers
Particularly http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html
特别是http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html
First, you need to establish a connection with the DBMS you want to use. Typically, a JDBC? application connects to a target data source using one of two mechanisms:
DriverManager: This fully implemented class requires an application to load a specific driver, using a hardcoded URL. As part of its initialization, the DriverManager class attempts to load the driver classes referenced in the jdbc.drivers system property. This allows you to customize the JDBC Drivers used by your applications.
DataSource: This interface is preferred over DriverManager because it allows details about the underlying data source to be transparent to your application. A DataSource object's properties are set so that it represents a particular data source.
首先,您需要与要使用的 DBMS 建立连接。通常,JDBC?应用程序使用以下两种机制之一连接到目标数据源:
DriverManager:这个完全实现的类需要应用程序使用硬编码的 URL 加载特定的驱动程序。作为其初始化的一部分,DriverManager 类尝试加载 jdbc.drivers 系统属性中引用的驱动程序类。这允许您自定义应用程序使用的 JDBC 驱动程序。
DataSource:此接口优于 DriverManager,因为它允许有关底层数据源的详细信息对您的应用程序透明。设置 DataSource 对象的属性以便它表示特定的数据源。
Connection con = DriverManager.getConnection
( "jdbc:myDriver:wombat", "myLogin","myPassword");
回答by Pascal Thivent
To create a connection, you need to load the driverfirst. For example, for MySQL:
要创建连接,您需要先加载驱动程序。例如,对于 MySQL:
Class.forName("com.mysql.jdbc.Driver");
The driver class to load obviously depends on the JDBC driver (and thus on the database) that needs to be on the classpath.
要加载的驱动程序类显然取决于需要在类路径上的 JDBC 驱动程序(因此也取决于数据库)。
Then, make the connection:
然后,建立连接:
String url = "jdbc:mysql://host_name:port/dbname";
String user = "scott";
String pwd = "tiger";
Connection con = DriverManager.getConnection(url, user, pwd);
The url
is the "connection url" and identifies the database to connect to. Again, the syntax will depend on the JDBC driver you're using. So, refer to the documentation of your JDBC driver.
该url
是“连接网址”和标识数据库连接。同样,语法将取决于您使用的 JDBC 驱动程序。因此,请参阅 JDBC 驱动程序的文档。
Establishing a Connectionin The Java Tutorials is indeed a good reference.
在 The Java Tutorials 中建立连接确实是一个很好的参考。
回答by Lalith
Have a look at ABCs of JDBC.
看看JDBC 的 ABCs。
回答by SSSM
To create a jdbc connection in java , you need to know about some classes and interfaces required to establish a connection which are provided in java.sql package.
要在 java 中创建 jdbc 连接,您需要了解 java.sql 包中提供的一些建立连接所需的类和接口。
Classes are Drivermanager and Types.
类是 Drivermanager 和 Types。
Interfaces are Connection, Statement, PreparedStatement, CallableStatement, ResultSet...etc
接口有 Connection、Statement、PreparedStatement、CallableStatement、ResultSet...等
This is the program to establish a jdbc connection
这是建立jdbc连接的程序
import java.sql.*;
class Sssm
{
public static void main(String aaa[])throws SqlException
{
Drivermanager d=new oracle.jdbc.driver.OracleDriver();
DriverManager.getconnection(d);
Connection c=Drivermanager.getconnection("jdbc:oracle:thin:@localhost:1521:xe","scott","tiger");
c.close();
}
}
Note: Instead of scott and tiger.. please write the login credentials that you have provided for oracle database.
注意:而不是scott 和tiger.. 请写下您为oracle 数据库提供的登录凭据。