Derby

Derby installation, create database, use Derby in Java program

 

1. Download and install Derby:

Download address: http://db.apache.org/derby/derby_downloads.html ,  download the latest version.

I am using 10.5.3.0.

Unzip to any folder, mine is: E:\ Java \Joy\derby

2. Configure environment variables:

Built DERBY_HOME, 值: E: \ Java \ Joy \ derby \ db- derby-10.5.3.0-bin \ db-derby-10.5.3.0-bin

Add in Path: %DERBY_HOME%\bin

在 CLASSPATH 加入 :% DERBY_HOME% \ lib \ derby.jar;% DERBY_HOME% \ lib \ derbyclient.jar;% DERBY_HOME% \ lib \ derbytools.jar;% DERBY_HOME% \ lib \ derbynet.jar

3. Test to see if Derby is installed successfully

Start-Run-CMD

run sysinfo

If the installation is successful, you will see relevant information, and the following is some intercepted information.

——— Derby 信息 ——–
JRE - JDBC: Java SE 6 - JDBC 4.0
[E: \ Java \ Joy \ derby \ db-derby-10.5.3.0-bin \ db-derby-10.5.3.0-bin \ lib \ derby.jar] 10
.5.3.0 - (802917)
[E: \ Java \ Joy \ derby \ db-derby-10.5.3.0-bin \ db-derby-10.5.3.0-bin \ lib \ derbytools.ja
r] 10.5.3.0 - (802917)
[E: \ Java \ Joy \ derby \ db-derby-10.5.3.0-bin \ db-derby-10.5.3.0-bin \ lib \ derbynet.jar]
10.5.3.0 - (802917)
[E: \ Java \ Joy \ derby \ db-derby-10.5.3.0-bin \ db-derby-10.5.3.0-bin \ lib \ derbyclient.j
ar] 10.5.3.0 - (802917)

4. Create and use the database

Open the command line and enter the directory where you want to place the database, mine is: E:\Java\Joy \derby\Derby_data

then enter ij

will see:

ij version 10.5
ij>

The next step is to create 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.

Example:
Create firstdb database:
ij> connect 'jdbc:derby:firstdb;create=true';
Connect firstdb database:
ij> connect 'jdbc:derby:firstdb';

For operations such as querying the database, you only need to enter the corresponding Sql statement.

Create a table:

create table firsttable(id int primary key, name varchar(20));

Insert data:

insert into firsttable values(1, ‘Hotpepper’);

Test it on the command line:

select * from firsttable;

The result is as follows:

ID         |NAME
——————————–
1          |Hotpepper

Script commands can also be executed:

 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".

 

[sql]view plaincopy  
  1. createtable students(   
  2.   id  numeric(20),  
  3.   namevarchar(30),    
  4.   age   numeric(6)  
  5. );  
  6. insertinto students values(10001,'Aa',10);   
  7. insertinto students values(10002,'Bb',20);   
  8. insertinto students values(10003,'Cc',30);   
  9. 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 commands to
disconnect:
ij> disconnect;
exit ij:
ij> exit;

5. Use Derby in Java programs

First, add the relevant Derby jar package (Build Path).

把derby.jar加进来,如果没有加进来会出现 “java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver”错误

好了,下面写JAVA代码进行测试:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
private static String driver = “org.apache.derby.jdbc.EmbeddedDriver”;
private static String protocol = “jdbc:derby:”;
String dbName = “E:\\Java\\Joy\\derby\\Derby_data\\firstdb”;

static void loadDriver() {
try {
Class.forName(driver).newInstance();
System.out.println(“Loaded the appropriate driver”);
} catch (Exception e) {
e.printStackTrace();
}
}

public void doIt() {
Connection conn = null;
Statement s = null;
ResultSet rs = null;

System.out.println(“starting”);
try {
conn = DriverManager.getConnection(protocol + dbName
+ “;create=true”);
} catch (SQLException e) {
e.printStackTrace();
}

System.out.println(“Connected to and created database ” + dbName);

try {

s=conn.createStatement();
rs=s.executeQuery(“select * from firsttable”);

while (rs.next()) {
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
}
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
conn.close();
conn = null;
s.close();
s = null;
rs.close();
rs = null;
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Test t = new Test();
t.loadDriver();
t.doIt();
}
}

OK,完成了。

—————————————————-

附:复制粘贴Derby数据库:

备份Derby数据库(derby-10.5.3.0)

小试了一下,很简单的,就复制粘贴就好了。

找到想要复制的数据库文件夹,比如:

C:\Program Files\SQuirreL SQL Client\firstdb

我们就把整个firstdb文件夹复制下来,然后粘贴到想要粘贴的地方。

就那么简单。

The above is a whim I tried to try, and I didn't expect it. Searched on the Internet, it seems that the derby backup method is not like this, and this method is the legendary "hot backup"?

6. Using Derby in DbVisualizer

Select the directory to the database when selecting Database.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327041007&siteId=291194637