如何使用JDBC API在Java中建立数据库连接? [英] How to make database connectivity in Java using JDBC API?

查看:123
本文介绍了如何使用JDBC API在Java中建立数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中创建jdbc连接?

How to create a jdbc connection in Java?

推荐答案

要创建连接,您需要加载驱动程序首先。例如,对于MySQL:

To create a connection, you need to load the driver first. For example, for MySQL:

Class.forName("com.mysql.jdbc.Driver");

加载的驱动程序类显然取决于需要的JDBC驱动程序(以及数据库)在类路径上。

The driver class to load obviously depends on the JDBC driver (and thus on the database) that needs to be on the classpath.

然后,建立连接

String url = "jdbc:mysql://host_name:port/dbname";
String user = "scott";
String pwd = "tiger";
Connection con = DriverManager.getConnection(url, user, pwd);

url 是连接网址并标识要连接的数据库。同样,语法将取决于您正在使用的JDBC驱动程序。因此,请参阅JDBC驱动程序的文档。

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.

建立连接确实是一个很好的参考。

Establishing a Connection in The Java Tutorials is indeed a good reference.

这篇关于如何使用JDBC API在Java中建立数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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