如何从Java应用程序访问MySQL数据库?

Java is popular programming language generally used commercial and enterprise applications. MySql is popular database too which is used big service providers like Google, Facebook etc. Most of the applications generally uses data and needs to store this data in a relational database. MySQL is generally the first choice if we do not want to give money for license. In this tutorial we will look how to connect a Java applications into MySQL database in detail.

Java是普遍用于商业和企业应用程序的流行编程语言。 MySql也是流行的数据库,使用了Google,Facebook等大型服务提供商。大多数应用程序通常使用数据,并且需要将这些数据存储在关系数据库中。 如果我们不想为许可证花钱,那么MySQL通常是首选。 在本教程中,我们将详细研究如何将Java应用程序连接到MySQL数据库。

创建上下文和数据源 (Create Context and DataSource)

JDBC MySQL driver can be used to connect MySQL database like below. JDBC driver is by default provided by Java Development Kit or simply JDK.

JDBC MySQL驱动程序可用于连接MySQL数据库,如下所示。 默认情况下,JDBC驱动程序由Java Development Kit或简单地由JDK提供。

Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myDB");
  • new InitialContext() creates a context for us.

    新的InitialContext()为我们创建了一个上下文。

  • How To Access MySQL Database From Java Applications?

    如何从Java应用程序访问MySQL数据库?

配置MySql数据库连接 (Configure MySql Database Connection)

We will create a new data source named dataSource . This data source will be used to specify the database server , user name, password and other related database and connection settings.

我们将创建一个名为dataSource的新数据源。 该数据源将用于指定数据库服务器,用户名,密码以及其他相关的数据库和连接设置。

MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("ahmet");
dataSource.setPassword("ali");
dataSource.setServerName("mysql.poftut.com");
  • We create MySql datasource

    我们创建MySql数据源

  • Then set MySql Server username and password to connect with setUser and setPassword .

    然后设置MySql Server用户名和密码以与setUsersetPassword连接。

  • We set MySql Server address or dns name with mysql.poftut.com

    我们使用mysql.poftut.com设置MySql Server地址或dns名称

连接和查询MySql数据库(Connect and Query MySql Database)

We will use created and established connection names conn . We will create a statement which will run over connection conn and return result set named rs . But keep in mind that after using these objects for security, performance and resource usage we need to close them properly which is generally implemented in close() function like below.

我们将使用创建和建立的连接名称conn 。 我们将创建一条语句,该语句将在连接conn运行并返回名为rs结果集。 但是请记住,在将这些对象用于安全性,性能和资源使用之后,我们需要正确关闭它们,这通常在如下的close()函数中实现。

Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM USERS");
...
rs.close();
stmt.close();
conn.close();
  • We create real connection object and assign to conn

    我们创建真实的连接对象并分配给conn

  • We create query statement into stmt

    我们将查询语句创建到stmt中

  • rs result set is populated with query executed by stmt

    rs结果集由stmt执行的查询填充

  • rs, stmt, conn are closed with close function to release resources

    使用关闭功能关闭rs,stmt,conn ,以释放资源

LEARN MORE  How To Install and Configure MongoDB NoSQL Database To Linux, Ubuntu, Debian, CentOS, Fedora
了解更多如何在Linux,Ubuntu,Debian,CentOS,Fedora上安装和配置MongoDB NoSQL数据库

如何从Java应用程序访问MySQL数据库? 信息移植 (How To Access MySQL Database From Java Applications? Infografic)

How To Access MySQL Database From Java Applications? Infografic
How To Access MySQL Database From Java Applications? Infografic
如何从Java应用程序访问MySQL数据库? 信息移植

翻译自: https://www.poftut.com/how-to-access-mysql-database-from-java-applications/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值