December 30, 2017

JDBC Connections Cheat Sheet

Abstract

This is a quick reference for JDBC connections for common databases. I seem to have to lookup this information a lot, so I figured it be good to have a reference all in one place.

Derby

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbyclient</artifactId>
    <version>10.11.1.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>10.11.1.1</version>
    <scope>test</scope>
</dependency>

Embedded (in-memory)

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

String connectionUrl
    = "jdbc:derby:C:/My Databases/Derby/Test;user=;password=;create=true";

Connection conn
    = DriverManager.getConnection(connectionUrl);

Remote

Class.forName("org.apache.derby.jdbc.ClientDriver");

String connectionUrl
    = "jdbc:derby://localhost:1527/widget";

String user = "sa";
String pass = "sa";

Connection conn
    = DriverManager.getConnection(connectionUrl, user, pass);

PostgeSQL

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.1.4.jre7</version>
    <scope>test</scope>
</dependency>
Class.forName("org.postgresql.Driver");

String connectionUrl
    = "jdbc:postgresql://localhost:5432/widget";

String user = "widgetapp";
String pass = "widgetapp";

Connection conn
    = DriverManager.getConnection(connectionUrl, user, pass);

Oracle

Download JDBC drivers from http://www.oracle.com/technetwork/database/features/jdbc/index.html

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.4</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/ojdbc6.jar</systemPath>
</dependency>
Class.forName("oracle.jdbc.driver.OracleDriver");

String SID
    = "xe";

String connectionUrl
    = "jdbc:oracle:thin:@localhost:1521:" + SID;

String user = "hr";
String pass = "hr";

Connection conn
    = DriverManager.getConnection(connectionUrl, user, pass);

Summary

That’s it…enjoy!

December 22, 2017

Java EE Deployment Descriptor and XML Reference

Abstract

This is a quick reference for the most used Java EE deployment descriptors and XML documents. Most developers do not know the versions of these files change with EE versions and that EE servers use the versions of these files to determine what EE standard to apply to your application. So, as you migrate your application to newer application servers, make sure to update the versions of the Java EE deployment descriptors and XML documents to take advantage of the new EE features.

web.xml

<!-- EE 8 -->
<web-app version="4.0"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
<!-- EE 7 -->
<web-app version="3.1" 
         xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<!-- EE 6 -->
<web-app  version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 

beans.xml

<!-- EE 8 -->
<beans version="2.0" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd" bean-discovery-mode="all">
<!-- EE 7 -->             
<beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
<!-- EE 6 -->    
<beans 
xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd" -->

Summary

That’s it…enjoy!