Connect to derby database with tomcat as server

How to connect to derby database (which comes with netbeans)? I am using Tomcat as a server. I used to use the following instructions to connect to a derby database, but then I used glassfish as a 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 do not know how to do this.

Note: Tomcat and Derby are pre-installed with the NetBeans IDE that I am currently using.

+6
source share
3 answers

In Tomcat, find conf / context.xml, then edit and write something like this:

 <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: with the above url, the driver will be org.apache.derby.jdbc.ClientDriver

Note 2: You can also add the above information to the META-INF / context.xml of your project. This becomes application specific. If you add information to tomcat context.xml, which will become global.

Note 3: Download the jar from this site . Download db-derby-10.9.1.0-bin.zip.It contains many files, including derby.jar and derbyclient.jar (along with great documentation) .derbyclient.jar contains our friend org.apache.derby.jdbc.ClientDriver.class . derby.jar contains org.apache.derby.jdbc.EmbeddedDriver . Store the downloaded jar in the Tomcat lib folder.

and in your web.xml application "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 can look at these questions:

+10
source

You need:

1) Copy derbyclient-*.jar to ${TOMCAT_HOME}/lib .

2) Edit your server.xml and add the following lines to the GlobalNamingResources section:

  <Resource auth="Container" driverClassName="org.apache.derby.jdbc.EmbeddedDriver" maxActive="8" maxIdle="4" name="jdbc/my-ds" type="javax.sql.DataSource" url="jdbc:derby:mydb;create=true" username="myuser" password="mypassword" /> 

3) In the definition of your context, add:

  <Context docBase="myapp" path="/myapp" reloadable="true" ...> <ResourceLink name="jdbc/my-ds" global="jdbc/my-ds" type="javax.sql.DataSource" /> </Context> 

4) Restart Tomcat.

+4
source

The example you have requires JNDI. See the relevant tomcat documentation documents for setting this parameter.

Or use the connection string, here is the page from the docs derby http://db.apache.org/derby/integrate/plugin_help/derby_app.html

+1
source

Source: https://habr.com/ru/post/920197/


All Articles