Tags in my blog

27 November 2009

Paging Calculation Note

Formula to calculate starting row from maxRows: maxRows*(page-1)
Formula to calculate numberOfPages (count + maxRows - 1) / maxRows

public List getCustomers() {
return customerManager.findAllCustomers(MAX_RESULTS * (page-1), MAX_RESULTS);
}

public long getNumOfPages() {
return (customerManager.findAllCustomersCount().longValue() + MAX_RESULTS - 1) / MAX_RESULTS;
}


import java.util.List;

import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.holibi.pharmabeans.db.manager.CustomerManager;
import com.holibi.pharmabeans.db.model.Customer;

@Controller("listCustomerBean")
@Scope("request")
public class ListCustomerBean {
private static final int MAX_RESULTS = 4;

@Autowired
private CustomerManager customerManager;

private int page;

public ListCustomerBean() {
FacesContext context = FacesContext.getCurrentInstance();
String strPage = context.getExternalContext().getRequestParameterMap().get("page");
if (strPage != null) {
page = Integer.parseInt(strPage);
}
if (page <= 1) {
page = 1;
}
}

public List getCustomers() {
return customerManager.findAllCustomers(MAX_RESULTS * (page-1), MAX_RESULTS);
}

public long getNumOfPages() {
return (customerManager.findAllCustomersCount().longValue() + MAX_RESULTS - 1) / MAX_RESULTS;
}

public void action() {
System.out.println("Page: " + page);
}
}

20 November 2009

Apache Tiles 2 insertDefinition

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="main.page" />

Restrict access to JSP

<security-constraint>
<web-resource-collection>
<web-resource-name>no_access</web-resource-name>
<url-pattern>*.jsp</url-pattern>
</web-resource-collection>
<auth-constraint/>
</security-constraint>

Apache Tiles 2 Config Servlet

<servlet>
<servlet-name>tiles</servlet-name>
<servlet-class>org.apache.tiles.web.startup.TilesServlet</servlet-class>
<init-param>
<param-name>
org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG</param-name>
<param-value>
/WEB-INF/tiles-defs.xml,/org/apache/tiles/classpath-defs.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Apache Tiles 2 Config Listener

<listener>
<listener-class>org.apache.tiles.web.startup.TilesListener</listener-class>
</listener>
<context-param>
<param-name>
org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>
/WEB-INF/tiles-defs.xml,/org/apache/tiles/classpath-defs.xml
</param-value>
</context-param>

Apache Tiles 2 Front Controller

<servlet>
<servlet-name>Tiles Dispatch Servlet</servlet-name>
<servlet-class>org.apache.tiles.web.util.TilesDispatchServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Tiles Dispatch Servlet</servlet-name>
<url-pattern>*.tiles</url-pattern>
</servlet-mapping>

19 November 2009

Example use JPA on spring applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<context:component-scan
base-package="database.db"/>

<!-- To inject @PersistenceContext -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/database"/>
<property name="properties">
<props>
<prop key="c3p0.acquire_increment">5</prop>
<prop key="c3p0.idle_test_period">100</prop>
<prop key="c3p0.max_size">100</prop>
<prop key="c3p0.max_statements">0</prop>
<prop key="c3p0.min_size">10</prop>
<prop key="user"></prop>
<prop key="password"></prop>
</props>
</property>
</bean>

<bean id="defaultDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/database"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>

<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/database" />
<property name="username" value="" />
<property name="password" value="" />
<!--
Added below properties to deal with connection timeout (to db) when
pollService hasnt been used for a long time
-->
<!--
Max number of connections that can remain idle in pool, without extra
conns being released
-->
<property name="maxIdle" value="2" />
<!-- Max number of active connections in the pool -->
<property name="maxActive" value="8" />
<!--
In milliseconds - wait no longer for a connection to be returned
(when no conns available in pool)
-->
<property name="maxWait" value="10000" />
<!-- Validation Query to be used to validate connections from pool -->
<property name="validationQuery" value="SELECT 1" />
<!-- Validate objects before being returned to pool (true/false) -->
<property name="testOnBorrow" value="true" />
<!--
Below methods are deprecated in Commons DBCP 1.3 (should be removed
from here?)
-->
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="30" />
<property name="logAbandoned" value="true" />
</bean>


<!-- To enable transaction using @Transactional -->
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="databasePU"/>
<property name="dataSource" ref="c3p0DataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
</bean>
</property>
</bean>

</beans>

Example persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="pu">
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/database" />
<property name="hibernate.connection.username" value="" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="10"/>
<property name="hibernate.c3p0.timeout" value="60"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="100"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="none"/>
</properties>
</persistence-unit>
</persistence>

23 May 2009

Complex type on XSD

<xs:element name="employee" type="fullpersoninfo"/>

<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

Study XSD

Restrictions on XSD simple element values

<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

How to Define an Attribute?

<xs:attribute name="xxx" type="yyy"/>

XML Schema has a lot of built-in data types. The most common types are:

* xs:string
* xs:decimal
* xs:integer
* xs:boolean
* xs:date
* xs:time

Default and Fixed Values for Attributes

<xs:attribute name="lang" type="xs:string" default="EN"/>
<xs:attribute name="lang" type="xs:string" fixed="EN"/>

Optional and Required Attributes

<xs:attribute name="lang" type="xs:string" use="required"/>

Restrictions on Values

<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Restrictions on a Set of Values

The example below defines an element called "car" with a restriction. The only acceptable values are: Audi, Golf, BMW:
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

The example above could also have been written like this:

<xs:element name="car" type="carType"/>

<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>


<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Restrictions on Whitespace Characters

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Restrictions on Length

<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

20 May 2009

Sample faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config 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-facesconfig_1_2.xsd"
version="1.2">

<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>

<managed-bean>
<managed-bean-name>backing</managed-bean-name>
<managed-bean-class>com.temp.Backing</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<navigation-rule>
<from-view-id>/backing.xhtml</from-view-id>
<navigation-case>
<from-outcome>greeting</from-outcome>
<to-view-id>/backing.xhtml</to-view-id>
</navigation-case>
</navigation-rule>

</faces-config>

09 May 2009

Disable button when clicked in RichFaces

<a4j:commandButton
id="cbutton"
value="Invoke"
actionListener="#{test.myActionListener}"
action="#{test.anAction}"
onclick="this.disabled=true"
oncomplete="this.disabled=false"
/>

Example web.xml Spring+JSF+RichFaces+Facelets


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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_2_5.xsd">

<display-name>Testing</display-name>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>

<!-- Spring application context -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-managers.xml
/WEB-INF/applicationContext-beans.xml
</param-value>
</context-param>

<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>

<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>laguna</param-value>
</context-param>

<context-param>
<param-name>org.richfaces.CONTROL_SKINNING</param-name>
<param-value>enable</param-value>
</context-param>

<!-- Enabling facelets: Use Documents Saved as *.xhtml -->
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>

<context-param>
<param-name>facelets.REFRESH_PERIOD</param-name>
<param-value>2</param-value>
</context-param>

<!-- Special Debug Output for Development -->
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>

<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

<!-- Defining and mapping the RichFaces filter -->
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>

<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- Don't show XHTML files unless user is in developer role -->
<security-constraint>
<display-name>Restrict XHTML Documents</display-name>
<web-resource-collection>
<web-resource-name>XHTML</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint>
<description>Only Let 'developer's access XHTML pages
</description>
<role-name>developer</role-name>
</auth-constraint>
</security-constraint>
</web-app>

Panel always refreshed except by limitToList

This panel will refreshed in every ajax request:

<a4j:outputPanel ajaxRendered="true">
<h:messages />
</a4j:outputPanel>

If you want to ignore that outputPanel just add limitToList:

<h:form>
<h:inputText value="#{person.name}">
<a4j:support event="onkeyup" reRender="test" limitToList="true"/>
</h:inputText>
<h:outputText value="#{person.name}" id="test"/>
</hform>

Example RichFaces <a4j:status>

This is example how to use <a4j:status>

<a4j:status startText="Started" stopText="stopped" />

or

<a4j:status for="stat2">
<f:facet name="start">
<h:graphicImage value="ajax_process.png" />
</f:facet>
<f:facet name="stop">
<h:graphicImage value="ajax_stoped.png" />
</f:facet>
</a4j:status>

Wrong web.xml namespace version

Last time I troubled again with wrong namespace version in eclipse. I got error like this on eclipse:

"CHKJ3020E: Invalid Security role-name: xxxx"

The solution is just replace your web.xml namespace to version 2.5:

<web-app version="2.5" 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_2_5.xsd">

Restrict files accessibility from web.xml

Example to restrict access to facelet .xhtml files from web.xml:

<!-- Don't show XHTML files unless user is in developer role -->
<security-constraint>
<display-name>Restrict XHTML Documents</display-name>
<web-resource-collection>
<web-resource-name>XHTML</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint>
<description>Only Let 'developer's access XHTML pages
</description>
<role-name>developer</role-name>
</auth-constraint>
</security-constraint>

RichFaces web.xml

I love this framework, this JSF component already using ajax technology. This is example web.xml to use RichFaces that also using laguna skin and facelet:

<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>


<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

<!-- Defining and mapping the RichFaces filter -->
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>

<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>

<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>laguna</param-value>
</context-param>

<context-param>
<param-name>org.richfaces.CONTROL_SKINNING</param-name>
<param-value>enable</param-value>
</context-param>

<!-- Use Documents Saved as *.xhtml -->
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.REFRESH_PERIOD</param-name>
<param-value>2</param-value>
</context-param>

<!-- Special Debug Output for Development -->
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>

JSF Implementation

There are 2 JSF implementations that I ever try to use. There are Mojarra (seperated project to use JSF implementation outside Glassfish Application Server) and Apache MyFaces from ASF.

These are minimum required files for Mojarra:
- jsf-api.jar
- jsf-impl.jar
that could be downloaded from here.

These are minimum required files for Apache MyFaces:
- myfaces-api-1.2.6.jar
- myfaces-impl-1.2.6.jar
could be downloaded from here.

When I started my web app with tomcat that could run already using Mojarra and change the implementation to MyFaces, the framework asked me to add 2 more files:
- commons-discovery-0.4.jar, and
- commons-codec-1.3.jar

In the web.xml i do not need to change anything. This is part of my web.xml:

<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>

<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

19 April 2009

Spring getting started

To start using spring just add this two jars:
- spring.jar
- commons-logging.jar

Put this spring xml bean configuration file with .xml extension. For example beans.xml.
<beans xmlns="http://www.springframework.org/schema/beans"
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="beanName" class="path.to.ClassName">
<property name="propName" value="1"/>
<property name="otherPropName" value="2"/>
</bean>
</beans>


Load the xml bean configuration by using this syntax in your java class:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

You can get your object using that context:
ClassName beanName = (ClassName) context.getBean("beanName");

16 April 2009

Send email with JavaMail

Require to download mail.jar and activation.jar from JavaMail library.

Example:

Main.java

package sendmail;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
*
* @author Stefan
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String host = "mail.hostname.com";
String from = "user1@hostname.com";
String to = "user2@hostname.com";
Properties props = new Properties();
props.put("mail.smtp.host", host);
Authenticator auth = new MyAuth();
Session session = Session.getDefaultInstance(props, auth);
MimeMessage message = new MimeMessage(session);
try {
Address addressFrom = new InternetAddress(from);
Address addressTo = new InternetAddress(to);
message.setText("Hello");
message.setSubject("First");
message.setFrom(addressFrom);
message.addRecipient(RecipientType.TO, addressTo);
Transport.send(message);
} catch (MessagingException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}


MyAuth.java

package sendmail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
*
* @author Stefan
*/
public class MyAuth extends Authenticator {

@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user1@hostname.com", "password");
}
}

13 April 2009

How to use JUnit

To perform unit testing using JUnit, simply just add @org.junit.Test annotation before the test method.

You can run your testing class by invoking main method from JUnitCore like this:

java org.junit.runner.JUnitCore TestClass1 TestClass2 ...

If you expect certain Exception have to thrown by a JUnit test method, you can annotate the method like this:

@Test(expected = IndexOutOfBoundsException.class) public void empty() {
new ArrayList<object>().get(0);
}

How to create JavaDB database embeded and client/server

Embeded Mode

Run locally:
java -jar derbyrun.jar ij;
ij> connect 'jdbc:derby:firstdb;create=true';


To connect from java:
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String dbName="jdbcDemoDB";
String connectionURL = "jdbc:derby:" + dbName + ";create=true";



Client/Server Mode

Start the server:
java -jar derbyrun.jar server start

Start the client:
java -jar derbyrun.jar ij
ij> connect 'jdbc:derby://localhost:1527/seconddb;create=true';


To connect from java:
String driver = "org.apache.derby.jdbc.ClientDriver";
...
String connectionURL = "jdbc:derby://localhost:1527/" + dbName + ";create=true";