`
269629151
  • 浏览: 120017 次
社区版块
存档分类
最新评论

derby数据库嵌入程序模式

阅读更多

导入相应jar包,我是直接在derby官网下载压缩包后解压后把 lib文件夹下的jar包全导入到项目中了,然后就可以在项目中直接使用

 

 

 

package db;
/*

Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

import javax.sql.*;
import java.sql.*;
import org.apache.derby.jdbc.EmbeddedDataSource;

/**
* A container for the singleton data source, so we don't have to
* create a separate one for each class that wants to do JDBC
*/
public class DatabaseManager {

private static EmbeddedDataSource ds;

public static String REQUESTS_TABLE = "APP.REQUESTS";
public static String EVENTS_TABLE = "APP.EVENTS";

// We want to keep the same connection for a given thread
// as long as we're in the same transaction
private static ThreadLocal<Connection> tranConnection = new ThreadLocal();

private static void initDataSource(String dbname, String user,
String password) {
ds = new EmbeddedDataSource();
ds.setDatabaseName(dbname);
ds.setUser(user);
ds.setPassword(password);
ds.setCreateDatabase("create");
}

public static void logSql() throws Exception {
executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
"'derby.language.logStatementText', 'true')");
}

public static synchronized void beginTransaction() throws Exception {
if ( tranConnection.get() != null ) {
throw new Exception("This thread is already in a transaction");
}
Connection conn = getConnection();
conn.setAutoCommit(false);
tranConnection.set(conn);
}

public static void commitTransaction() throws Exception {
if ( tranConnection.get() == null ) {
throw new Exception("Can't commit: this thread isn't currently in a " +
"transaction");
}
tranConnection.get().commit();
tranConnection.set(null);
}

public static void rollbackTransaction() throws Exception {
if ( tranConnection.get() == null ) {
throw new Exception("Can't rollback: this thread isn't currently in a " +
"transaction");
}
tranConnection.get().rollback();
tranConnection.set(null);
}

/** get a connection */
public static Connection getConnection() throws Exception {
if ( tranConnection.get() != null ) {
return tranConnection.get();
} else {
return ds.getConnection();
}
}

public static void releaseConnection(Connection conn) throws Exception {
// We don't close the connection while we're in a transaction,
// as it needs to be used by others in the same transaction context
if ( tranConnection.get() == null ) {
conn.close();
}
}

public static void initDatabase(String dbname, String user, String password,
boolean dropTables)
throws Exception {
initDataSource(dbname, user, password);

if ( dropTables ) {
dropTables();
}

// Assumption: if the requests table doesn't exist, none of the
// tables exists. Avoids multiple queries to the database
if ( ! tableExists("REQUESTS") ) {
createTables();
}
}

private static boolean tableExists(String tablename) throws Exception {
Connection conn = getConnection();
ResultSet rs;
boolean exists;

try {
DatabaseMetaData md = conn.getMetaData();

rs = md.getTables(null, "APP", tablename, null);
exists = rs.next();
} finally {
releaseConnection(conn);
}

return exists;
}

private static void createTables() throws Exception {
System.out.println("Creating tables");

executeUpdate(
"CREATE TABLE " + REQUESTS_TABLE + "(" +
"sequence_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, " +
"request_type INTEGER, " +
"event_id VARCHAR(300), " +
"date VARCHAR(20), " +
"title VARCHAR(300), " +
"edit_url VARCHAR(300))");

executeUpdate(
"CREATE TABLE " + EVENTS_TABLE + "(" +
"event_id VARCHAR(300) PRIMARY KEY, " +
"date VARCHAR(20), " +
"title VARCHAR(300), " +
"edit_url VARCHAR(300), " +
"version_id VARCHAR(300))");
}

/**
* Drop the tables. Used mostly for unit testing, to get back
* to a clean state
*/
public static void dropTables() throws Exception {
try {
executeUpdate("DROP TABLE " + REQUESTS_TABLE);
} catch ( SQLException sqle ) {
if (! tableDoesntExist(sqle.getSQLState())) {
throw sqle;
}
}

try {
executeUpdate("DROP TABLE " + EVENTS_TABLE);
} catch ( SQLException sqle ) {
if (! tableDoesntExist(sqle.getSQLState())) {
throw sqle;
}
}
}

private static boolean tableDoesntExist(String sqlState) {
return sqlState.equals("42X05") ||
sqlState.equals("42Y55");
}

/**
* Clean out the tables
*/
public static void clearTables() throws Exception {
Connection conn = getConnection();

try {
executeUpdate("DELETE FROM " + REQUESTS_TABLE);
executeUpdate("DELETE FROM " + EVENTS_TABLE);
} finally {
releaseConnection(conn);
}

}

/**
* Helper wrapper around boilerplate JDBC code. Execute a statement
* that doesn't return results using a PreparedStatment, and returns
* the number of rows affected
*/
public static int executeUpdate(String statement)
throws Exception {
Connection conn = getConnection();
try {
PreparedStatement ps = conn.prepareStatement(statement);
return ps.executeUpdate();
} finally {
releaseConnection(conn);
}
}

/**
* Helper wrapper around boilerplat JDBC code. Execute a statement
* that returns results using a PreparedStatement that takes no
* parameters (you're on your own if you're binding parameters).
*
* @return the results from the query
*/
public static ResultSet executeQueryNoParams(Connection conn,
String statement) throws Exception {
PreparedStatement ps = conn.prepareStatement(statement);
return ps.executeQuery();
}

public static void main(String[] args) throws Exception{

initDataSource("test1","app","app");
logSql();
Connection conn = getConnection() ;
PreparedStatement ps = conn.prepareStatement("select * from T ") ;
ResultSet st = ps.executeQuery() ;
while(st.next()){
System.out.println(st.getString(1));
}
conn.close() ;
}
}

 

 

分享到:
评论

相关推荐

    derby.zip,Derby数据库

    Derby数据库完整压缩包,解压缩即可使用,bat文件在bin文件目录下,驱动程序在lib目录下。 Apache Derby非常小巧,核心部分derby.jar只有2M,所以既可以做为单独的数据库服务器使用,也可以内嵌在应用程序中使用。...

    derby数据库以及在MyEclipse中的配置

    上网页也可以找到一些资源关于derby数据库的安装配置,但是你看起来会很麻烦,而且比较难以上手。 这是我自己动手实践过的,里面有很多截图方便你理解安装过程,已经电脑上面环境变量的配置,在MyEclipse上面的...

    derby 数据库 eclipse插件

    derby 数据库 eclipse插件 derby 数据库 eclipse插件

    连接derby数据库方法—附图

    连接derby数据库方法—附图

    derby嵌入式数据库连接问题

    derby嵌入式数据库连接问题

    Derby数据库初级使用文档

    Derby数据库初级使用文档,包括Derby数据库的安装、部署、使用等详细步骤,适合刚接触Derby人员使用。

    derby数据库的简单操作

    文档中简单列出了对derby数据库的几个简单的操作,包括连接derby、创建数据库、连接数据库、查询数据库等

    JavaSE6.0的Derby嵌入式数据库

    derby的入门资料 给大家共享过来 很值得下载

    derby数据库免安装jvm内置数据库

    Apache Derby非常小巧,核心部分derby.jar只有2M,所以既可以做为单独的数据库服务器使用,也可以内嵌在应用程序中使用。Cognos 8 BI的Content Store默认就是使用的Derby数据库,可以在Cognos8的安装目录下看到一个...

    derby 数据库 使用的例子

    derby 数据库 使用的例子 derby 数据库 使用的例子

    derby数据库

    详细描述了derby的使用,Derby数据库是一个纯用Java实现的内存数据库,属于Apache的一个开源项目。由于是用Java实现的,所以可以在任何平台上运行;另外一个特点是体积小,免安装,只需要几个小jar包就可以运行了。

    Derby数据库(V10.9)用户手册(PDF版)

    01. Getting Started with Derby - 10.9.pdf 02. Derby Reference Manual - 10.9.pdf 03. Derby Developer's Guide - 10.9.pdf 04. Tuning Derby - 10.9.pdf 05. Derby Server and Administration Guide - 10.9.pdf ...

    Derby数据库(V10.7)用户手册(PDF版)

    01. Getting Started with Derby - 10.7.pdf 02. Derby Reference Manual - 10.7.pdf 03. Derby Developer's Guide - 10.7.pdf 04. Tuning Derby - 10.7.pdf 05. Derby Server and Administration Guide - 10.7.pdf ...

    Derby数据库(V10.1)用户手册(PDF版)

    01. Getting Started with Derby - 10.1.pdf 02. Derby Reference Manual - 10.1.pdf 03. Derby Developer's Guide - 10.1.pdf 04. Tuning Derby - 10.1.pdf 05. Derby Server and Administration Guide - 10.1.pdf ...

    商品出入库例子+derby数据库

    简单的商品出入库操作的例子,还有打印的小功能,应用了derby数据库,这样可以将数据库也打包成一个exe文件,可以不用客户端安装数据库了。

    Derby数据库(V10.5)用户手册(PDF版)

    01. Getting Started with Derby - 10.5.pdf 02. Derby Reference Manual - 10.5.pdf 03. Derby Developer's Guide - 10.5.pdf 04. Tuning Derby - 10.5.pdf 05. Derby Server and Administration Guide - 10.5.pdf ...

    Derby安装,创建数据库,在JAVA程序中使用Derby

    真正的Derby新手教程,Derby安装,创建数据库,在Java程序中使用Derby 本人原创

    Derby数据库ij工具的使用

    Apache Derby项目的目标是构建一个完全用 Java 编程语言编写的、易于使用却适合大多数应用程序的开放源码数据库。Derby 数据库符合许多数据库标准,例如 SQL-92 和 JDBC 3.0 版本,所以开始用 Derby 数据库系统开发...

    内嵌数据库derby 10.6.2开发文档

    内嵌数据库derby 10.6.2开发文档

    derby数据库驱动包

    derby数据库驱动包,java编程需要的包

Global site tag (gtag.js) - Google Analytics