- 浏览: 2552590 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
彩票计算JAVA版(三)内置数据库derby
1.配置derby运行环境
JVM里面运行的嵌入式derby,不晓得JRE6后是咋样的。但是目前启动derby来存储本地数据,我是这样处理的
首先导入derby必要的几个包,修改POM文件pom.xml如下:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.4.2.0</version>
</dependency>
还是按照dbcp老的方式配置ibatis、spring和数据库的配置,配置信息如下:
#==========================================
# database configration
#==========================================
database.driver=org.apache.derby.jdbc.EmbeddedDriver
database.url=jdbc:derby:easyluckDB;create=true
database.username=easyluck
database.pwd=easyluck
库的名字叫easyluckDB,用户名和密码都是easyluck
2.利用ibatis的工具类初始化数据库
利用ScriptRunner 初始化sql脚本,核心类DatabaseCreation.java如下:
package com.sillycat.easyluck.database;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import com.ibatis.common.jdbc.ScriptRunner;
import com.sillycat.easyluck.common.utils.SpringContextUtil;
public class DatabaseCreation {
public void init() {
this.createTables();
this.createData();
}
public void createTables() {
String sourceUrl = "sql/create_table.sql";
runScript(sourceUrl);
}
public void createData() {
String sourceUrl = "sql/create_data.sql";
runScript(sourceUrl);
}
public void runScript(String sourceUrl) {
ApplicationContext context = SpringContextUtil.create();
DataSource ds = (DataSource) context.getBean("dataSource");
ScriptRunner runner = null;
try {
runner = new ScriptRunner(ds.getConnection(), true, true);
} catch (SQLException e) {
e.printStackTrace();
}
String file = DatabaseCreation.class.getResource(sourceUrl).getFile();
Reader reader = null;
try {
reader = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
runner.runScript(reader);
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DatabaseCreation dsCreation = new DatabaseCreation();
dsCreation.init();
}
}
SQL文件放置在这个类的sql目录下,文件内容参考如下
建表sql脚本create_table.sql:
-- connect 'jdbc:derby://localhost:1527/easyluckDB;create=true;user=easyluck;password=easyluck';
drop table BALLS;
drop table LUCKS;
drop table R_LUCK_BALL;
create table BALLS (
ID integer primary key GENERATED ALWAYS AS IDENTITY,
NUM integer not null,
COLOR varchar(30)
);
create table LUCKS (
ID integer primary key GENERATED ALWAYS AS IDENTITY,
CODE varchar(30)
);
create table R_LUCK_BALL (
LUCK_ID integer,
BALL_ID integer
);
初始化数据脚本create_data.sql:
-- red balls
insert into BALLS (NUM,COLOR) values(1,'red');
insert into BALLS (NUM,COLOR) values(2,'red');
insert into BALLS (NUM,COLOR) values(3,'red');
insert into BALLS (NUM,COLOR) values(4,'red');
insert into BALLS (NUM,COLOR) values(5,'red');
insert into BALLS (NUM,COLOR) values(6,'red');
insert into BALLS (NUM,COLOR) values(7,'red');
insert into BALLS (NUM,COLOR) values(8,'red');
insert into BALLS (NUM,COLOR) values(9,'red');
insert into BALLS (NUM,COLOR) values(10,'red');
insert into BALLS (NUM,COLOR) values(11,'red');
insert into BALLS (NUM,COLOR) values(12,'red');
insert into BALLS (NUM,COLOR) values(13,'red');
insert into BALLS (NUM,COLOR) values(14,'red');
insert into BALLS (NUM,COLOR) values(15,'red');
insert into BALLS (NUM,COLOR) values(16,'red');
insert into BALLS (NUM,COLOR) values(17,'red');
insert into BALLS (NUM,COLOR) values(18,'red');
insert into BALLS (NUM,COLOR) values(19,'red');
insert into BALLS (NUM,COLOR) values(20,'red');
insert into BALLS (NUM,COLOR) values(21,'red');
insert into BALLS (NUM,COLOR) values(22,'red');
insert into BALLS (NUM,COLOR) values(23,'red');
insert into BALLS (NUM,COLOR) values(24,'red');
insert into BALLS (NUM,COLOR) values(25,'red');
insert into BALLS (NUM,COLOR) values(26,'red');
insert into BALLS (NUM,COLOR) values(27,'red');
insert into BALLS (NUM,COLOR) values(28,'red');
insert into BALLS (NUM,COLOR) values(29,'red');
insert into BALLS (NUM,COLOR) values(30,'red');
insert into BALLS (NUM,COLOR) values(31,'red');
insert into BALLS (NUM,COLOR) values(32,'red');
insert into BALLS (NUM,COLOR) values(33,'red');
-- blue balls
insert into BALLS (NUM,COLOR) values(1,'blue');
insert into BALLS (NUM,COLOR) values(2,'blue');
insert into BALLS (NUM,COLOR) values(3,'blue');
insert into BALLS (NUM,COLOR) values(4,'blue');
insert into BALLS (NUM,COLOR) values(5,'blue');
insert into BALLS (NUM,COLOR) values(6,'blue');
insert into BALLS (NUM,COLOR) values(7,'blue');
insert into BALLS (NUM,COLOR) values(8,'blue');
insert into BALLS (NUM,COLOR) values(9,'blue');
insert into BALLS (NUM,COLOR) values(10,'blue');
insert into BALLS (NUM,COLOR) values(11,'blue');
insert into BALLS (NUM,COLOR) values(12,'blue');
insert into BALLS (NUM,COLOR) values(13,'blue');
insert into BALLS (NUM,COLOR) values(14,'blue');
insert into BALLS (NUM,COLOR) values(15,'blue');
insert into BALLS (NUM,COLOR) values(16,'blue');
3.spring+ibatis连接derby撰写DAO层代码:
其实和平时写ibatis是一样的,只是在处理ID的时候和平时用的ORACLE等在XML有些区别,核心区别如下:
<insert id="insert" parameterClass="luck">
insert into
LUCKS (
CODE
) values (
#code:VARCHAR#
)
<selectKey keyProperty="id" resultClass="int">
VALUES IDENTITY_VAL_LOCAL()
</selectKey>
</insert>
另外,在网上查到的各种数据库的ID处理方式
Cloudscape VALUES IDENTITY_VAL_LOCAL()
DB2 VALUES IDENTITY_VAL_LOCAL()
Derby VALUES IDENTITY_VAL_LOCAL()
HSQLDB CALL IDENTITY()
MySql SELECT LAST_INSERT_ID()
SqlServer SELECT SCOPE_IDENTITY()
SYBASE SELECT @@IDENTITY
ORACLE SELECT CUSTOM_SQL.NEXTVAL AS ID FROM DUAL
1.配置derby运行环境
JVM里面运行的嵌入式derby,不晓得JRE6后是咋样的。但是目前启动derby来存储本地数据,我是这样处理的
首先导入derby必要的几个包,修改POM文件pom.xml如下:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.4.2.0</version>
</dependency>
还是按照dbcp老的方式配置ibatis、spring和数据库的配置,配置信息如下:
#==========================================
# database configration
#==========================================
database.driver=org.apache.derby.jdbc.EmbeddedDriver
database.url=jdbc:derby:easyluckDB;create=true
database.username=easyluck
database.pwd=easyluck
库的名字叫easyluckDB,用户名和密码都是easyluck
2.利用ibatis的工具类初始化数据库
利用ScriptRunner 初始化sql脚本,核心类DatabaseCreation.java如下:
package com.sillycat.easyluck.database;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import com.ibatis.common.jdbc.ScriptRunner;
import com.sillycat.easyluck.common.utils.SpringContextUtil;
public class DatabaseCreation {
public void init() {
this.createTables();
this.createData();
}
public void createTables() {
String sourceUrl = "sql/create_table.sql";
runScript(sourceUrl);
}
public void createData() {
String sourceUrl = "sql/create_data.sql";
runScript(sourceUrl);
}
public void runScript(String sourceUrl) {
ApplicationContext context = SpringContextUtil.create();
DataSource ds = (DataSource) context.getBean("dataSource");
ScriptRunner runner = null;
try {
runner = new ScriptRunner(ds.getConnection(), true, true);
} catch (SQLException e) {
e.printStackTrace();
}
String file = DatabaseCreation.class.getResource(sourceUrl).getFile();
Reader reader = null;
try {
reader = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
runner.runScript(reader);
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DatabaseCreation dsCreation = new DatabaseCreation();
dsCreation.init();
}
}
SQL文件放置在这个类的sql目录下,文件内容参考如下
建表sql脚本create_table.sql:
-- connect 'jdbc:derby://localhost:1527/easyluckDB;create=true;user=easyluck;password=easyluck';
drop table BALLS;
drop table LUCKS;
drop table R_LUCK_BALL;
create table BALLS (
ID integer primary key GENERATED ALWAYS AS IDENTITY,
NUM integer not null,
COLOR varchar(30)
);
create table LUCKS (
ID integer primary key GENERATED ALWAYS AS IDENTITY,
CODE varchar(30)
);
create table R_LUCK_BALL (
LUCK_ID integer,
BALL_ID integer
);
初始化数据脚本create_data.sql:
-- red balls
insert into BALLS (NUM,COLOR) values(1,'red');
insert into BALLS (NUM,COLOR) values(2,'red');
insert into BALLS (NUM,COLOR) values(3,'red');
insert into BALLS (NUM,COLOR) values(4,'red');
insert into BALLS (NUM,COLOR) values(5,'red');
insert into BALLS (NUM,COLOR) values(6,'red');
insert into BALLS (NUM,COLOR) values(7,'red');
insert into BALLS (NUM,COLOR) values(8,'red');
insert into BALLS (NUM,COLOR) values(9,'red');
insert into BALLS (NUM,COLOR) values(10,'red');
insert into BALLS (NUM,COLOR) values(11,'red');
insert into BALLS (NUM,COLOR) values(12,'red');
insert into BALLS (NUM,COLOR) values(13,'red');
insert into BALLS (NUM,COLOR) values(14,'red');
insert into BALLS (NUM,COLOR) values(15,'red');
insert into BALLS (NUM,COLOR) values(16,'red');
insert into BALLS (NUM,COLOR) values(17,'red');
insert into BALLS (NUM,COLOR) values(18,'red');
insert into BALLS (NUM,COLOR) values(19,'red');
insert into BALLS (NUM,COLOR) values(20,'red');
insert into BALLS (NUM,COLOR) values(21,'red');
insert into BALLS (NUM,COLOR) values(22,'red');
insert into BALLS (NUM,COLOR) values(23,'red');
insert into BALLS (NUM,COLOR) values(24,'red');
insert into BALLS (NUM,COLOR) values(25,'red');
insert into BALLS (NUM,COLOR) values(26,'red');
insert into BALLS (NUM,COLOR) values(27,'red');
insert into BALLS (NUM,COLOR) values(28,'red');
insert into BALLS (NUM,COLOR) values(29,'red');
insert into BALLS (NUM,COLOR) values(30,'red');
insert into BALLS (NUM,COLOR) values(31,'red');
insert into BALLS (NUM,COLOR) values(32,'red');
insert into BALLS (NUM,COLOR) values(33,'red');
-- blue balls
insert into BALLS (NUM,COLOR) values(1,'blue');
insert into BALLS (NUM,COLOR) values(2,'blue');
insert into BALLS (NUM,COLOR) values(3,'blue');
insert into BALLS (NUM,COLOR) values(4,'blue');
insert into BALLS (NUM,COLOR) values(5,'blue');
insert into BALLS (NUM,COLOR) values(6,'blue');
insert into BALLS (NUM,COLOR) values(7,'blue');
insert into BALLS (NUM,COLOR) values(8,'blue');
insert into BALLS (NUM,COLOR) values(9,'blue');
insert into BALLS (NUM,COLOR) values(10,'blue');
insert into BALLS (NUM,COLOR) values(11,'blue');
insert into BALLS (NUM,COLOR) values(12,'blue');
insert into BALLS (NUM,COLOR) values(13,'blue');
insert into BALLS (NUM,COLOR) values(14,'blue');
insert into BALLS (NUM,COLOR) values(15,'blue');
insert into BALLS (NUM,COLOR) values(16,'blue');
3.spring+ibatis连接derby撰写DAO层代码:
其实和平时写ibatis是一样的,只是在处理ID的时候和平时用的ORACLE等在XML有些区别,核心区别如下:
<insert id="insert" parameterClass="luck">
insert into
LUCKS (
CODE
) values (
#code:VARCHAR#
)
<selectKey keyProperty="id" resultClass="int">
VALUES IDENTITY_VAL_LOCAL()
</selectKey>
</insert>
另外,在网上查到的各种数据库的ID处理方式
Cloudscape VALUES IDENTITY_VAL_LOCAL()
DB2 VALUES IDENTITY_VAL_LOCAL()
Derby VALUES IDENTITY_VAL_LOCAL()
HSQLDB CALL IDENTITY()
MySql SELECT LAST_INSERT_ID()
SqlServer SELECT SCOPE_IDENTITY()
SYBASE SELECT @@IDENTITY
ORACLE SELECT CUSTOM_SQL.NEXTVAL AS ID FROM DUAL
发表评论
-
Stop Update Here
2020-04-28 09:00 316I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 369Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 370Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 431Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 374Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 456VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 479NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 424Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 248GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 452GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 319Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ...
相关推荐
Derby是一款开源的关系型数据库管理系统(RDBMS),特别适用于Java应用程序。它完全用Java语言编写,并且可以作为嵌入式数据库运行,也可以作为客户端/服务器模式运行。Derby支持标准的SQL语法、事务管理以及ACID...
Apache Derby是一个完全开放源代码的Java数据库,完全用Java编写,它符合标准的SQL和JDBC接口。它在Java平台上被设计为嵌入式数据库。由于其轻量级特性,它特别适合小型应用程序。Derby的SQL使用规范非常详细,涵盖...
### Derby开发指南知识点详解 ...通过以上知识点的详细介绍,可以看出Derby官方开发指南覆盖了数据库开发所需的基础和高级特性,对于希望深入了解和使用Derby数据库的开发者来说,是一份宝贵的资源。
《Derby 10.4 参考手册》是一份详尽的技术文档,主要针对Apache Derby数据库管理系统,提供了深入的SQL语言使用指南、语法规范以及系统功能介绍。这份手册不仅适用于初学者,也适合有经验的数据库管理员和开发人员,...
H2数据库是一个开源的关系型数据库管理系统,它以Java语言编写,可以在多种平台下运行,既支持嵌入式模式也支持网络版的数据库服务器模式。H2数据库的设计强调了轻量级、高性能以及与Java的紧密集成。它的主要特征...
"db"目录包含了Java数据库(Java DB或Derby),这是一个轻量级的关系型数据库管理系统,主要用于教学和小型应用。尽管不是所有Java开发者都会使用这个数据库,但它是JDK的一部分,提供了内置的数据库支持。 ...
- **db**:包含 Derby 数据库,这是一个轻量级的关系型数据库,常用于开发和测试。 - **include**:包含头文件,供C/C++开发者调用Java Native Interface (JNI) 编写本地方法。 - **lib**:存放JDK的库文件,包括...
在描述中提到,DBeaver支持多种数据库,包括但不限于MySQL、PostgreSQL、Oracle、DB2、MSSQL(Microsoft SQL Server)、Sybase、Mimer、HSQLDB和Derby。这显示了DBeaver的广泛兼容性,使得开发者和数据库管理员可以...
- **db**:可能包含Java DB(以前称为Derby),这是一个轻量级的关系数据库管理系统,用于开发和测试Java应用程序。 以上是对JDK 1.8 64位及其主要特性的详细阐述,这些内容对于理解和使用Java 8开发环境至关重要。
元数据库可以使用内置的Derby数据库,也可以配置成使用更强大的MySQL或PostgreSQL。 5. **Hive的数据存储**:Hive将数据存储在HDFS上,采用分桶和分区策略来提高查询效率。数据文件格式多样,如TextFile、...
- **DERBY**:Hive默认的元数据库是内嵌的Apache Derby,轻量级且易于设置,适用于测试环境。 - **Mysql**:在生产环境中,为了高可用性和性能,通常会将元数据存储在外部的MySQL数据库中。 ### 1.5 HIVE的数据存储...
默认情况下,Hive使用内置的Derby数据库作为元数据库,但为了提高性能和并发性,通常会配置使用MySQL或其他更强大的数据库系统。 5. **Hive的数据存储** Hive将数据存储在HDFS上,数据文件格式可选如TextFile、...
如果使用内置的Derby数据库,可以跳过数据库配置;若使用MySQL或PostgreSQL,则需额外配置数据库连接。 最后,启动Hive服务: ```bash hive ``` 至此,你已经在虚拟机中成功搭建了一个Hadoop集群,并配置了Hive...
3. **元数据**:Hive维护着关于数据的结构信息,如表名、列名、分区等,这些信息存储在元数据存储中,可以是内置的Derby数据库或外部的MySQL、PostgreSQL等。 4. **延迟计算**:Hive执行查询时并不会立即进行计算,...
由于文件内容未详细说明安装和配置的步骤,一般而言,Hive的安装通常包括下载Hive软件包并解压,然后配置环境变量,设置Hive的配置文件,如hive-site.xml,定义元数据存储(通常为Derby或MySQL数据库),以及确保...
Hive中的元数据库可以使用MySQL或Derby等关系数据库来存储,这些数据库是Hive用于存储和管理元数据的地方。元数据包含了Hive中的表结构、数据类型、分区信息、索引信息等。 Hive的数据存储是基于HDFS的,HiveQL的...