Real Derby Beginner tutorial, Derby installation, creating a database, using Derby in Java programs

Source: Internet
Author: User
Tags sql client java se

1. Download and install Derby:

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

I'm using 10.5.3.0.

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

2. Configure Environment variables:

Build Derby_home, Value: E:\Java\Joy\derby\db-derby-10.5.3.0-bin\db-derby-10.5.3.0-bin

Join in Path:%derby_home%\bin

Join in Classpath:%derby_home%\lib \derby.jar;%d erby_home%\lib\derbyclient.jar;%d erby_home%\lib\derbytools.jar;%d Erby_home%\lib\derbynet.jar

3. Test to see if the Derby installation was successful.

Start-run-cmd

Run SysInfo

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

——— Derby Information--–
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 a database

Open the command line and go to the directory where you want to put the database, mine is: E:\Java\Joy \derby\derby_data

then enter IJ

You'll see:

IJ Version 10.5
Ij>

Next, you can create a database.

Cases:
To create a FIRSTDB database:
Ij> Connect ' jdbc:derby:firstdb;create=true ';
To connect the FIRSTDB database:
Ij> Connect ' jdbc:derby:firstdb ';

Query the database, and so on, only need to enter the appropriate SQL statement.

Create a table:

CREATE TABLE firsttable (ID int primary key, name varchar (20));

Insert data:

INSERT into firsttable values (1, ' hotpepper ');

Test at the command line:

SELECT * from Firsttable;

The results are as follows:

ID |name
——————————–
1 | Hotpepper

Other commands
To disconnect the connection:
Ij> Disconnect;
Exit IJ:
Ij> exit;

5, using Derby in Java programs

The first thing to do is to add the relevant Derby jar package (Build Path).

Add the Derby.jar, if not added, there will be a "java.lang.ClassNotFoundException:org.apache.derby.jdbc.EmbeddedDriver" error

Okay, here's the Java code to test:

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, it's done.

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

Attached: Copy and paste Derby database:

Backing Up the Derby database (derby-10.5.3.0)

Small try, very simple, just copy and paste just fine.

Locate the database folder you want to copy, for example:

C:\Program Files\squirrel SQL Client\firstdb

We'll just copy the entire Firstdb folder and paste it where you want to paste it.

It's that simple.

The above is my whim to try, did not expect to become. Search on the Internet, it seems that Derby backup method is not the case, and this method is the legendary "Hot backup"?

See

http://www.infoq.com/cn/news/2007/06/derby103

Original: http://www.joyzhong.com/archives/558

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.