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

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

问题描述

我是servlet和database的新手。我有一个 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中更好吗?

我的项目结构就像:

  - 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应用程序,请注意连接池配置在很大程度上取决于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数据库连接池配置和< a href =https://community.jboss.org/wiki/DataSourceConfigurationInAS7 =nofollow> 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文件,您可以在其中放置连接属性:database 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 <的类/ code>像这样:

    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天全站免登陆