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用户名和密码以与setUser和setPassword连接。
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 ,以释放资源
如何从Java应用程序访问MySQL数据库? 信息移植 (How To Access MySQL Database From Java Applications? Infografic)
翻译自: https://www.poftut.com/how-to-access-mysql-database-from-java-applications/