1. Introduction to Derby Database
1. Overview of the development history and characteristics of Derby
Derby is an open source, 100% Java-developed relational database. With the popularity of the Java platform, Derby has also received
more and more attention. Derby's predecessor is ColdScape of IBM Corporation of the United States. In April 2004, IBM will
CloudScape's database was donated to the Apache Software Foundation, which renamed it Derby,
Then SUN also donated a team to Derby. The difference is that in JavaSE6.0, SUN named it JavaDB.
So CloudScape, Derby, and JavaDB are just different names for the project at different times.
When it comes to relational databases, it always makes people feel that the installation is cumbersome and takes up a lot of space. Not so with Derby, however.
No installation required, you can use it directly. At the same time, the accompanying Derby only takes up less than 10MB of space in JavaSE6.0.
Don't think Derby is weak, it fully supports the SQL92 standard and many SQL99 extensions, and
It provides the features of large databases such as transactions, crash recovery, and concurrent connections, and manages dozens of GB of data with ease.
high speed.
At the same time, since Derby is developed by pure java, it is also born with the cross-platform nature of java, which can be very good.
Works on various operating systems. In addition, derby can not only work in a C/S way like a traditional database, but also
Embedded working mode is also supported.
2. The directory structure of derby in JavaSE 6.0
The default is the installation path
Several jar files in the "lib" directory
derby.jar------- contains some necessary classes when using derby database, such as the JDBC driver class of derby database.
derbytools.jar - provides some utilities for derby databases, such as command line tools for managing derby databases.
derbyclient.jar-provides some necessary classes for developing derby data clients, by using the classes in this jar package,
Client programs that access the derby database server in network mode can be developed.
derbynet.jar----classes that provide support for the derby database server in network mode.
2. Management tools ij
Under normal circumstances, each database system will provide management tools, such as mysql's command line client, oracle's sql*plus, etc.
Derby is no exception, providing a very powerful command-line tool - ij.
1. Preparation
Before using this tool, you need to do some preparatory work first, mainly adding the jar package Lujin required by the ij tool into the classpath environment variable,
And set an environment variable named DERBY_INSTALL, the steps are as follows:
①Modify/increase the classpath environment variable (I am the user environment variable here)
variable name: classpath
变量值:C:\Program Files\Sun\JavaDB\lib\derbytools.jar;C:\Program Files\Sun\JavaDB\lib\derby.jar
If you are modifying the classpath environment variable, don't forget to add a semicolon.
Tip: Set it according to the installation of java db in your own machine.
②Increase the DERBY_INSTALL environment variable
Variable name: DERBY_INSTALL
Variable value: C:\Program Files\Sun\JavaDB
Tip: Set it according to the installation of java db in your own machine.
After completing the above steps, you are ready to use the ij command line tool. To verify that the preparation was successful,
ij can work normally, enter the following command in the command line prompt window (cmd)
java org.apache.derby.tools.sysinfo
If there is no problem with the settings of the preparation work, a lot of information about the java system will be displayed in cmd, as shown in the following figure
2. Simple to use
ij is a powerful database management tool that comes with derby, which can perform many database management operations, including creating databases,
Start/shutdown databases, execute SQL scripts, etc. Once the preparations are done, it is time to start and use the ij tool.
Enter the following command in cmd to start the ij tool. The running status of java org.apache.derby.tools.ij is shown in the following figure:
Several commonly used ij commands are introduced below, as listed below:
①Create and connect to the database
The syntax format of the command command to create and connect to the specified database using ij is as follows:
connect 'jdbc:derby:<database path>[;create=True/False]';
Database Roway refers to the location where the specified database is stored on the disk, such as "E:\roway". In addition, the path can also use a relative path,
For example, "roway", this path indicates the roway subdirectory under the current execution directory. If the current execution directory is "E:\", the real scene path is "E:\roway".
The content of the square brackets is optional, that is to say, it can be omitted. If not, it is equivalent to "create=False".
"create=False" means to only connect to the existing database, and "create=True" means to create the database if it does not exist.
Tip: The commands to connect and create a database described above are for the embedded working mode of derby.
Commands used in the network case will be described later.
For example, the following gives a command to create a database in the roway directory under the E drive
connect 'jdbc:derby:e:/roway;create=True';
Once the database is created and connected, you can use other commands or run SQL scripts to operate on the database.
Note: When the database does not exist for the first time, use the "create=True" parameter to create and connect to the database.
Later you can use the "create=False" parameter to only connect to the database.
②Run the SQL statement
③Run the SQL script
The above describes the direct input and execution of SQL statements in ij, which is very useful when the operation is simple. If the operation is complicated, input directly
It is very inconvenient. Therefore, ij also allows users to use the run command to execute the specified SQL script. The basic command format is as follows:
run '<path to SQL script file>';
Enter the following SQL script in a text editor and save it as a derby.sql script file, such as "E:/derby.sql".
- createtable students(
- id numeric(20),
- namevarchar(30),
- age numeric(6)
- );
- insertinto students values(10001,'Aa',10);
- insertinto students values(10002,'Bb',20);
- insertinto students values(10003,'Cc',30);
- select * from students;
Connect to the database created earlier, and use the run command to execute the "derby.sql" script file, as shown in the following figure:
④Other common commands
In addition to the several more important commands introduced earlier, there are some other commonly used commands, such as closing database connections, etc.
disconnect;-------------Disconnect the database connection
Exit;---------------------------------Exit the ij tool
Third, the embedded application of Derby database
1. The working principle of embedded derby
The java application that accesses the derby database in the embedded working mode works in the same JVM as the derby database engine,
Unlike other databases (such as oracle), the database connection is connected to a database engine outside the JVM. The advantage of this is that,
The steps of database software installation and data source configuration are omitted. With the startup of the java application, the database also starts to work.
It is especially suitable for the development of stand-alone small software or test programs.
But it should be noted that the connection of the application accessing the database in the embedded working mode is exclusive, which means that other
Applications cannot access the database at the same time.
2. Development steps of embedded derby application
The development of embedded derby applications is not very different from the usual JDBC database development. The basic steps are still
Loading the database driver, creating a database connection, and manipulating the database through the connection are just slightly different in details.
①Load the database driver
The derby database needs to be connected, and the jdbc driver class of the derby database needs to be loaded, which is located in the derby.jar file in the "lib" directory.
The path to the derby.jar file needs to be added to the classpath path when running the program.
- //Load the JDBC driver class of the embedded derby
- Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Note: The jdbc driver class of derby in network mode is not this one, which will be introduced later.
②Database connection string
After successfully loading the database driver class, you can get the database connection through the connection string,
The basic format of the connection string is given below:
jdbc:derby:<database path>[;create=True/False]
In addition, it should be noted that since the introduced derby works in embedded mode, the database should be closed before the application exits.
Closing the embedded derby database also uses the connection string, the syntax format is given below.
jdbc:derby:;shutdown=True
- //Connect to the specified derby database
- Connection conn = DriverManager.getConnection("jdbc:derby:e:/roway;create=False","","");
- //Close the connected database
- DriverManager.getConnection("jdbc:derby:;shutdown=True");
Tip: After closing the database successfully, the getConnection() method will throw an exception to notify.
③Operate the database
The code used to operate on the derby database is exactly the same as that used to access other databases,
It also uses the Statement or PreparedStatement obtained through the database connection to execute the specified SQL statement.
Note: From the introduction of the above 3 steps, it can be seen that accessing the derby database in embedded mode is basically the same as accessing other databases.
It is very easy to port applications that use other databases to derby.
3. Simple case of using embedded derby
TIP: Developing with an embedded derby database is not much different from a coding point of view from using other databases,
But it saves you the trouble of installing and configuring the database, which can be very useful in certain situations.
Fourth, the network mode application of Derby database
The derby database can not only work in embedded mode, like other databases, but also in network mode
Serving multiple applications at the same time, handling database operation requests from different JVMs.
1. The working principle of network mode derby
If you have used some other large databases, such as oracle, db2, etc., then the network of the database should be
The principle of the working mode (c/s mode) is quite familiar.
In this mode derby works like other databases on the network as an independent server, waiting for other
Connection requests for java applications. In this mode, derby can serve different java applications at the same time, processing data from different JVMs
database operation requests, that is to say, multiple applications can access the same derby database at the same time.
Description: When it is not suitable to operate with embedded, such as the application and the database are not on the same machine,
Or when the database requires concurrent access by multiple users, derby in network mode can be used.
2. Operate derby in network mode
Next, we will introduce how to operate derby in network mode, including starting derby in network mode.
and derby using the ij tool to connect network mode.
①Start derby in network mode
Before operating the derby database in network mode, first start the derby database server in network mode.
The basic format of the startup command is given below:
java org.apache.derby.drda.NetworkServerControl start [-h <host> [-p <port>]]
NetworkServerControl is a class provided by the system for starting the derby server, located in the derbynet.jar file.
Therefore, you need to add the derbynet.jar file path to the system's classpath environment variable.
Square brackets are optional parameters, "-h <host>" is used to specify the host, "-p <port>" is used to specify the port number,
The default host is localhost and the default port number is 1527.
For example, the following gives a command to start the derby database server working on port 1527 of the local machine.
java org.apache.derby.drda.NetworkServerControl start -h localhost -p 1572
When the network service of the derby database is successfully started, the derby database in the machine can be accessed from other machines in the network or this machine
done. It should be noted that during the process of performing network operations, the cmd window cannot be closed, and once closed, the network service will be disconnected.
②Use the ij tool to connect to the derby in network mode
When the network service of the derby database is started, you can use the ij tool to connect and operate,
The name format of the connection network mode derby in ij is given below.
connect 'jdbc:derby://<server host address>:<server port number>/<database path>[;create=True|False]';
Tip: Before using the above command to connect to the derby database server, you first need to add the derbyclient.jar file path to the system's
In the classpath environment variable,
For example, the following command connects to the derby database service started above.
connect 'jdbc:derby://localhost:1572/E:/roway';
If the above command is successfully executed in ij, the connection to the specified database is successfully established, and then various commands can be used to
The database has been operated. The commands to operate on the network derby database are exactly the same as the embedded derby.
3. Develop a program to start the derby network service
In practical applications, not only can the network service of the derby database be started by the command introduced above, but also can be developed by oneself.
java program to start the network service of the derby database.
As can be seen from the previous introduction, the network service that starts the derby database through the command needs to use the
org.apache.derby.drda.NetworkServerControl class. Similarly, it is also necessary to develop a program to start the network service of the derby database
Using this class, those who are interested can consult the API by themselves.
4. Simple case of using network mode derby
The JDBC driver class org.apache.derby.jdbc.ClientDriver required by the client to connect to the network derby server