- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Introduction to Derby
In this chapter, we will cover the basic concepts and definitions of Derby. It is necessary to know these basics because Derby is quite involved. It is not as simple as SQLite.
Derby is a relational database management system written in Java. It implements an SQL-92 core subset, as well as some SQL-99 features. It uses IBM DB2 SQL syntax. Derby has a small footprint around 2MB. It has transaction support. The database format used by Derby is portable and platform independent.
Deployment options
Derby can operate in two modes. Embedded or client-server. In the embedded mode the Derby engine runs within the Java Virtual Machine of the application. The application is accessing the database directly and exclusively. No other application can access the database simultaneously. There is no need to set up a Derby server before connecting to the database. The Derby engine is started when we load the embedded driver. In the client-server mode Derby provides multi-user connectivity across a network. The Derby engine runs in the JVM of the server. Other applications can connect to the Database from different JVMs. We must start a database server before connecting to the database. This is similar to other RDBMS like Oracle or MySQL.
The Derby system
The Derby system consists of an instance of the Derby engine and its environment. It includes the system directory, the databases and the system-wide configuration. Each database is located in a subdirectory of the system directory having the same name as the database. In addition it has an error log file and an optional derby.properties
file. Derby writes information and error messages to a log file with a default name derby.log
. The properties file contains configuration data, parameters specific to the Derby system. The derby system is not persistent. We must provide the location of the system at every startup.
The Derby system is specified with the derby.system.home
property. If the directory specified in the property does not exist, Derby creates it automatically. If we do not specify the property explicitly, the current directory is used. The current directory is the value of the JVM user.dir
property. It is recommended to always specify the derby.system.home
explicitly.
-Dderby.system.home=/home/janbodnar/programming/derby/dbs
An example of how we set the property. It can be set on the command line, in a dialog box in case we use an IDE or in the derby.properties
file.
The Derby properties
The derby.properties
is a configuration file for the Derby system. It is a simple text file which enables us to configure the entire Derby system, a specific database or a conglomerate. A conglomerate is a table or index in Derby. We can configure several options. Whether to authorise users or not. Which databases to boot. How to name the error log file. The location of the system directory and many more. The derby.properties is not created automatically. We must create the file ourselves if we want to use it.
Starting & ending Derby database
In the embedded mode, the database is started when an application first makes a connection to it. It is possible to configure the Derby system to boot all available databases when it starts with the derby.system.bootAll
property. When the database has started, a message is written to the derby.log file. A database is shut down by specifying the shutdown=true
attribute in the connection URL. We can shutdown a database or an entire Derby system.
DriverManager.getConnection("jdbc:derby:testdb;shutdown=true"); DriverManager.getConnection("jdbc:derby:;shutdown=true");
The first JDBC call shuts down the testdb
database. The second call ends the whole Derby system. Note that an SQLExpection
is raised when a system shuts down. This exception should be programmatically handled. We should also close all existing resources before shutting down the database or the system.
In the client-server mode, the Derby server is started with the startNetworkServer script and ended with the stopNetworkServer script. They are located in the bin subdirectory of the installation directory.
The JAR files
Inside the lib
subdirectory of the installation directory, we can find several libraries. Each of the JAR files serves a specific function. The derby.jar
is a library used for embedded databases. We need to have this JAR file in our classpath in embedded applications. In client-server mode, this library must be on the server. The derbynet.jar
is used to start the Derby Network Server. The derbyclient.jar
is used by clients connecting to the Derby Network Server. The derbytools.jar
is used with Derby tools. The derbyrun.jar
file is a special jar file that simplifies how we invoke the Derby tools and the Network Server. Finally, there are some locale libraries. They are used to provide translated messages. For example, the derbyLocale_cs.jar
provides Czech messages.
The Derby schema
In database theory, a schema is a structure of a database. It is described in a formal language (SQL). It refers to the organisation of data. The formal definition of a database schema is a set of formulas called integrity constraints imposed on a database. (Wikipedia)
In Derby, a database schema has a narrower meaning. It is a container which is used to logically group objects. It is similar to Java packages or C++ namespaces. A Derby database may have multiple schemas. We can create two tables with the same names provided that they are placed in two different schemas. Derby default schema is APP. If we do not provide any schema, a database object is assigned to this default schema.
A schema is created in Derby with CREATE SCHEMA
statement. If we connect to a database, the user name that we have provided in the connection URL becomes the current schema of the connection. All database objects will be created within this schema. The current schema can be changed with the SET SCHEMA
statement. There is another built-in schema called SYS which is used to isolate system tables.
Derby has a tool called dblook
which dumps a database and its schemas. The output of the tool is the formal description of the database in DDL (data definition language).
The connection URL
After the driver is loaded, a connection to the database is created. A connection is a session with a database. At the end of the work, the connection to the database is closed. To establish a connection, we call the getConnection()
method of the DriverManager
class.
The connection URL specifies the characteristics of a connection.
jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
The above is a syntax for a Derby database connection URL. The default subprotocol is directory: and it is often omitted. We can work with databases only in memory when we specify the memory: subprotocol. The databaseName is the name of the database that we want to create and/or connect to.
We can use several attributes in the connection URL. We can use attributes to create a database, to connect to a secured database with a user name and a password. Further we use the connection attributes to shut down a database or a Derby system, to encrypt data or to restore a database from a backup.
jdbc:derby:testdb jdbc:derby://localhost:1527/testdb
We use different connection strings to connect to a embedded and to client-server Derby system. The first connection string connects to an embedded database the second one to the client-server database. The default port for Derby is 1527.
jdbc:derby:testdb;create=true jdbc:derby:testdb;shutdown=true jdbc:derby:memory:testdb
We have another three connection strings. The first connection string creates the testdb database. The second one shuts down the testdb database. The third one connects to a testdb created in memory.
SQL files
In the following two SQL files, cars.sql
and authors_books.sql
, we create three tables which we will use throughout this tutorial.
$ cat cars.sql SET SCHEMA USER12; CREATE TABLE CARS(ID INT PRIMARY KEY, NAME VARCHAR(30), PRICE INT); INSERT INTO CARS VALUES(1, 'Audi', 52642); INSERT INTO CARS VALUES(2, 'Mercedes', 57127); INSERT INTO CARS VALUES(3, 'Skoda', 9000); INSERT INTO CARS VALUES(4, 'Volvo', 29000); INSERT INTO CARS VALUES(5, 'Bentley', 350000); INSERT INTO CARS VALUES(6, 'Citroen', 21000); INSERT INTO CARS VALUES(7, 'Hummer', 41400); INSERT INTO CARS VALUES(8, 'Volkswagen', 21600);
The cars.sql
file creates the CARS
table.
$ cat authors_books.sql SET SCHEMA USER12; CREATE TABLE AUTHORS(ID INT PRIMARY KEY, NAME VARCHAR(25)); CREATE TABLE BOOKS(ID INT PRIMARY KEY, AUTHOR_ID INT, TITLE VARCHAR(100), FOREIGN KEY(AUTHOR_ID) REFERENCES AUTHORS(ID)); INSERT INTO AUTHORS(ID, NAME) VALUES(1, 'Jack London'); INSERT INTO AUTHORS(ID, NAME) VALUES(2, 'Honore de Balzac'); INSERT INTO AUTHORS(ID, NAME) VALUES(3, 'Lion Feuchtwanger'); INSERT INTO AUTHORS(ID, NAME) VALUES(4, 'Emile Zola'); INSERT INTO AUTHORS(ID, NAME) VALUES(5, 'Truman Capote'); INSERT INTO BOOKS(ID, AUTHOR_ID, TITLE) VALUES(1, 1, 'Call of the Wild'); INSERT INTO BOOKS(ID, AUTHOR_ID, TITLE) VALUES(2, 1, 'Martin Eden'); INSERT INTO BOOKS(ID, AUTHOR_ID, TITLE) VALUES(3, 2, 'Old Goriot'); INSERT INTO BOOKS(ID, AUTHOR_ID, TITLE) VALUES(4, 2, 'Cousin Bette'); INSERT INTO BOOKS(ID, AUTHOR_ID, TITLE) VALUES(5, 3, 'Jew Suess'); INSERT INTO BOOKS(ID, AUTHOR_ID, TITLE) VALUES(6, 4, 'Nana'); INSERT INTO BOOKS(ID, AUTHOR_ID, TITLE) VALUES(7, 4, 'The Belly of Paris'); INSERT INTO BOOKS(ID, AUTHOR_ID, TITLE) VALUES(8, 5, 'In Cold blood'); INSERT INTO BOOKS(ID, AUTHOR_ID, TITLE) VALUES(9, 5, 'Breakfast at Tiffany');
The authors_books.sql
file creates two tables: AUTHORS
and BOOKS
.
Sources
The following materials were used to create this tutorial. Derby Developer's Guide, Derby Server and Administration Guide, Getting Started with Derby, Derby Tools and Utilities Guide and Derby Reference Manual.
In the chapter, we have introduced the basic concepts of the Derby database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论