java操作derby_javaDB—— derby简单操作
Derby并不是一个新的数据库产品,它是由IBM捐献给Apache的DB项目的一个纯Java数据库,JDK6.0里面带的这个Derby的版本是10.2.1.7,支持存储过程和触发器;有两种运行模式,一种是作为嵌入式数据库,另一种是作为网络数据库,前者的数据库服务器和客户端都在同一个JVM里面运行,后者允许数据库服务器端和客户端不在同一个JVM里面,而且允许这两者在不同的物理机器上.值得注意的是JDK6里面的这个Derby支持JDK6的新特性JDBC 4.0规范(JSR 221),现在我们如果要练习JDBC的用法,没有必要单独装一个数据库产品了,直接用Derby就行.下面是个使用derby的简单例子:首先启动数据库。创建一个简单的数据库
Java代码
private Connection getConnection() throws SQLException {
Connection connection = DriverManager
.getConnection("jdbc:derby:userDB;create=true;user=test;password=test");
connection.setAutoCommit(false);
return connection;
}
private Connection getConnection() throws SQLException {
Connection connection = DriverManager
.getConnection("jdbc:derby:userDB;create=true;user=test;password=test");
connection.setAutoCommit(false);
return connection;
}
其中userDB是要连接数据库的名字,create=true表示如果该数据库不存在,则创建该数据库,如果数据库存在,则用用户user=test;密码password=test连接数据库.有了数据库,接下来该建表了:
Java代码
private void createTable(Connection connection) throws SQLException {
Statement statement = connection.createStatement();
String sql = "create table USERS("
+ " ID BIGINT not null generated by default as identity,"
+ " USER_NAME VARCHAR(20) not null,"
+ " PASSWORD VARCHAR(20),"
+ " constraint P_KEY_1 primary key (ID))";
statement.execute(sql);
sql = "create unique index USER_NAME_INDEX on USERS ("
+ " USER_NAME ASC)";
statement.execute(sql);
statement.close();
}
private void createTable(Connection connection) throws SQLException {
Statement statement = connection.createStatement();
String sql = "create table USERS("
+ "IDBIGINTnot null generated by default as identity,"
+ "USER_NAMEVARCHAR(20)not null,"
+ "PASSWORDVARCHAR(20),"
+ "constraint P_KEY_1 primary key (ID))";
statement.execute(sql);
sql = "create unique index USER_NAME_INDEX on USERS ("
+ "USER_NAMEASC)";
statement.execute(sql);
statement.close();
}
创建了USERS表,包括ID,USER_NAME,PASSWORD三个列,其中ID是主键,其中generated by default as identity的作用类似sequence,identity是定义自动加一的列,
GENERATED BY ALWAYS AS IDENTITY
GENERATED BY DEFAULT AS IDENTITY
By always和by default是说明生成这个IDENTITY的方式。By always是完全由系统自动生成。by default是可以由用户来指定一个值。编写与USERS表对应的javabean(这个就不多说了),:
Java代码
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private String userName;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private String userName;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
接下来就可以就数据库进行增删改查的操作了:插入数据:
Java代码
private void create(User user) {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("insert into users (user_name,password) values(?,?)");
int index = 1;
statement.setString(index++, user.getUserName());
statement.setString(index++, user.getPassword());
statement.execute();
user.setId(this.getId(connection));
connection.commit();
} catch (SQLException e) {
rollback(connection);
throw new RuntimeException(e);
} finally {
if (connection != null) {
close(connection);
}
}
}
private void create(User user) {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("insert into users (user_name,password) values(?,?)");
int index = 1;
statement.setString(index++, user.getUserName());
statement.setString(index++, user.getPassword());
statement.execute();
user.setId(this.getId(connection));
connection.commit();
} catch (SQLException e) {
rollback(connection);
throw new RuntimeException(e);
} finally {
if (connection != null) {
close(connection);
}
}
}
Java代码
private Long getId(Connection connection) throws SQLException {
CallableStatement callableStatement = connection
.prepareCall("values identity_val_local()");
ResultSet resultSet = callableStatement.executeQuery();
resultSet.next();
Long id = resultSet.getLong(1);
resultSet.close();
callableStatement.close();
return id;
}
private Long getId(Connection connection) throws SQLException {
CallableStatement callableStatement = connection
.prepareCall("values identity_val_local()");
ResultSet resultSet = callableStatement.executeQuery();
resultSet.next();
Long id = resultSet.getLong(1);
resultSet.close();
callableStatement.close();
return id;
}
getId方法是获得系统默认的id值,是通过identity_val_local()获得的,而函数IDENTITY_VAL_LOCAL()则可以在INSERT语句执行之后,为我们返回刚才系统为id所产生的值.感觉还是有点想sequence的curr_val.修改数据:
Java代码
private void update(User user) {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("update users set user_name=?,password=? where id=?");
int index = 1;
statement.setString(index++, user.getUserName());
statement.setString(index++, user.getPassword());
statement.setLong(index++, user.getId());
statement.execute();
connection.commit();
} catch (SQLException e) {
rollback(connection);
throw new RuntimeException(e);
} finally {
if (connection != null) {
close(connection);
}
}
}
private void update(User user) {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("update users set user_name=?,password=? where id=?");
int index = 1;
statement.setString(index++, user.getUserName());
statement.setString(index++, user.getPassword());
statement.setLong(index++, user.getId());
statement.execute();
connection.commit();
} catch (SQLException e) {
rollback(connection);
throw new RuntimeException(e);
} finally {
if (connection != null) {
close(connection);
}
}
}
删除数据:
Java代码
public void delete(Long id) {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("delete from users where id=?");
statement.setLong(1, id);
statement.execute();
connection.commit();
} catch (SQLException e) {
rollback(connection);
throw new RuntimeException(e);
} finally {
if (connection != null) {
close(connection);
}
}
}
public void delete(Long id) {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("delete from users where id=?");
statement.setLong(1, id);
statement.execute();
connection.commit();
} catch (SQLException e) {
rollback(connection);
throw new RuntimeException(e);
} finally {
if (connection != null) {
close(connection);
}
}
}
查询数据:
Java代码
public User findById(Long id) {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("select user_name,password from users where id=?");
statement.setLong(1, id);
ResultSet resultSet = statement.executeQuery();
User user = null;
if (resultSet.next()) {
user = new User();
user.setId(id);
user.setUserName(resultSet.getString("user_name"));
user.setPassword(resultSet.getString("password"));
}
resultSet.close();
statement.close();
connection.commit();
return user;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (connection != null) {
close(connection);
}
}
}
public User findById(Long id) {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("select user_name,password from users where id=?");
statement.setLong(1, id);
ResultSet resultSet = statement.executeQuery();
User user = null;
if (resultSet.next()) {
user = new User();
user.setId(id);
user.setUserName(resultSet.getString("user_name"));
user.setPassword(resultSet.getString("password"));
}
resultSet.close();
statement.close();
connection.commit();
return user;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (connection != null) {
close(connection);
}
}
}
以上就是derby的简单操作.
java操作derby_javaDB—— derby简单操作相关推荐
- java对mysql的简单操作的综合运用——登录+注册+修改密码
本篇博客是java对mysql的简单操作的综合运用--登录系统.java对mysql的简单操作的综合运用--注册系统.java对mysql的简单操作的综合运用--修改密码系统的整合. 因为使用的是数据 ...
- java对mysql的简单操作的综合运用——修改密码系统
本篇博客运用到: java连接mysql数据库连接 java对mysql的简单操作--修改数据 下面是修改密码系统的完整代码 import java.awt.event.ActionEvent; im ...
- java对mysql的简单操作的综合运用——登录系统
本篇博客运用到: java连接mysql数据库连接(数据搜索) 下面是登录系统的完整代码 import java.awt.event.ActionEvent; import java.awt.even ...
- java对mysql的简单操作——增删改查的总结
增删改查的详细内容可以点击以下链接: java对mysql的简单操作--增加数据 java对mysql的简单操作--删除数据 java对mysql的简单操作--修改数据 java连接mysql5.1教 ...
- java对mysql的简单操作的综合运用——注册系统
本篇博客运用到: java连接mysql数据库连接 java对mysql的简单操作--增加数据 下面是注册系统的完整代码 import java.awt.event.ActionEvent; impo ...
- java对mysql的简单操作——增加数据
java连接mysql5.1教程(含代码)+ 查询数据 下面是数据添加片段的代码 Connection conn = null; Statement stmt = null; PreparedStat ...
- JAVA使用ASCII码简单操作
JAVA使用ASCII码简单操作 int intParam = 'a' + 'b'; //ascii码(a+b = 97 + 98)char charParam = 'a' + 'b';char ch ...
- java对mysql的简单操作——删除数据
java连接mysql5.1教程(含代码)+ 查询数据 相关文章推荐: java对mysql的简单操作--增加数据 下面是数据删除片段的代码 Connection conn = null; State ...
- mysql 空间数据操作,MySQL 空间数据 简单操作
在做的项目中需要,自己绘制区域图形,并存储起来,后面还有更新的需要,存文件不方面,想到现在数据库都支持空间数据库. 现在用的就是 MySQL ,就继续用 MySQL 来存储.管理空间数据.下面就做一些 ...
最新文章
- sublime text 3 python开发环境配置
- 只腐蚀毛刺 腐蚀算法_摩托车油箱防腐蚀、油封安装、密封清洗经验分享
- px,em, rem的区别,在项目中怎么使用rem.
- wxWidgets:wxListCtrl 示例
- java项目上线mysql查询慢_Java Web应用程序在缓慢的MySQL查询中停滞不前
- 新浪微博放开140字限制:社交向左 原创向右
- jmx客户端_Java JMX客户端示例– JMX身份验证
- postgresql 修改表字段的长度
- 简单提高MIDI音量的方法
- PyHook3实现监控键盘鼠标操作
- 国内最适合年轻人旅游的地方
- 微信自动跳转默认浏览器 微信扫一扫直接打开外部浏览器
- 【Unity2D入门教程】简单制作战机弹幕射击游戏⑤C#编写 背景滚动移动以及增加粒子特效
- Flappy Bird游戏 C语言实现
- Ubuntu中解压出现:bzip2: (stdin) is not a bzip2 file.
- Intel核显--OpenCL环境--Linux
- Java面试题汇总大杂汇
- Linux实战技巧--文件系统操作(二)--创建和删除目录(mkdir/rm)
- 浅谈关于申请CCC认证以后的好处
- redis slow log采集
热门文章
- Android 注解及apt使用
- mulesoft Module 3 quiz 解析
- 手把手教你搭建腾讯云服务器
- 关于 torch 的 device id 与真实 GPU id 的关系
- 论文翻译-SAFL A Self-Attention Scene Text Recognizer with Focal Loss
- 安徽大学计算机学院博士生导师,安徽大学计算机科学与技术学院博士生导师:张兆翔教授...
- python列表和字典各自对应操作的时间复杂度
- jdom 组装xml以及解析xml
- hive 笔记(有点乱,凑合看)
- java存储json到mongo_使用spring在mongodb中存储JSON模式