Derby database usage

Source: Internet
Author: User

Derby database is an open-source dB project under Apache. It has the following advantages over other databases:

1. fully implemented in Java

2. Support for JDBC and SQL standards

3. Only about 2 MB of all files

4. Provides embedded support and can be easily embedded into other Java projects.

5. The network database function is also provided and can be accessed in C/S mode.

 

This article briefly introduces how to use Derby:

1. Download

The Derby Project homepage is.

Ii. Installation

Derby downloads a file as a zip file. You only need to extract the file to a directory (replace derby_home here.

3. Local access

Derby can be used as a local database, which is like access. The following describes how to use the IJ tool provided by Derby to create a new database.

Create a new demo directory and run it in the demo directory:

Derby_home/bin/IJ 

Enter

Connect 'jdbc: Derby: mydemo; Create = true ';

Create a new database named mydemo and save it in the current directory. For more information about how to use IJ, enter help.

Iv. Network Access

In the demo directory, run

Java-jar derby_home/lib/derbyrun. Jar Server start

The Derby server is successfully started. The default port is 1527. Open another console and run

IJ <br/> connect 'jdbc: Derby: // localhost: 1527/mydemo ';

You can successfully connect to the mydemo database you just created. To facilitate the following test, run the following statement to create a test table and insert two pieces of data.

Create Table Test (ID int, name varchar (20); <br/> insert into test values (0, 'jack '); <br/> insert into test values (1, 'Tom ');

V. Java code access

Create a Java file with the following code:

Import Java. SQL. connection; <br/> Import Java. SQL. drivermanager; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. sqlexception; <br/> Import Java. SQL. statement; </P> <p> public class derbydemo {</P> <p> static string embeded_driver = "org. apache. derby. JDBC. embeddeddriver "; <br/> static string net_driver =" org. apache. derby. JDBC. clientdriver "; </P> <p> static string db_name =" mydemo "; <br/> static string H Ost_url = "// localhost: 1527 /"; </P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> connection conn = NULL; <br/> statement stmt = NULL; <br/> resultset rs = NULL; <br/> try {<br/> conn = getnetwrokconnect (); <br/> stmt = Conn. createstatement (); <br/> rs = stmt.exe cutequery ("select * from test"); <br/> while (RS! = NULL & RS. next () {<br/> system. out. println (RS. getstring (2); <br/>}< br/>} catch (exception ex) {<br/> system. out. println (ex. getmessage (); <br/>}finally {<br/> try {<br/> Rs. close (); <br/>}catch (exception e) {<br/>}< br/> rs = NULL; <br/> try {<br/> stmt. close (); <br/>}catch (exception e) {<br/>}< br/> stmt = NULL; <br/> try {<br/> Conn. close (); <br/>}catch (exception e) {<br/>}< br/> conn = NULL; <br/>}</P> <p> Public static connection getembededconnect () {<br/> try {<br/> class. forname (embeded_driver); <br/>} catch (classnotfoundexception e) {<br/> // todo auto-generated Catch Block <br/> throw new runtimeexception (E. getmessage (); <br/>}< br/> try {<br/> return drivermanager. getconnection ("JDBC: Derby:" + db_name); <br/>} catch (sqlexception E) {<br/> // todo auto-generated Catch Block <br/> throw new runtimeexception (E. getmessage (); <br/>}</P> <p> Public static connection getnetwrokconnect () {<br/> try {<br/> class. forname (net_driver); <br/>} catch (classnotfoundexception e) {<br/> // todo auto-generated Catch Block <br/> throw new runtimeexception (E. getmessage (); <br/>}< br/> try {<br/> return drivermanager. getconnection ("JDBC: Derby:" + host_url <br/> + db_name); <br/>} catch (sqlexception E) {<br/> // todo auto-generated Catch Block <br/> throw new runtimeexception (E. getmessage (); <br/>}</P> <p >}< br/>

Save derbydemo to the demo directory and run the following code to compile and run it to access the server through the network (Step 4 is required to start the server)

Javac-CP derby_home/lib/derbyclient. Jar derbydemo. Java <br/> JAVA-CP.; derby_home/lib/derbyclient. Jar derbydemo

To use embedded access, you only need to change getnetwrokconnect in main to getembededconnect.

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.