其实,JAVA连接Derby数据库也很简单,和一般的数据库操作一样:
1、加载驱动
2、获取连接
3、进行数据库操作
4、记得关闭连接
示例如下:
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TestDerbyServer
{
public static void main(String[] args)
{
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");//加载驱动
Connection conn = DriverManager.getConnection("jdbc:derby:derbyDB;create=true","user","pwd");
Statement st = conn.createStatement();
ResultSet rs=st.executeQuery(" select count(*) from SYS.SYSTABLES where tablename='DBINFO' ");
//注意derby数据库中的表明不存在小写,所以如果此处查询结果为小写那就查不到
int k=0;
while(rs.next())
{
if("0".equals(rs.getObject(1).toString()))
k=-1;
}
if(k==-1)
{
st.execute("create table DBINFO(xmname varchar(50),dbip varchar(50))");
System.out.println("创建新表");
k=0;
}
rs=st.executeQuery(" select max(xmname) from DBINFO ");
while(rs.next())
{
if(null!=rs.getObject(1))
k=Integer.parseInt(String.valueOf(rs.getObject(1)));
}
st.execute("insert into DBINFO values('"+(k+1)+"','sinboy')");
st.execute("insert into DBINFO values('"+(k+2)+"','Tom')");
rs=st.executeQuery(" select * from DBINFO ");
while(rs.next())
{
System.out.println(rs.getObject(1)+"????"+rs.getObject(2));
}
// st.execute("drop table DBINFO");
rs.close();
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(TestDerbyServer