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.
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>
Reuse testcase in other Modules.
In ‘Module A’, I want to reuse the testcases in ‘Module-Core’ (e.g. extend Module-Core…AbstractUserManagerTest)
http://maven.apache.org/guides/mini/guide-attached-tests.html
Steps:
mvn archetype:generate -DgroupId=com.liguoliang.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
mvn eclipse:eclipse
mvn package
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.
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>
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.