<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    honzeland

    記錄點滴。。。

    常用鏈接

    統(tǒng)計

    Famous Websites

    Java

    Linux

    P2P

    最新評論

    How do I use DataSources with Tomcat?

    ZZ from: http://wiki.apache.org/tomcat/UsingDataSources

    When developing J2EE web applications, the task of database connection management can be daunting. Best practice involves using a J2EE DataSource to provide connection pooling, but configuring DataSources in web application servers and connecting your application to them is often a cumbersome process and poorly documented.

    The usual procedure requires the application developer to set up a DataSource in the web application server, specifying the driver class, JDBC URL (connect string), username, password, and various pooling options. Then, the developer must reference the DataSource in his application's web.xml configuration file, and then access it properly in his servlet or JSP. Particularly during development, setting all of this up is tedious and error-prone.

    With Tomcat 5.5, the process is vastly simplified. Tomcat allows you to configure DataSources for your J2EE web application in a context.xml file that is stored in your web application project. You don't have to mess with configuring the DataSource separately in the Tomcat server.xml, or referencing it in your application's web.xml file. Here's how:

    Install the JDBC Driver

    Install the .jar file(s) containing the JDBC driver in Tomcat's common/lib folder. You do not need to put them in your application's WEB-INF/lib folder. When working with J2EE DataSources, the web application server manages connections for your application.

    Create META-INF/context.xml

    In the root of your web app directory structure, create a folder named META-INF (all caps). Inside that folder, create a file named context.xml that contains a Resource like this:

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

    <Context>

    <Resource name="jdbc/WallyDB" auth="Container"
    type="javax.sql.DataSource" username="wally" password="wally"
    driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
    url="jdbc:sqlserver://localhost;DatabaseName=mytest;SelectMethod=cursor;"
    maxActive="8"
    />

    </Context>

    This example shows how to configure a DataSource for a SQL Server database named mytest located on the development machine. Simply edit the Resource name, driverClassName, username, password, and url to provide values appropriate for your JDBC driver.

    Access the DataSource in Your Application

    From a Servlet

    Here's how you might access the data in a servlet:

      InitialContext ic = new InitialContext();
    DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/WallyDB");
    Connection c = ds.getConnection();
    ...
    c.close();

    Notice that, when doing the DataSource lookup, you must prefix the JNDI name of the resource with java:comp/env/

    Sample Project

    Here's a sample web application project that shows where all the files go. This one shows how to access data from from a JSP page: datasourcedemo.war

    Known-Working examples for other Databases

        <Resource name="jdbc/denali" auth="Container" type="javax.sql.DataSource"
    username="denali" url="jdbc:postgresql://localhost:5432/demo"
    factory="org.apache.commons.dbcp.BasicDataSourceFactory"
    driverClassName="org.postgresql.Driver"
    maxActive="20" maxIdle="10"/>
        <Resource name="jdbc/ccsdatasource" auth="Container" type="javax.sql.DataSource"
    username="ccs" password="secret" url="jdbc:mysql://localhost:3306/ccs"
    driverClassName="com.mysql.jdbc.Driver"
    maxActive="20" maxIdle="10"/>

    Please Note

    This technique is Tomcat-specific. If you deploy your web application to another application server (or even an older version of Tomcat), you will need to configure the database according to your application server's documentation, and reference it in your application's web.xml.



    posted on 2007-07-26 16:22 honzeland 閱讀(627) 評論(1)  編輯  收藏 所屬分類: Tomcat

    評論

    # re: How do I use DataSources with Tomcat? 2007-12-18 14:59 Andreas

    InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/WallyDB"); Connection c = ds.getConnection();
    ---------------------------------------
    建立了連接以后我想定義一個Struct類型的變量,來與Oracle中的Object來進(jìn)行映射。但是會出現(xiàn)錯誤。
    StructDescriptor st = new StructDescriptor("ObjectNameInOracle",c);
    error marshalling return; nested exception is:
    java.io.NotSerializableException: oracle.jdbc.driver.T4CConnection

    謝謝,Email:dutguoyi@hotmail.com

      回復(fù)  更多評論   


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 日本激情猛烈在线看免费观看| 亚洲另类春色国产精品| 久久亚洲AV成人无码国产最大| 国产曰批免费视频播放免费s| 内射少妇36P亚洲区| 免费人成网站在线观看不卡| 亚洲日韩精品一区二区三区无码 | 鲁大师在线影院免费观看| 久久夜色精品国产亚洲| 久久午夜无码免费| 亚洲精品中文字幕乱码影院| 最近免费中文在线视频| 亚洲av无码一区二区三区天堂古代 | 一级特黄色毛片免费看| 中文字幕精品亚洲无线码一区应用| 深夜久久AAAAA级毛片免费看| 亚洲人成无码www久久久| 国产精品免费久久久久电影网| 亚洲精品无码av人在线观看| 国产精品免费高清在线观看| 亚洲人成在线精品| 国产在线观看免费不卡| 久久精品无码免费不卡| 老汉色老汉首页a亚洲| 四虎免费大片aⅴ入口| 一本久久A久久免费精品不卡| 亚洲人精品午夜射精日韩| 最近2022中文字幕免费视频 | 国产a v无码专区亚洲av| 大地资源在线资源免费观看 | 污视频网站免费观看| 亚洲精品tv久久久久久久久| 亚洲毛片免费观看| 亚洲欧美在线x视频| 亚洲免费在线观看视频| 日韩一区二区a片免费观看 | 亚洲乱码av中文一区二区| 亚洲乱码国产一区网址| 777爽死你无码免费看一二区| 国产亚洲一卡2卡3卡4卡新区| 国产亚洲人成无码网在线观看|