首先是Java环境变量要配置:
1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH
变量名:JAVA_HOME
变量值:C:\Program Files\Java\jdk1.7.0
变量名:CLASSPATH
变量值:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; 3. 选择“系统变量”中变量名为“Path”的环境变量,双击该变量,把JDK安装路径中bin目录的绝对路径,添加到Path变量的值中,并使用半角的分号和已有的路径进行分隔。
变量名:Path
变量值:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
这是java的环境配置,配置完成后直接启动eclipse,它会自动完成java环境的配置
1:官方下载地址http://db.apache.org/derby/derby_downloads.html
2:配置环境变量
1.打开我的电脑--属性--高级--环境变量
Name:DERBY_HOME
Value:E:\Java\derby\db-derby-10.10.1.1-bin
在Path加入:%DERBY_HOME%\bin
在CLASSPATH加入:%DERBY_HOME%\lib \derby.jar;%DERBY_HOME%\lib\derbyclient.jar;%DERBY_HOME%\lib\derbytools.jar;%DERBY_HOME%\lib\derbynet.jar
2:测试看看Derby是否安装成功
开始—Run—CMD
运行 sysinfo
如果安装成功,将会看到相关信息,下面是截取的部分信息
------------------ Java 信息 ------------------
Java 版本: 1.6.0_10-rc2
Java 供应商: Sun Microsystems Inc.
Java 主目录: C:\Program Files\Java\jdk1.6.0_10\jre
Java 类路径: E:\Java\derby\db-derby-10.10.1.1-bin\lib \derby.jar;E:\Java\d
erby\db-derby-10.10.1.1-bin\lib\derbyclient.jar;E:\Java\derby\db-derby-10.10.1.1
-bin\lib\derbytools.jar;E:\Java\derby\db-derby-10.10.1.1-bin\lib\derbynet.jar;C:
\Program Files\Java\jdk1.6.0_10\lib\dt.jar;C:\Program Files\Java\jdk1.6.0_10\lib
\tools.jar;E:\Java\derby\db-derby-10.10.1.1-bin/lib/derby.jar;E:\Java\derby\db-d
erby-10.10.1.1-bin/lib/derbynet.jar;E:\Java\derby\db-derby-10.10.1.1-bin/lib/der
byclient.jar;E:\Java\derby\db-derby-10.10.1.1-bin/lib/derbytools.jar
OS 名: Windows XP
OS 体系结构: x86
OS 版本: 5.1
Java 用户名: Administrator
Java 用户主目录:C:\Documents and Settings\Administrator
Java 用户目录: E:\Java\derby\db-derby-10.10.1.1-bin\bin
java.specification.name: Java Platform API Specification
java.specification.version: 1.6
java.runtime.version: 1.6.0_10-rc2-b32
--------- Derby 信息 --------
[E:\Java\derby\db-derby-10.10.1.1-bin\lib\derby.jar] 10.10.1.1 - (1458268)
[E:\Java\derby\db-derby-10.10.1.1-bin\lib\derbytools.jar] 10.10.1.1 - (1458268)
[E:\Java\derby\db-derby-10.10.1.1-bin\lib\derbynet.jar] 10.10.1.1 - (1458268)
[E:\Java\derby\db-derby-10.10.1.1-bin\lib\derbyclient.jar] 10.10.1.1 - (1458268)
......
3:创建和使用数据库
数据库的目录默认情况下在,E:\Java\derby\db-derby-10.10.1.1-bin\bin 目录下,(命令输出的位置)
然后输入ij
E:\Java\derby\db-derby-10.10.1.1-bin\bin>ij;
ij 版本 10.1
下面是创建数据库。
例:创建mydb数据库:
ij> connect ‘jdbc:derby:mydb;create=true’;
连接mydb数据库:
ij> connect'jdbc:derby:mytestdb';
查询数据库等操作,只需要输入相应的Sql语句即可。
创建一个table:
ij(CONNECTION1)> create table mytable(id int primary key,name varchar(20));
插入数据:
ij(CONNECTION1)> insert into mytable values(1,'Test');
在命令行测试一下:
ij(CONNECTION1)> select * from mytable;
ID |NAME
--------------------------------
1 |Test
其它命令
断开连接:
ij> disconnect;
退出ij:
ij> exit;
二:在JAVA中使用Derby
NOTE:记得将derby.jar包加进去
package com.db.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
private static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private static String protocol = "jdbc:derby:";
String dbName = "E:\\Java\\derby\\db-derby-10.10.1.1-bin\\bin\\mydb";
static void loadDriver() {
try {
Class.forName(driver).newInstance();
System.out.println("Loaded the appropriate driver");
} catch (Exception e) {
e.printStackTrace();
}
}
public void doIt() {
Connection conn = null;
Statement s = null;
ResultSet rs = null;
System.out.println("starting");
try {
conn = DriverManager.getConnection(protocol + dbName
+ ";create=true");
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("Connected to and created database " + dbName);
try {
s = conn.createStatement();
rs = s.executeQuery("select * from mytable");
while (rs.next()) {
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
}
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
conn.close();
conn = null;
s.close();
s = null;
rs.close();
rs = null;
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Test t = new Test();
t.loadDriver();
t.doIt();
}
}
sourece: http://www.joyzhong.com/archives/558