Three minutes to learn to use the Derby database

Derby database using in-memory database is a pure Java implementation of a part of the Apache open source project. Because it is implemented in Java, can be run on any platform; Another feature is small, free installation, start java1.6 derby integrated database, located in the directory jdk db below.

  1. Environment variable configuration

  • CLASSPATH =
    1
    2
    3
    4
    5
    ‪C:openSourcejdk1.8.0_162dblibderby.jar;
    ‪C:openSourcejdk1.8.0_162dblibderbyclient.jar;
    ‪C:openSourcejdk1.8.0_162dblibderbytool.jar;
    ‪C:openSourcejdk1.8.0_162dblibderbynet.jar;
    ‪C:openSourcejdk1.8.0_162dblibderbytools.jar;
  • PATH =
    1
    C:openSourcejdk1.8.0_162dbbin
  • If you do not want to configure, you can use the following commands to simplify your use
  • Provided in the / bin directory of the Derby database in several scripts for setting the classpath, to simplify you manually add a jar package in the classpath trouble:

  • setEmbeddedCP. When using the embedded mode to run Derby, you can use the script to set. The script adds derby.jar and derbytools.jar to environment variables;
    setNetworkServerCP. When using the network to run the Derby mode, use this script to set the classpath variable Derby server-side. The script adds derbynet.jar to the environment variable;
  • setNetworkClientCP. When using the network to run the Derby mode, use this script to set the classpath variable Derby client. The script adds derbyclient.jar and derbytools.jar to the environment variable.
    Generally only when you run the Derby tools derbyrun.jar will use these scripts.

  1. Derby provides three tools script:

    After the bin directory derby database adding environment variables, you can use the following script from the command line

  • sysinfo
    use sysinfo can display the version information about your Java environment information and the Derby. Usage is entered directly at the command line:
    sysinfo.bat
  • dblook
    use dblook can export all or part of the DDL definitions database to the console or a file. Usage:
    dblook.bat -d [Options]
  • ij
    using the ij tool to interact with the database, execute SQL scripts, such as queries, additions and deletions, create tables and so on. At the command line, type:
    the ij.bat
    to start the ij tool, then you can begin to execute SQL script. When you want to exit the ij tool, enter the command line
    exit;
    can be.

Note: the command line using derby, both the service (using the service to start the database
) or connection file to be executed ij command line, enter the derby database;


  1. Command line using derby:

  • Way services (independent database)

    In this mode, the console requires the use of two windows, one used to start the Derby database server, the other as a visiting Derby database client.
    You can start the Derby database server by == startNetworkServer.bat == DERBY database under the / bin directory, just type == == in the command line:
    == startNetworkServer.bat ==
    or entered on the command line

    1
    java -jar  derbyrun.jar server start;

Started the database, a successful start in the console will output the following information:
Used basic server security policy installed safety management procedures.
Apache Derby Network Server - 10.4.1.3 - ( 648739) has started and is ready 2008-09-06
00: 38: 12.540 GMT accepting connections on port 1527

  • Ij enter on the command line, enter the database
    1
    2
    3
    connect 'jdbc:derby://localhost:1527/db_name';
    或者是
    connect 'jdbc:derby://localhost:1527/db_name;user=root;password=root;create=true;';

user = root; password = root; to create a user.

create = true no database, then create a


  1. Way linked files (embedded database):

  • Ij enter on the command line, enter the database
  • Then enter the following command (note db_name can be relative or absolute path)
    1
    connect 'jdbc:derby:db_name';

Once connected to the server, you can begin to execute SQL scripts, and:

  • Such as creating a table

    table firsttable(id int primary key, name varchar(20)); ```
    1
    2
    - then insert the recording: 
    `` `INSERT INTO firsttable values (. 1, 'Hotpepper');
  • Also you can execute the query:

    * from firsttable; ```
    1 
    second large column   three minutes to learn to use the Derby database an>
    - you may be performed by a run command file sql: 
    `` `run 'E: /derby/demo/programs/toursdb/ToursDB_schema.sql';
  • Finally, exit; to exit the ij tool


  1. Use jdbc connection (a separate database) in java

    Note derby of all the drivers in the lib directory

  • First derbycilent.jar import the items, and then click buildpath

    java code

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    public class Db {
    public static void main(String[] args) {
    try {
    // 创建实例
    Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
    // 获得数据库连接
    Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/firstdb");
    String sql = "select name from stu";
    PreparedStatement ps = conn.prepareStatement(sql);
    // 执行查询语句
    ResultSet rs = ps.executeQuery();
    while (rs.next()) {
    System.out.println(rs.getString("name"));
    }

    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (SQLException e) {

    e.printStackTrace();
    } catch (InstantiationException e) {

    e.printStackTrace();
    } catch (IllegalAccessException e) {

    e.printStackTrace();
    }
    }
    }
  1. Connection may be used within the insert JDBC database connection (to a final drive and load can Conncetion)

Db_name here can be a relative path or an absolute path, shutdown = true; embedded database users when using the derby has the responsibility to close the database

  • Please derby.jar added to the project build path path
1
2
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:derby:db_name;shutdown=true);
Tips: Note that the network mode and an embedded pattern is different in that:
  • Database Connectivity different URL. (Stand-alone server mode is the name of the database library, and the other is the path of the database)
  • When you exit the application invalid closed Derby database; (embedded database must be closed)
  • Different database-driven;

  • Close the database
    1
    Java -jar derbyrun.jar server stop performing the command line;

About the operation of the database will not say I'm here, there is a need to learn what users can own Baidu

  • [X] Reference

Derby net server doc

Derby Getting Started

gsls200808 column


Guess you like

Origin www.cnblogs.com/lijianming180/p/12032701.html