Professional Documents
Culture Documents
DerbyDB and Eclipse
DerbyDB and Eclipse
Apache Derby, an Apache DB subproject, is an open source relational database implemented entirely in Java and available under the Apache License, Version 2.0. Some key advantages include:
Derby has a small footprint -- about 2 megabytes for the base engine and embedded JDBC driver. Derby is based on the Java, JDBC, and SQL standards. Derby provides an embedded JDBC driver that lets you embed Derby in any Java-based solution. Derby also supports the more familiar client/server mode with the Derby Network Client JDBC driver and Derby Network Server. Derby is easy to install, deploy, and use.
Eclipse + Derby
http://www.eclipse.org/downloads/
derby_core_plugin - provides the Derby jar files to other plugins in Eclipse. derby_ui_plugin - provides an Apache Derby Nature in Eclipse for easy database application development.
http://db.apache.org/derby/releases/release-10.5.3.0.cgi plugin
eclipse2 C:\eclipse2\plugins
10
11
.jar
derbyDB
ij
http://db.apache.org/derby/integrate/plugin_help/ij.html
12 derby database and eclipse |
DB
13
import java.util.*; import java.sql.*; public class derbyDB { public static void insertIntoDB(String term,String defintion) { try { String driver = "org.apache.derby.jdbc.EmbeddedDriver"; Class.forName(driver); String dbName = "DB"; String connectionURL = "jdbc:derby:" + dbName; Connection conn = null; conn = DriverManager.getConnection(connectionURL); Statement statement = conn.createStatement(); ArrayList statements = new ArrayList(); PreparedStatement psInsert = null; psInsert = conn.prepareStatement("insert into db values (?,?)"); statements.add(psInsert); psInsert.setString(1, term); psInsert.setString(2, defintion); psInsert.executeUpdate(); statement.close(); conn.close(); } catch ( Exception e ) { System.out.println(e); e.printStackTrace(); } } public static void PrintDB () { try { String driver = "org.apache.derby.jdbc.EmbeddedDriver"; Class.forName(driver); String dbName = "DB"; String connectionURL = "jdbc:derby:" + dbName; Connection conn = null; conn = DriverManager.getConnection(connectionURL); Statement statement = conn.createStatement(); ArrayList statements = new ArrayList(); PreparedStatement psSelect = null;
14
psSelect = conn.prepareStatement("SELECT * FROM db"); statements.add(psSelect); ResultSet rs = psSelect.executeQuery(); while ( rs.next() ) { System.out.print("Term: "+rs.getString(1) + " " ); System.out.print("Defintion: "+rs.getString(2) + " " ); } rs.close(); statement.close(); conn.close(); } catch ( Exception e ) { System.out.println(e); e.printStackTrace(); } } public static void main(String[] args) { insertIntoDB("try1","try2"); PrintDB(); } }
15