Introduction to small database Derby and database derby

Source: Internet
Author: User

Introduction to small database Derby and database derby
1. Derby Introduction

The reason for focusing on small Derby is that it is pure green, lightweight, and has a small memory usage. You can run it on your machine every minute. It is very convenient to do code practices that require you to connect to the database.

Even Mysql is fine, isn't it a good choice?

Apache Derby is a database fully written in java, and Derby is an Open source product.

Apache Derby is very small. The core part of derby. jar is only 2 MB, which can be used as a separate database server or embedded in applications.

Official Website: http://db.apache.org/derby/derby_downloads.html

Click to enter the version, pay attention to the conditions required for building Environment, and click Download zip to decompress the arbitrary directory.

2. Slightly configure the Environment Variables

Derby is written based on Java. You need to have a Java JRE on your machine. The specific installation and configuration will not be mentioned.

The following are the system environment variables that Derby needs to configure. to configure the environment variables, you want the system to know where to find the corresponding execution program of the command.

Name: DERBY_HOME Value: E: \ Java \ derby \ db-derby-10.10.1.1-bin in Path Add: % DERBY_HOME % \ bin in CLASSPATH Add: % DERBY_HOME % \ lib \ derby. jar; % DERBY_HOME % \ lib \ derbyclient. jar; % DERBY_HOME % \ lib \ derbytools. jar; % DERBY_HOME % \ lib \ derbynet. jar

Switch to the cmd black box and tap sysinfo.

So far, the small Derby has been successfully installed on your computer, isn't it fast? Cool.

3. Derby operations and Java access

A. Create a database and connect to the database. (If yes, connect to the database. If no, connect to the database after creation)

connect 'jdbc:derby:dedb;user=root;password=root;create=true'; 

B. Create a system user table

create table t_user(uuid varchar(32), name varchar(10), age int, address varchar(40));

C. insert some test data

insert into t_user values('B82A6C5244244B9BB226EF31D5CBE508', 'Miachel', 20, 'street 1');insert into t_user values('B82A6C5244244B9BB226EF31D5CBE509', 'Andrew', 35, 'street 1');insert into t_user values('B82A6C5244244B9BB226EF31D5CBE510', 'Orson', 47, 'street 1');insert into t_user values('B82A6C5244244B9BB226EF31D5CBE511', 'Rambo', 19, 'street 1');

Note: To operate Derby, you need to use the ij tool (similar to the oracle plus). Enter ij under CMD to enter the ij mode;

The path for creating a database depends on the path of your CMD, such as C: \ Users \ Administrator>. The created Derby database is under this directory;

If you are familiar with SQL, you can operate derby without any problems.

E. Use Derby in Java

import java.sql.*;public class DerbyTest {    private static String driver = "org.apache.derby.jdbc.EmbeddedDriver";    private static String protocol = "jdbc:derby:";    String dbName = "E:\\Users\\Workspaces\\Derby\\dedb";    public static void loadDriver() {        try {            Class.forName(driver).newInstance();        } catch (Exception e) {            e.printStackTrace();        }    }    public void getDataFromDerby() {        try {            Connection conn = DriverManager.getConnection(protocol + dbName + ";user=root;password=root;create=true");            Statement statement = conn.createStatement();            ResultSet resultSet = statement.executeQuery("select * from t_user");            while (resultSet.next()) {                System.out.println(resultSet.getString(1));                System.out.println(resultSet.getString(2));            }            conn.close();            statement.close();            resultSet.close();        } catch (Exception e1) {            e1.printStackTrace();        }    }    public static void main(String[] args) {        DerbyTest derbyTest = new DerbyTest();        loadDriver();        derbyTest.getDataFromDerby();    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.