6

How do i connect to derby database (that comes with the netbeans) ? I am using Tomcat as the server. Earlier i used the following statements 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 the server i am unaware how to do this.

Note : Tomcat and Derby are pre installed with netbeans IDE that i am using currently

Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328

3 Answers3

10

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 in META-INF/context.xml of your project. This becomes application specific.If you add the information in tomcat's context.xml that becomes global.

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

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 :

Community
  • 1
  • 1
Raknel
  • 529
  • 3
  • 12
  • 1
    This the name of the JDBC driver, specific for database. For your application that will be org.apache.derby.jdbc.EmbeddedDriver. You can download this in jar from: http://mvnrepository.com/artifact/org.apache.derby/derby – Raknel Jul 11 '12 at 09:40
  • I added these tags but get an [exception](http://stackoverflow.com/questions/11516747/org-apache-tomcat-dbcp-dbcp-sqlnestedexception-what-is-this-and-why-do-i-get-t) – Suhail Gupta Jul 17 '12 at 11:19
  • can you please explain how are these tags looked up when attempting a connection. I have problem applying this and a problem understanding this – Suhail Gupta Jul 18 '12 at 11:02
4

You need to:

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

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

 <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 your context definition, 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.

carlspring
  • 31,231
  • 29
  • 115
  • 197
  • [I added these tags but get an exception.](http://stackoverflow.com/questions/11516747/org-apache-tomcat-dbcp-dbcp-sqlnestedexception-what-is-this-and-why-do-i-get-t)Though i have edited the `context.xml` of tomcat and `web.xml` in my project. – Suhail Gupta Jul 18 '12 at 09:26
  • Upvoting this because I found it by accident and it fits really nicely in my current application. – thonnor Dec 19 '14 at 14:40
1

The example you have requires JNDI. See the relevant tomcat versions docs on setting that up.

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

TedTrippin
  • 3,525
  • 5
  • 28
  • 46