java 本地数据库的javaDB

java的自带数据库,觉得挺方便的就实验了下  

package com.gemantic.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class JavaDBTest {

public static void main(String[] args) throws Exception {
String driver
= "org.apache.derby.jdbc.EmbeddedDriver";
String url
= "jdbc:derby:";
String facetDir
= "/data/javaDB/";
Class.forName(driver).newInstance();
Connection connection
= DriverManager.getConnection(url + facetDir+ ";create=true");
connection.setAutoCommit(
true);
Statement stmt
= connection.createStatement();
String creatSql
="CREATE TABLE testDB (" + "facetId INTEGER PRIMARY KEY," + " name VARCHAR(256))";
stmt.executeUpdate(creatSql);
stmt.executeUpdate(
"INSERT INTO testDB(facetId, name) " + "VALUES(1, '123')");
stmt.executeUpdate(
"INSERT INTO testDB(facetId, name) " + "VALUES(2, '123')");
stmt.executeUpdate(
"INSERT INTO testDB(facetId, name) " + "VALUES(3, '123')");
}
}

这样就会在相应的位置建立一个数据库,create=true ,若数据库已经存在的话打开原来的数据库,不然的话重新创建一个数据库 。

posted @ 2011-03-31 15:06  xiao晓  阅读(3137)  评论(0编辑  收藏  举报