jdk 6 Derby 数据库
<span style="font-family: Arial, sans-serif, Helvetica, Tahoma;"><span style="line-height: 18px;"><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数据库,一种是嵌入式数据库,一种是网络数据库.一.嵌入式数据库
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background-color: #e6e6e6; padding-bottom: 4px; width: 680px; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifpublicclassEmbeddedDerbyTester{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifpublicstaticvoidmain(String[]args){
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifStringdriver="org.apache.derby.jdbc.EmbeddedDriver";//在derby.jar里面
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifStringdbName="EmbeddedDB";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifStringdbURL="jdbc:derby:"+dbName+";create=true";//create=true表示当数据库不存在时就创建它
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.giftry{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifClass.forName(driver);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifConnectionconn=DriverManager.getConnection(dbURL);//启动嵌入式数据库
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifStatementst=conn.createStatement();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifst.execute("createtablefoo(FOOIDINTNOTNULL,FOONAMEVARCHAR(30)NOTNULL)");//创建foo表
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifst.executeUpdate("insertintofoo(FOOID,FOONAME)values(1,'chinajash')");//插入一条数据
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifResultSetrs=st.executeQuery("select*fromfoo");//读取刚插入的数据
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifwhile(rs.next()){
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifintid=rs.getInt(1);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifStringname=rs.getString(2);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.out.println("id="+id+";name="+name);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif}catch(Exceptione){
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gife.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]