连接derby时,只是jdbc的连接属性有所改变,大同小异而已。
derby数据库与mysql不同的是,它有两种连接方式:embbed 和 net server
这里只用了第一种embbed的形式,第二种还没有试过。
系统运行环境:winxp+jdk1.4.2+tomcat+cloudscape10.0
以下是具体的网页代码:
<html>
<head><title>derbyconnect.jsp</title></head>
<body>
<%@ page language="java" import="java.sql.*" import="java.util.Properties" %>
<%
String databaseURL ="jdbc:derby:C:\\Database\\jimmyDB"; //C:\\Database\\jimmyDB是数据库的存放位置
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); //derby数据库的embbed驱动名
out.println("Success loading derby Driver!");
}
catch(Exception e)
{
out.print("Error loading derby Driver!");
e.printStackTrace();
}
try{
Properties properties = new Properties();
properties.put("create", "true");//新建数据库
properties.put("user", "APP");//用户名
properties.put("password", "APP"); //密码
properties.put("retreiveMessagesFromServerOnGetMessage", "true");
//Get a connection
Connection connect= DriverManager.getConnection(databaseURL, properties);
out.print("Success connect derby server!");
Statement s = connect.createStatement();
s.execute("create table jimmyDB2(num int, addr varchar(40))");
out.println("Created table jimmyDB2");
s.execute("insert into jimmyDB2 values (1956,''Webster St.'')");
out.println("Inserted 1956 Webster");
s.execute("insert into jimmyDB2 values (1910,''Union St.'')");
out.println("Inserted 1910 Union");
s.execute("update jimmyDB2 set num=180, addr=''Grand Ave.'' where num=1956");
out.println("Updated 1956 Webster to 180 Grand");
s.execute("update jimmyDB2 set num=300, addr=''Lakeshore Ave.'' where num=180");
out.println("Updated 180 Grand to 300 Lakeshore");
ResultSet rs = s.executeQuery("SELECT num, addr FROM jimmyDB2 ORDER BY num");
while(rs.next())
{
out.println(rs.getInt(1));
out.println(rs.getString(2));
}
}
catch(Exception e)
{
out.print(" error!");
e.printStackTrace();
}
%>
</body></html>
文章来源于领测软件测试网 https://www.ltesting.net/