将 jdbc 连接代码放在 servlet 类中是个好主意吗? [英] Is it a good idea to put jdbc connection code in servlet class?

查看:23
本文介绍了将 jdbc 连接代码放在 servlet 类中是个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 servlet 和数据库的新手.我有一个 core.java 文件,其中有一个 JDBC 连接代码.在同一个目录中我有一个 coreServlet.java 从 HTML 页面获取表单值的文件.我的问题是:

I am new in servlet and database.I have a core.java file in which I have a JDBC connection code.In the same directory I have a coreServlet.java file which take form value from HTML page.My questions are:

  1. 如何在两个 Java 类之间进行交互,例如如何使用从 servlet(coreServlet.java) 中的 HTML 表单获取的变量到 core.java 文件,以便将其存储在我的数据库中?
  2. 将我的 jdbc 连接代码放在 coreServlet.java 中是否更好?

我的项目结构是这样的:

My project structure is like:

  - aarya(project name)   
        |
         - WEB-INF
                 |
        |         -web.xml
        -src
           |
          -pkg
           |
            -CoreServlet.java(servlet to interact HTML form)
            -Main.java
            -Core.java(jdbc code is here to interact database)
        |
        -html
            |
            - core.html
        |
        - css
             |
             -core.css

        |
        -javascript
                  |
                   -core.js
        |
        - lib
        |
        -index.html

推荐答案

数据库连接数据应该在你的 JNDI 数据源中,如果你使用连接池会更好但永远不要班级.由于您的应用程序是 Web 应用程序,请注意连接池配置在很大程度上取决于 Web 应用程序服务器.

The database connection data should be in your JNDI Data Source and it would be better if you use a Connection Pool but never in a class. Since yours is a Web Application, note that the connection pool configuration depends heavily on the web application server.

作为例子,这在 Tomcat 7 数据库连接池配置JBoss 7 数据库连接池配置 (在 GlassFish 和其他 Web 应用服务器上配置数据库连接池还有其他步骤,注意这在每个服务器上是不同的).

As examples, this is very well explained in Tomcat 7 Database Connection Pool configuration and JBoss 7 Database Connection Pool configuration (there are other steps to configure the database connection pool on GlassFish and other Web application server, note that this is different on each server).

从这两个示例中,您可以看到将有一个 XML 文件,您将在其中放置连接属性:数据库 URL、用户、密码、最小和最大池连接大小(将打开多少连接到数据库)

From both examples, you can see that you will have a XML file where you put the connection attributes: database URL, user, password, min and max pool connection size (how many connections to database will be open)

  • Tomcat 方式:

  • Tomcat way:

<Resource name="jdbc/ProjectX" auth="Container"
    type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/projectx"
    username="user" password="password" maxActive="20" maxIdle="10" maxWait="-1"/>

  • JBoss 方式:

  • JBoss way:

    <datasource jndi-name="jdbc/ProjectX" pool-name="MySqlDS">
            <connection-url>jdbc:mysql://localhost:3306/projectx</connection-url>
            <driver>com.mysql</driver>
            <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
            <pool>
                <min-pool-size>10</min-pool-size>
                <max-pool-size>100</max-pool-size>
                <prefill>true</prefill>
            </pool>
            <security>
                <user-name>user</user-name>
                <password>password</password>
            </security>
            <statement>
                <prepared-statement-cache-size>32</prepared-statement-cache-size>
                <share-prepared-statements/>
            </statement>
    </datasource>
    <drivers>
        <driver name="com.mysql" module="com.mysql">
            <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
        </driver>
    </drivers>
    

  • 最后,如果您已经配置了数据库连接池并且它可以工作,那么您在代码中恢复连接所需要做的就是调用 InitialContext#lookup 使用其恢复资源JNDI 资源名称.

    In the end, if you have configured your database connection pool and it works, all you have to do in your code to recover the connection is call InitialContext#lookup to recover the resource using its JNDI resource name.

    知道了这一点,在配置了一个 JNDI 资源连接到名为jdbc/ProjectX"的 MySQL 数据库后,你可以有一个类来恢复 Connection 像这样:

    Knowing this, after configuring a JNDI resource to connect to MySQL database with name "jdbc/ProjectX", you can have a class which recovers the Connection like this:

    public class DatabaseConnectivity {
        public static Connection getConnection() throws NamingException, SQLException {
            InitialContext cxt = new InitialContext();
            DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/ProjectX" );
            return ds.getConnection();
        }
    }
    

    顺便说一下,我会为包使用不同的名称来了解类的功能组.例如:

    By the way, I would use different names for packages to know the group of functionality of the classes. For example:

    src
    - edu.home.controller.servlet
      + CoreServlet.java
    - edu.home.controller.filter
      + SessionFilter.java
    - edu.home.model.entity
      + AnEntity.java
      + AnotherEntity.java
    - edu.home.model.database
      + DatabaseConnectivity.java
    - edu.home.model.service
      + AnEntityService.java
      + AnotherEntityService.java
    (and on and on...)
    

    这篇关于将 jdbc 连接代码放在 servlet 类中是个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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