以前写过一个jsp页面中连接mysql的例子
连接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>