0
点赞
收藏
分享

微信扫一扫

Java 中设置数据源

爪哇驿站 2022-05-06 阅读 94

在本教程中,我们将学习如何在 Java 中设置数据源。我们使用 MySQL 数据库系统。

我们使用MySQL Connector/J驱动程序。它是 MySQL 的官方 JDBC 驱动程序。

在 Java 中创建与数据库的连接有两种基本方法:a) 使用驱动程序管理器,b) 使用数据源。与驱动程序管理器相比,数据源有几个优点:

  • 它支持分布式事务
  • 它提供了一种连接池技术
  • 它可以由服务器管理,即在应用程序之外

由于在 Java 类中创建和关闭连接,驱动程序管理器会妨碍应用程序的性能。驱动管理器可用于简单的测试应用程序;对于复杂的应用程序,始终建议使用数据源。请参阅MySQL Java 教程以了解如何在 Java 应用程序中使用驱动程序管理器。

实现数据源接口的对象通常会向基于 Java 命名和目录接口 (JNDI) API 的命名服务注册。

JDBC

JDBC是一种用于 Java 编程语言的 API,它定义了客户端如何访问数据库。它提供了查询和更新数据库中数据的方法。JDBC面向关系数据库。从技术角度来看,API 是作为java.sql 包中的一组类。要将 JDBC 用于特定数据库,我们需要该数据库的 JDBC 驱动程序。

MySQL

MySQL是领先的开源数据库管理系统。它是一个多用户、多线程的数据库管理系统。MySQL 在网络上特别流行。MySQL有两个版本:MySQL服务器系统和MySQL嵌入式系统。

mysql> CREATE DATABASE testdb;
Query OK, 1 row affected (0.02 sec)

我们创建一个新的testdb数据库。在本教程中,我们只需要一个数据库对象;我们不会使用表格。我们将使用一条SELECT VERSION 语句来获取 MySQL 数据库的版本。

命令行应用程序

在此示例中,我们使用命令行 Java 应用程序连接到数据库。

图:项目结构

这就是项目结构在 NetBeans 中的样子。

MysqlDataSource是一个用于创建数据源的类。

db.properties

# mysql properties
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/testdb
mysql.username=testuser
mysql.password=test623

这些是 MySQL 数据库的属性。该db.properties文件位于src/resources该项目的子目录中。

ComLineDSEx.java

package com.zetcode;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.Properties;

public class ComLineDSEx {

    public static MysqlDataSource getMySQLDataSource() throws
            FileNotFoundException, IOException {

        Properties props = new Properties();
        FileInputStream fis = null;
        MysqlDataSource ds = null;

        fis = new FileInputStream("src/resources/db.properties");
        props.load(fis);

        ds = new MysqlConnectionPoolDataSource();
        ds.setURL(props.getProperty("mysql.url"));
        ds.setUser(props.getProperty("mysql.username"));
        ds.setPassword(props.getProperty("mysql.password"));

        return ds;
    }

    public static void main(String[] args) throws IOException, SQLException {

        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        MysqlDataSource ds = getMySQLDataSource();

        try {

            con = ds.getConnection();
            pst = con.prepareStatement("SELECT VERSION()");
            rs = pst.executeQuery();

            if (rs.next()) {

                String version = rs.getString(1);
                System.out.println(version);
            }

        } finally {

            if (rs != null) {
                rs.close();
            }

            if (pst != null) {
                pst.close();
            }

            if (con != null) {
                con.close();
            }
        }
    }
}

在此示例中,我们使用数据源连接到数据库并获取 MySQL 的版本。

fis = new FileInputStream("src/main/Resources/db.properties");
props.load(fis);

数据库属性是从带有类 的db.properties文件 中读取的。FileInputStream

ds = new MysqlConnectionPoolDataSource();
ds.setURL(props.getProperty("mysql.url"));
ds.setUser(props.getProperty("mysql.username"));
ds.setPassword(props.getProperty("mysql.password"));

创建了AMysqlConnectionPoolDataSource并设置了数据源属性。

con = ds.getConnection();

使用该getConnection 方法从数据源创建连接对象。

pst = con.prepareStatement("SELECT VERSION()");

一条 SQL 语句被创建。该SELECT VERSION 命令返回 MySQL 的版本。

rs = pst.executeQuery();

执行查询。它返回一个结果集。

if (rs.next()) {

    String version = rs.getString(1);
    System.out.println(version);
}

我们从结果集中获取第一个值并将其打印到控制台。

} finally {

    if (rs != null) {
        rs.close();
    }

    if (pst != null) {
        pst.close();
    }

    if (con != null) {
        con.close();
    }
}

最后,资源被释放。

Tomcat 中的 Web 应用程序

我们创建一个 Web 应用程序,它将检索 MySQL 的版本。该应用程序部署在 Tomcat 上。

 图:项目库

在我们的项目中,我们使用 JSTL 和 MySQL 驱动程序 JAR。JavaServer Pages 标准标记库(JSTL) 是一组有用的 JSP 标记,它们提供许多 JSP 文件共有的核心功能。

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/TomcatDSEx">

    <Resource name="jdbc/testdb" 
              auth="Container"
              type="javax.sql.DataSource" 
              username="testuser" 
              password="test623"              
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/testdb"
              maxActive="10" 
              maxIdle="4"/>
    
</Context>

对于 Tomcat Web 服务器,我们在context.xml 文件中创建一个新资源。该文件位于META-INF目录中。

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
 <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/testdb</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

然后,在web.xml文件中,我们创建对资源的引用。jdbc/testdb在我们的应用程序中,我们将使用逻辑名称 来引用数据源。

索引.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <c:redirect url="/Version"/>
    </body>
</html>

index.jsp文件重定向到Versionservlet。

显示版本.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>MySQL version</title>
    </head>
    <body>
        MySQL version: <c:out value="${version}"/>
        
    </body>
</html>

showVersion.jsp是一个 UI 元素,用于显示从数据库中检索到的数据。

MySQL version: <c:out value="${version}"/>

JSTL 的<c:out>标签用于输出响应的值。

Version.java

package com.zetcode.version;

import com.zetcode.version.service.DBVersionService;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "Version", urlPatterns = {"/Version"})
public class Version extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        String page = "/showVersion.jsp";
        
        String version = DBVersionService.getMySQLVersion();

        request.setAttribute("version", version);

        RequestDispatcher disp = getServletContext().getRequestDispatcher(page);
        disp.forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Returns version of MySQL";
    }
}

servlet 调用服务方法来获取 MySQL的Version版本。返回的值被设置为请求对象的属性。

String page = "/showVersion.jsp";

最后,servlet 指向该showVersion.jsp文件。

String version = DBVersionService.getMySQLVersion();

调用服务方法来获取 MySQL 的版本。

request.setAttribute("version", version);

版本值通过该setAttribute 方法设置为请求对象。

RequestDispatcher disp = getServletContext().getRequestDispatcher(page);
disp.forward(request, response);

我们发送到showVersion.jsp文件。

DBVersionService.java

package com.zetcode.version.service;

import com.zetcode.version.Version;
import com.zetcode.version.util.ServiceLocator;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;

public class DBVersionService {
    
    public static String getMySQLVersion() {

        String version = "no version";
        
        DataSource ds = ServiceLocator.getDataSource("java:comp/env/jdbc/testdb");
        Connection con = null;
        
        try {
            con = ds.getConnection();
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("SELECT VERSION()");

            if (rs.next()) {

                version = rs.getString(1);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Version.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(DBVersionService.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return version;
    }
}

DBVersionService是一个服务类,它包含一个获取 MySQL 版本的方法。

DataSource ds = ServiceLocator.getDataSource("java:comp/env/jdbc/testdb");

数据源是使用ServiceLocator类创建的。

con = ds.getConnection();
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("SELECT VERSION()");

if (rs.next()) {

    version = rs.getString(1);
}

这里我们有连接数据库并执行 SQL 语句的 JDBC 代码。

ServiceLocator.java

package com.zetcode.version.util;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ServiceLocator {

    public static DataSource getDataSource(String jndiName) {

        Context ctx = null;
        DataSource ds = null;
        
        try {
            ctx = new InitialContext();
            ds = (DataSource) ctx.lookup(jndiName);
        } catch (NamingException ex) {
            Logger.getLogger(ServiceLocator.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return ds;
    }
}

通过其ServiceLocator给定的 JNDI 名称查找数据源并将其返回给调用者。

$ curl localhost:8084/TomcatDSEx/Version

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>MySQL version</title>
    </head>
    <body>
        MySQL version: 5.5.49-0ubuntu0.14.04.1
        
    </body>
</html>

应用程序响应一个包含 MySQL 版本的 HTML 页面。

这是 Java 教程中的数据源。

 

举报

相关推荐

0 条评论