javademo —— Java uses Derby database

2023-03-18  

jdk6 to JDK8 comes with Derby database. DB/lib will find Derby.jar under the jdk directory.

Other JDK versions are imported with Maven:

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>10.14.2.0</version>
    <!-- <scope>test</scope> -->
</dependency>

Note:

1. Remark <Scope> test </scope>, otherwise Maven will not find the Derby’s JDBCDRIVER class;https://stackoverflow.com/questions/21262316/java-lang-classnotfoundexception-org-apache-derby-jdbc-embeddeddriverFind);

2.Derby’s 10.15.x version does not have EmbeddedDriver, and 10.14.x version is required; 10.14.x;

Demo:

/** 
  * 9:31:21 am on April 22, 2019 
  */ 
 package testderby; 

 Import java.sql.connection; 
 Import java.sql.driverManager; 
 Import java.sql.preparedStatement; 
 Import java.sql.ResultSet; 
 Import java.sql.sqlexception; 
 Import java.sql.statement; 

 /** 
  * @Author xwf 
  * 
  */ 
 public class testBy { 

 /** 
 * @param ARGS 
 */ 
 Public Static Void Main (String [] args) {{ 
 try { 
 Class.Forname ("ORG.APACHE.DERBY.JDBC.embeddeddriver"); 
 Connection conn = driverManager.getConnection ("jdbc: Derby: Derbydb; Create = TRUE"); 
 // connection conn = driverManager.getConnection ("jdbc: derby: derbydb"); 
 
 Statement state = conn.createStatement (); 
 State.executeupDate ("Create Table Derbytable (ID Int, Val Varchar (128))"); 
 state.close (); 
 
 Statement state2 = conn.createStatement (); 
 State2.executeupDate ("Insert Into Derbytable Values (1, 'Tom')"); 
 State2.executeupDate ("Insert Into Derbytable Values (2, 'Jerry')"); 
 state2.close (); 
 
 Preparedatement pState1 = const.preparestatement ("Select * from derbytable where id =?"); 
 pstate1.Setint (1, 2); 
 ResultSet RSET1 = PState1.executequry (); 
 While (rset1.next ()) {{ 
 System.out.println (RSET1.Getint (1)+">"+RSET1.GetString (2)); 
 } 
 pState1.close (); 
 
 conn.close (); 
 } Catch (sqlexception e) {{ 
 e.printstacktrace (); 
 } Catch (ClassNotFoundException E) {{ 
 e.printstacktrace (); 
 } 
 } 

 }

Result:

Reference:

http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html

source

Random Posts

The stack and queue of algorithm series (1): Design a stack to get the maximum value and minimum value in the stack

nginx compilation and installation often see error analysis

Detailed explanation: nested problems of pointers, array and functions

Solving the problem of incompleteness of cycript information display

ACM-ICPC 2018 Nanjing Division Online Preliminaries L Magical Girl Haze (Dijkstra deformation)