Category Archives: Glassfish

Setting up a JDBC connection in Glassfish.

Setting up a JDBC connection in Glassfish.

How to setup a JDBC connection in Glassfish

This is one of that kind of things that can be a challenge for beginners but, at last, is really simple, easy and fast to do. Follow these steps.

  1. Be sure that you Glassfish server is stopped before going on.
  2. Download MySQL JDBC driver from Mysql.com. You can find it here. JDBC driver is called MySQL Connector/J. At the moment I write this article current version is 5.1.25
  3. Unzip and extract all files from the file. Locate the jar containing the JDBC driver. Currently this file is called mysql-connector-java-5.1.25-bin.jar and is located at the root of the folder you have just unzipped.
  4. Copy this jar file to $glassfish_install_folder\glassfish\lib
  5. Start Glassfish and go to the admin console, usually located at http://localhost:4848
  6. At left side on your console you will see a tree, and one node called Resources. Open Resources\JDBC\JDBC Connection Pools. Create a connection pool with the following properties:Pool name: MyDatabae
    Resource type: java.sql.Driver (you can choose any other but by now is the simplest option).
    Database Driver Vendor: MySQL. Click on next. Because you choose database driver vendor MySQL you will have already specified the driver classname (com.mysql.jdbc.Driver).
    Initial and Minimum Pool Size Set a zero value on this parameter. You don’t need initially 8 connections to the database in your development machine.Set the next additional properties:

    URL: jdbc:mysql://localhost:3306/booreg
    user: set the user you want to access this database. Notice that all connection will use the same user.
    password: write the password of your user. Notice that password is stored unencrypted.You should see your screen like similar to this two images:

Maven

Web Application/RESTful service

https://jersey.java.net/documentation/latest/index.html

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp \
                -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
                -DgroupId=com.welgg -DartifactId=jointforces -Dpackage=com.welgg.jointforces \
                -DarchetypeVersion=2.19

Upon deployment attempt in Glasfish you may get: “An error has occurred Error occurred during deployment: Exception while loading the app : CDI deployment failure:WELD-001408: Unsatisfied dependencies for type Set<Service> with qualifiers @Default at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject com.google.common.util.concurrent.ServiceManager(Set<Service>) at com.google.common.util.concurrent.ServiceManager.<init>(ServiceManager.java:0) . Please see server.log for more details.

Modify important version numbers in pom.xml

  • maven-compiler-plugin: 3.1
  • jersey.version: 2.19

 
Sample App

mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes  -DgroupId=nl.welgg.sample   -DartifactId=sample

Replace generated pom.xml
Replace generated App.java

mvn install
java -cp .\target\sample-1.0-SNAPSHOT.jar nl.welgg.sample.App

Make Eclipse ready

mvn eclicpse:eclipse -DdownloadSources
del .project
del .classpath
del .settings
mvn install

Import in Eclipse

Screen Shot 03-21-16 at 02.09 PM