How can I export a java project with derby database in Netbeans? I would like to after import project on another computer it works without any configuration.
The most basic installation of derby into your application is pretty effortless. It's really just a matter of put the library/jar on the class path. Netbeans already comes with this library, so you don't have to download it. If for any reason, it doesn't, you can download it from here. After you unzip it, just add the derby.jar
(and others if necessary) to the classpath.
Basically from Netbeans, from your project, right click on the [Library] and select [Add Library].
Then just select the [Java DB] library
If you downloaded the library, then instead of [Add Library], select [Add Jar] and search for the jar where you downloaded it to.
These are the jars that come with the Netbeans library
Then you can use the database in your application. The embedded version runs on the same JVM as your application, so you may want to take care of starting and shutting down the database yourself. Here's a sample application that starts-createsdb-inserts-selects-shutsdown.
import java.sql.*;
public class DerbyProject {
public static void main(String[] args) throws Exception {
/* ------- Start DB ----------- */
final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
Class.forName(driver).newInstance();
final String protocol = "jdbc:derby:";
final String dbName = "derbyDB";
Connection connection = DriverManager.getConnection(
protocol + dbName + ";create=true");
System.out.println("===== Started/Connected DB =====");
/*
* Drop table for testing. If we don't drop, running the
* same program will fail, if we start our application over
* as the new table has been persisted
*/
final String dropSql = "drop table users";
Statement statement = connection.createStatement();
try {
statement.execute(dropSql);
System.out.println("===== Dropped Table 'users' =====");
} catch (SQLException e) {
if (!e.getSQLState().equals("42Y55")) {
throw e;
}
}
/* ----- Creeate 'users' table ----- */
final String createSql = "create table users ( id int, name varchar(32) )";
statement.execute(createSql);
System.out.println("===== Created Table 'users' =====");
/* ----- Insert 'peeskillet' into 'users' ----*/
final String insertSql = "insert into users values ( 1 , 'peeskillet' )";
statement.execute(insertSql);
System.out.println("===== inserted 'peeskillet into 'users' =====");
/* ----- Select from 'users' table ----- */
final String selectSql = "select name from users where id = 1";
ResultSet rs = statement.executeQuery(selectSql);
if (rs.next()) {
System.out.println("===== Selected from 'users' with id 1 \n"
+ "\t\t\t result: " + rs.getString("name") + " =====");
}
/* ------ Shut Down DB ------- */
try {
DriverManager.getConnection("jdbc:derby:;shutdown=true");
} catch (SQLException se) {
if (((se.getErrorCode() == 50000)
&& ("XJ015".equals(se.getSQLState())))) {
System.out.println("Derby shut down normally");
} else {
System.err.println("Derby did not shut down normally");
throw se;
}
}
statement.close();
rs.close();
connection.close();
}
}
When we build it, the Netbeans default build should put the jars into the dist\lib
and put those jars on the classpath in the MANIFEST.MF
. You can run the jar from the command line to test it
If you open up the files view in Netbeans, you can see where the data is actually being stored.
For more information on Derby and Derby tutorials: