一. Derby能提供什么?
Derby是一个基于Java和SQL ,开源的 RDBMS 。完全使用Java实现,他为用户提供了一个小巧的基于标准的数据库引擎,他可以嵌入到任Java解决方案中,他确保数据完成并提供复杂的事务支持。Derby的数据完全存储在磁盘上,他是一可携带的数据库,你可以将他从一台机器拷贝到另一台机器。更多信息请访问 http://db.apache.org/derby
二. Derby如何开始?
Derby的两种模式:内嵌模式【他和你应用程序在同一个JVM中,不需要单独配置和你的应用程序有相同的生命周期】、服务模式【运行在独立的JVM中,需要网络配置这点类似于Mysql等数据库】
系统要求:Derby 使用 Java 开发依赖于JDK ,要求JDK版本 1.5+ 。查看版本可以使用命令 java -version
下载安装Derby : 下载地址: http://db.apache.org/derby/derby_downloads.html
设置环境变量: DERBY_HOME 变量值=解压的目录,目录的结构就不解释了。
设置Path 和 CLASSPATH : 在 Path中增加 %DERBY_HOME%\bin 、在 CLASSPATH 中增加 %DERBY_HOME%\lib
三. 有哪些工具?怎样用?
ij 是Derby交互式JDBC脚本工具,使用该工具可以使用SQL脚本和数据库交互。
配置完环境变量后有很多种方式启动ij工具:
1.在命令行使用 java -jar %DERBY_HOME%\lib\derbyrun.jar ij
2.在命令行使用 java org.apache.derby.tools.ij
3.直接使用 ij 或 双击%DERBY_HOME%\bin 下 ij.bat
启动后看到 说明启动成功
连接到数据库 connect 'jdbc:derby:mydb;create=true';
说明:connect 是ij工具内置的命令用于连接到数据库,mydb是数据库名称,create=true 说明如果数据库不存在就创建,创建位置为当前工作空间,该位置可以指定;
新建表 create table mytable(id int primary key,name varchar(20) ) ;
录入数据 insert into mytable(10,'shi'),(20,'ershi'),(30,'sanshi');
查询数据 select * from mytable;
现在我们已经有一个名为mydb的derby数据库了。并且数据库中已有一个表mytable和三条数据。
sysinfo 查询Derby版本信息和环境信息
dblook
SignatureChecker
PlanExporter
四.JDBC用法(代码)
数据库连接类 DBConnection
- public class DBConnection {
- private final static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
- public static Connection getConnection(){
- String dbName="mydb";
- Connection conn=null;
- try {
- Properties props = new Properties(); // connection properties
- // providing a user name and password is optional in the embedded
- // and derbyclient frameworks
- //props.put("user", "user1");
- //props.put("password", "user1");
- Class.forName(driver).newInstance();
- System.out.println("Loaded the appropriate driver");
- conn = DriverManager.getConnection("jdbc:derby:"+dbName+";create=true", props);
- System.out.println("Connected to and created database " + dbName);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return conn;
- }
-
- public static void shutdwon(){
- try {
- DriverManager.getConnection("jdbc:derby:;shutdown=true");
- } catch (SQLException se) {
- if (( (se.getErrorCode() == 50000)
- && ("XJ015".equals(se.getSQLState()) ))) {
- // we got the expected exception
- System.out.println("Derby shut down normally");
- // Note that for single database shutdown, the expected
- // SQL state is "08006", and the error code is 45000.
- } else {
- // if the error code or SQLState is different, we have
- // an unexpected exception (shutdown failed)
- System.err.println("Derby did not shut down normally");
- se.printStackTrace();
- }
- }
- }
- }
数据库访问类 myTableDao
- public class myTableDao{
-
- public int save(String email){
- Connection conn = DBConnection.getConnection();
- int result=;
- try {
- conn.setAutoCommit(false);
- PreparedStatement pstm = conn.prepareStatement("insert into location values (?)");
- pstm.setString(1, email);
- result=pstm.executeUpdate();
- conn.commit();
- if(pstm != null){
- pstm.close(); pstm = null;
- }
- if (conn != null) {
- conn.close();conn = null;
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return result;
- }
-
- public int delete(String email){
- Connection conn = DBConnection.getConnection();
- int result=;
- try {
- conn.setAutoCommit(false);
- PreparedStatement pstm = conn.prepareStatement("delete from location where addr=?");
- pstm.setString(1, email);
- result=pstm.executeUpdate();
- conn.commit();
- if(pstm != null){
- pstm.close(); pstm = null;
- }
- if (conn != null) {
- conn.close();conn = null;
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return result;
- }
-
- public int update(String email,String nemail){
- Connection conn = DBConnection.getConnection();
- int result=;
- try {
- conn.setAutoCommit(false);
- PreparedStatement pstm = conn.prepareStatement("update location set addr=? where addr=?");
- pstm.setString(1, nemail);
- pstm.setString(2, email);
- result=pstm.executeUpdate();
- conn.commit();
- if(pstm != null){
- pstm.close(); pstm = null;
- }
- if (conn != null) {
- conn.close();conn = null;
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return result;
- }
-
- public List<String> query(){
- Connection conn = DBConnection.getConnection();
- List<String> list=null;
- try {
- PreparedStatement pstm = conn.prepareStatement("SELECT addr FROM location");
- ResultSet rs = pstm.executeQuery();
- list = new ArrayList<String>();
- while (rs.next())
- {
- list.add(rs.getString(1));
- }
- if(rs != null){
- rs.close(); rs = null;
- }
- if(pstm != null){
- pstm.close(); pstm = null;
- }
- if (conn != null) {
- conn.close();conn = null;
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return list;
- }
-
- //初始化表
- public void initTable(){
- try {
- Connection conn = DBConnection.getConnection();
- conn.setAutoCommit(false);
- Statement s = conn.createStatement();
- s.execute("create table location(addr varchar(40))");
- System.out.println("Created table location");
- conn.commit();
- if(s != null){
- s.close(); s= null;
- }
- if (conn != null) {
- conn.close();conn = null;
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
补充:需要在工程中导入 derby.jar
五.分页语句如何写
SELECT id, jobid, logtime, msgtext FROM log_table where 1=1 order by logtime desc OFFSET ? ROWS FETCH NEXT ? ROWS ONLY
说明:自己试试