derby数据库简易使用说明
//con =
DriverManager.getConnection("jdbc:derby:userDB;create=true;user=test;password=test");
//userDB创建在class运行目录
//con
=
DriverManager.getConnection("jdbc:derby:a/b/userDB;create=true;user=test;password=test");
//userDB创建在class下面的a/b文件夹下面,
con = DriverManager.getConnection("jdbc:derby:d:/a/b/userDB;create=true;user=test;password=test"); //userDB创建在d盘下面的a/b文件夹下面,
package test;
import java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.ResultSet;
import
java.sql.Statement;
public
class EmbeddedDerbyTester {
public static void
main(String[] args) {
String driver = "org.apache.derby.jdbc.EmbeddedDriver";//在derby.jar里面
String dbName="EmbeddedDB";
String dbURL = "jdbc:derby:"+dbName+";create=true";//create=true表示当数据库不存在时就创建它
try {
Class.forName(driver);
Connection conn =
DriverManager.getConnection(dbURL);//启动嵌入式数据库
Statement st =
conn.createStatement();
st.execute("create table foo (FOOID INT NOT NULL,FOONAME
VARCHAR(30) NOT NULL)");//创建foo表
st.executeUpdate("insert into foo(FOOID,FOONAME) values
(1,‘chinajash‘)");//插入一条数据
ResultSet rs =
st.executeQuery("select * from foo");//读取刚插入的数据
while(rs.next()){
int id
= rs.getInt(1);
String name = rs.getString(2);
System.out.println("id="+id+";name="+name);
}
} catch(Exception e){
e.printStackTrace();
}
}
}
package test;
import java.io.PrintWriter;
import
java.sql.DriverManager;
import
org.apache.derby.drda.NetworkServerControl;
public class
NetworkServerDerbyTester {
public static
void main(String[] args) {
String driver = "org.apache.derby.jdbc.ClientDriver";//在derbyclient.jar里面
String dbName="NetworkDB";
String
connectionURL = "jdbc:derby://localhost:1527/" + dbName + ";create=true";
try {
/*
创建Derby网络服务器,默认端口是1527,也可以通过运行
<Derby_Home>/frameworks/NetworkServer/bin/startNetworkServer.bat
来创建并启动Derby网络服务器,如果是Unix,用startNetworkServer.ksh
*/
NetworkServerControl derbyServer = new NetworkServerControl();//NetworkServerControl类在derbynet.jar里面
PrintWriter pw =
new PrintWriter(System.out);//用系统输出作为Derby数据库的输出
derbyServer.start(pw);//启动Derby服务器
Class.forName(driver);
DriverManager.getConnection(connectionURL);
//do something
derbyServer.shutdown();//关闭Derby服务器
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。