xzknet 发表于 2013-1-14 23:07:05

JDK6的新特性之八:嵌入式数据库Derby

<div class="middleSize">Derby并不是一个新的数据库产品,它是由IBM捐献给Apache的DB项目的一个纯Java数据库,JDK6.0里面带的这个Derby的版本是10.2.1.7,支持存储过程和触发器;有两种运行模式,一种是作为嵌入式数据库,另一种是作为网络数据库,前者的数据库服务器和客户端都在同一个JVM里面运行,后者允许数据库服务器端和客户端不在同一个JVM里面,而且允许这两者在不同的物理机器上.值得注意的是JDK6里面的这个Derby支持JDK6的新特性JDBC 4.0规范(JSR 221),现在我们如果要练习JDBC的用法,没有必要单独装一个数据库产品了,直接用Derby就行.安装完JDK6.0后,Derby会被安装到<JDK6_HOME>/db下面,在<JDK6_HOME>/db/demo/programs下面还有一些示例程序,演示了如何启动,连接Derby数据库以及JDBC API的使用.下面分两种情况演示一下如何用代码操作Derby数据库,一种是嵌入式数据库,一种是网络数据库.
一.嵌入式数据库<span style="color: #000000;" />
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublic class EmbeddedDerbyTester ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static void main(String[] args) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        String driver = "org.apache.derby.jdbc.EmbeddedDriver";//在derby.jar里面
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        String dbName="EmbeddedDB";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        String dbURL = "jdbc:derby:"+dbName+";create=true";//create=true表示当数据库不存在时就创建它
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        try ...{           
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Class.forName(driver);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Connection conn = DriverManager.getConnection(dbURL);//启动嵌入式数据库
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Statement st = conn.createStatement();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            st.execute("create table foo (FOOID INT NOT NULL,FOONAME VARCHAR(30) NOT NULL)");//创建foo表
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            st.executeUpdate("insert into foo(FOOID,FOONAME) values (1,'chinajash')");//插入一条数据
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            ResultSet rs = st.executeQuery("select * from foo");//读取刚插入的数据
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            while(rs.next())...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                int id = rs.getInt(1);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                String name = rs.getString(2);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                System.out.println("id="+id+";name="+name);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        } catch(Exception e)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            e.printStackTrace();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页: [1]
查看完整版本: JDK6的新特性之八:嵌入式数据库Derby