Quick Setup a Project Using JDBC & Derby DB/Java DB

Categories: DatabaseJava; Tagged with: ; @ May 8th, 2014 0:03

Set up Maven dependences

<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.10.2.0</version>
</dependency>

<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.10.2.0</version>
</dependency>

Connect to the DB using JDBC

This method will print out all Tables;  // before you do anything with the Database, there should be some System tables already.

package com.liguoliang;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class JDBCTest {

	private static final String DB_URL = "jdbc:derby:tempDBForTest;create=true";	
	public static void main(String[] args) {
	    Connection conn = null ;
	    try {
	        Class.forName("org.apache.derby.jdbc.EmbeddedDriver") ;
	        conn = DriverManager.getConnection(DB_URL);
	        
	        String sql = "SELECT TABLENAME FROM SYS.SYSTABLES";
			PreparedStatement ps = conn.prepareStatement(sql);
	        
	        ResultSet rs = ps.executeQuery();
	        while (rs.next()) {
				System.out.println("Rs: " + rs.getString(1));
			}
	    } catch (SQLException se) {
	         se.printStackTrace();
	    } catch(ClassNotFoundException e){
	        System.out.println("JDBC Driver not found in CLASSPATH") ;
	    }finally {
	        if(conn != null){
	            try{
	                conn.close() ;
	            } catch(SQLException se){
	                se.printStackTrace();
	            }
	        }
	    }
	}

}

Use Eclipse to manage your Derby

Switch to ‘Database Development’ perspective, you may create/manage  Derby DB/table.

Maven Assembly Plugin Example

Categories: Development Notes; Tagged with: ; @ April 27th, 2014 0:22

Requirement:

Generate a distributable archive.

Example:

/src/assemble/distribution.xml:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
  <id>distribution</id>
  <formats>
    <format>zip</format>
  </formats>
  <files>
    <file>
      <source>/src/main/doc/install.txt</source>
      <outputDirectory>/docs</outputDirectory>
      <filtered>true</filtered>
    </file>
    <file>
        <source>/target/j2ee.war</source>
        <outputDirectory>/</outputDirectory>
    </file>
  </files>
  
   <fileSets>
    <fileSet>
      <directory>${basedir}/src/main/scripts</directory>
      <includes>
        <include>*.sh</include>
      </includes>
      <excludes>
        <exclude>README.txt</exclude>
        <exclude>NOTICE.txt</exclude>
      </excludes>
      <outputDirectory>/fileset</outputDirectory>
    </fileSet>
  </fileSets>
</assembly>

pom.xml:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
      <!-- <filters>
        <filter>src/assemble/filter.properties</filter>
      </filters> -->
      <descriptors>
        <descriptor>src/assemble/distribution.xml</descriptor>
      </descriptors>
    </configuration>
  </plugin>

image

Maven: Reuse tests in other Modules

Categories: Development Notes; Tagged with: ; @ September 6th, 2013 0:13

Requirement:

Reuse testcase in other Modules.

In ‘Module A’, I want to reuse the testcases in ‘Module-Core’ (e.g. extend Module-Core…AbstractUserManagerTest)

Solution: using attached test

http://maven.apache.org/guides/mini/guide-attached-tests.html

Maven hello world

Categories: Java; Tagged with: ; @ July 21st, 2013 11:45

Use maven to create, test, build project.

Steps:

  1. Create new project:
    mvn archetype:generate -DgroupId=com.liguoliang.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
  2. init Eclipse project:
    mvn eclipse:eclipse
  3. import the project into eclipse. (or you can directly create maven project in Eclipse using the Eclipse maven plugin)
  4. build the package:
    mvn package

 

life cycle:

  • validate: validate the project is correct and all necessary information is available
  • compile: compile the source code of the project
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package: take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run
  • verify: run any checks to verify the package is valid and meets quality criteria
  • install: install the package into the local repository, for use as a dependency in other projects locally
  • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Trouble shooting

Error: Caused by: java.lang.UnsupportedClassVersionError:Unsupported major.minor version 51.0

Check the JVM which is using by Maven:

mvn –version can get a summary:

C:\Users\Guoliang>mvn -version
Apache Maven 3.0.4 (r1232337; 2012-01-17 16:44:56+0800)
Maven home: C:\Tools\sts\apache-maven-3.0.4\bin\..
Java version: 1.7.0_17, vendor: Oracle Corporation
Java home: C:\Tools\jdk7\jre
Default locale: en_SG, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

You can edit the Java Home path or specify the JVM for maven.

Maven: modify the default JDK 修改Maven默认JDK

Categories: Development Notes; Tagged with: ; @ May 1st, 2013 11:18

I created a maven java project, and I want to use some new feature in Java7, but every time, after update the project, the JRE will be changed to 1.5 automatically.

After googling, got the solution: configure maven-compiler-plugin:

  <build>
    <finalName>J2EE</finalName>
    <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.1</version>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
          </configuration>
        </plugin>
    </plugins>
  </build>

 

http://stackoverflow.com/questions/3539139/what-causes-a-new-maven-project-in-eclipse-to-use-java-1-5-instead-of-java-1-6-b



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.