New Features of JDK 6: embedded database Derby

Source: Internet
Author: User
Tags websphere application server

Derby is not a new database product. It is a pure Java Database donated by IBM to the Apache dB project. The Derby version included in jdk6.0 is 10.2.1.7, supports stored procedures and triggers. There are two operating modes: embedded database and network database, the former database server and client are both running in the same JVM, and the latter allows the database server and client to be not in the same JVM, and the two are allowed on different physical machines. it is worth noting that the Derby in JDK 6 supports the jdk6 new JDBC 4.0 specification (JSR 221). Now, if we want to practice JDBC usage, there is no need to install a database product separately, use Derby directly. after jdk6.0 is installed, derby will be installed under <jdk6_home>/DB. There are some sample programs in <jdk6_home>/DB/demo/programs to demonstrate how to start them, connect to the Derby database and use the jdbc api. the following two cases describe how to use code to operate the Derby Database: an embedded database and a network database.

 

1. Embedded Database

 

Public class embeddedderbytester {<br/> Public static void main (string [] ARGs) {<br/> string driver = "org. apache. derby. JDBC. embeddeddriver "; // in Derby. jar <br/> string dbname = "embeddeddb"; <br/> string dburl = "JDBC: Derby:" + dbname + "; Create = true "; // create = true indicates that the database is created when it does not exist <br/> try {<br/> class. forname (driver); <br/> connection conn = drivermanager. getconnection (dburl); // start the embedded database <br/> statement ST = Conn. createstatement (); <br/> st.exe cute ("create table Foo (fooid int not null, fooname varchar (30) not null )"); // create the foo table <br/> st.exe cuteupdate ("insert into Foo (fooid, fooname) values (1, 'chinajash ')"); // insert a data record <br/> resultset rs = st.exe cutequery ("select * From Foo"); // read the newly inserted data <br/> while (RS. next () {<br/> int id = Rs. getint (1); <br/> string name = Rs. getstring (2); <br/> system. out. println ("ID =" + ID + "; name =" + name); <br/>}< br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>} 

 

 

After running the above program, a folder named embeddeddb will be generated in the current directory, which is the place where data files of the embeddeddb database are stored. The console will output:

Id = 1; name = chinajash


Ii. Network Database

 

 

Public class networkserverderbytester {<br/> Public static void main (string [] ARGs) {<br/> string driver = "org. apache. derby. JDBC. clientdriver "; // In derbyclient. <br/> string dbname = "networkdb"; <br/> string connectionurl = "JDBC: Derby: // localhost: 1527/" + dbname + "; create = true "; <br/> try {<br/>/* <br/> Create a derby network server. The default port is 1527, you can also run <br/> <derby_home> rameworksnetworkserver instartnetworkserver. bat <br/> to create and start the Derby network server. If it is UNIX, use startnetworkserver. ksh <br/> */<br/> networkservercontrol derbyserver = new networkservercontrol (); // The networkservercontrol class is in derbynet. <br/> printwriter PW = new printwriter (system. out); // use the system output as the Derby database output <br/> derbyserver. start (PW); // start the Derby server <br/> class. forname (driver); <br/> drivermanager. getconnection (connectionurl); <br/> // do something <br/> derbyserver. shutdown (); // shut down the Derby server <br/>} catch (exception ex) {<br/> ex. printstacktrace (); <br/>}< br/>} 

 

 

 

After running the preceding program, a folder named networkdb is generated in the current directory.

For more information about Derby, see: http://db.apache.org/derby

Let's take a look at IBM's article on the backup of this database. It's more authoritative.

Http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0502thalamati/index.html


You didn't install this thing. It doesn't matter. Check this.

Apache Derby -- go to the Apache Derby download page and download the latest binary version. The sample code in this tutorial is tested using Derby 10.2.2.0.

  1. Download the compressed file, extract it to a folder (for example, C:/software/Derby), and set the derby_home environment variable to point it to this folder.
  2. Use Apache derby in network mode. However, because WebSphere Application Server community edition already uses the default port (1572) of the Derby network server, other ports are required. Go to the <derby_home>/bin folder and issue the commandjava org.apache.derby.drda.NetworkServerControl -p 1088. The Derby network server will be started on port 1088.
  3. Useij.batCommand to create a test database. First, run this command to open the IJ console.
  4. In the IJ console, execute the commandconnect "jdbc:derby://localhost:1088/ContactDB:create=true;user=dbadmin;password=not2tell";. This will create the database contactdb in Apache derby.
  5. Then you can create a table .;) The rest is very simple. You can't install the latest ecl1_3.3 version.

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.