Java – connect to Derby database with Tomcat as server

How do I connect to the Derby database (included with NetBeans)? I use Tomcat as the server Before, I used the following statement to connect to the Derby database, but then I used GlassFish as the server

Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/PollDatasource");
Connection connection = ds.getConnection();

But now using Tomcat as a server, I don't know how to do this

Note: Tomcat and Derby pre installed the NetBeans ide I currently use

Solution

Find conf / context in Tomcat XML, then edit and write the following:

<Resource name="jdbc/PollDatasource" auth="Container" type="javax.sql.DataSource"
    driverClassName="com.YourDriver" 
    url="jdbc:derby://localhost:1527/nameOfTheDatabase;create=true"
    username="username" password="password" maxActive="20" 
    maxIdle="10" maxWait="-1" />

Note 1: using the URL above, the driver will be org apache. derby. jdbc. ClientDriver

Note 2: you can also use meta inf / context Add the above information to XML This will become application specific If you are in the context of Tomcat Add information to XML and the information will become global

Note 3: Download jar from this website Download db-derby-10.9. 1.0-bin. zip. It contains many files, including Derby Jar and derbyclient Jar (and lots of documentation) derbyclient. Jar contains our friend organization apache. derby. jdbc. ClientDriver. class. derby. Jar contains org apache. derby. jdbc. EmbeddedDriver. Save the downloaded jar in Tomcat's lib folder

And in your application web xml“resource-ref”:

<resource-ref>
    <description>my connection</description>
    <res-ref-name>jdbc/PollDatasource</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

You may want to look at these questions:

> Isn’t it necessary to mention the name of archive in the Resource tag? > When is the tag I added in context. xml gets read? > What are steps followed in the look-up? what is looked first web. xml or context. xml?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>