当前位置:首页 » 操作系统 » java数据库触发器

java数据库触发器

发布时间: 2023-02-04 17:19:52

java如何调用Mysql的触发器

触发器顾名思意就是在某个动作执行时自动触发执行的,不用调用,比如你是在add和delete数据时加触发器,只要你定义的对,数据库在向你指定的那张表add和delete数据时,该触发器就会自动触发

㈡ JavaDB的一些问题

javaDB其实就是Derby,它并不是一个新的数据库产品,它是由IBM捐献给Apache的DB项目的一个纯Java数据库,JDK6.0里面带的这个Derby的版本是 10.2.1.7,支持存储过程和触发器;有两种运行模式,一种是作为嵌入式数据库,另一种是作为网络数据库,前者的数据库服务器和客户端都在同一个 JVM里面运行,后者允许数据库服务器端和客户端不在同一个JVM里面,而且允许这两者在不同的物理机器上.值得注意的是JDK6里面的这个Derby支持JDK6的新特性JDBC 4.0规范(JSR 221),现在我们如果要练习JDBC的用法,没有必要单独装一个数据库产品了,直接用Derby就行.

1、本身没有操作界面,可以用第三方工具来管理(也就是你说的操作界面),Aqua Data Studio 具备管理功能的用于 Apache Derby 关系数据库的管理工具和数据库查询工具。直观管理功能让用户能够浏览和修改数据库结构,包括架构对象和数据库存储,以及维护数据库安全。集成查询工具让您能够迅速创建、编辑和执行 SQL 查询与脚本。Aqua Data Studio 进一步提供导入与导出工具,从而轻松地将数据移入和移出不同的数据格式及 Apache Derby 数据库。集成在这些工具内的是库浏览器 (Repository Browser),拥有 CVS 和 Subversion (SVN) 的完整来源控制客户端。

2、两者的区别,简单的说,就是javaDB是一个简化轻量级数据库,适合小型系统的小规模测试用,完全可以跑在内存里的数据库,它只有3M大小,而MySQL则是可以应用部署大型系统的数据库,功能更多更全,也更稳定,是用范围更广。

3、下面是个使用derby的简单例子:
首先导入JAR包:derby.jar,如果你装的是JDK6,在C:\Program Files\Sun\JavaDB\lib目录下就可以找到.
然后就要创建数据库了:

代码
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连接数据库.

有了数据库,接下来该建表了:

代码
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();
}

创建了 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(这个就不多说了),:

代码
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;
}
}

接下来就可以就数据库进行增删改查的操作了:

插入数据:

代码
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 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.

修改数据:

代码

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);
}
}
}

删除数据:

代码

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 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);
}
}
}

㈢ 如何用java创建触发器

java是应用程序,可以通过jdbc接口调用触发器:
create or replace trigger bj_customer
before update on customer
for each row
begin
update order set
cu_no=:new.cu_no,
cu_name=:new.cu_name,
cu_address=:new.cu_addess,
where cu_no=:old.cu_no;
end;

调用executeUpdate方法即可

㈣ 用触发器如何在java中删除数据库中两个表中的记录(两个表有关联如表A.aID=表B.bID)

这个 是在数据库内 写触发器就可以了

create or replace trigger tri_table_A
after delete on table_A
for each row
begin
delete from table_b where b.id=:old.id;
end tri_table_A;
/

㈤ java中怎么创建mysql的触发器

2.在Java程序里创建触发器
String sql=+" CREATE TRIGGER catefiles_trigger AFTER INSERT ON catefiles FOR EACH ROW"
+" begin"
+" declare scannum int;"
+" set scannum = (select num from est_client_catescan_status where"
+" cateid=new.cateId and taskid=new.taskId and clientid=new.clientId);"
+" if(scannum>=0) then"
+" update catescan_status set num=scannum+1 where cateid=new.cateId and taskid=new.taskId and clientid=new.clientId;"
+" else"
+" insert catescan_status(cateid,num,status,taskid,clientid) values(new.cateId,1,0,new.taskid,new.clientId);"
+" end if;"
+" end";

Connection con = DbConnectionManager.getConnection();
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.execute();

㈥ JAVA WEB项目 使用SQLSERVER数据库,数据发生改变时,保留操作痕迹

一般有三种解决方案:

  1. 从业务角度去控制,每次对数据进行操作的时候像楼下说的那样记录一些关键log信息

  2. 从数据角度去控制,对这个表添加触发器

  3. 配置log4j记录到文件中

㈦ java 不能调用sql的触发器

触发器不是直接调用的,而是DB服务器根据事件触发的。

㈧ JavaDB 是什么

Java DB简介Java DB是Sun公司的轻量级数据库。它却是一个先进的全事务处理的基于Java技术的数据库,它支持各类开放标准、触发器和存储程序。Java DB可以客户端服务器模式使用,也可以直接嵌入到一个Java应用程序中。在这些场合,Java DB都可以在同样的Java虚拟机(JVM)中运行,这就无需在应用程序之外单独购买、下载、安装或管理这个数据库。对于选择在生产中采用Java DB的客户,Sun将提供支持服务。

㈨ java里trigger是干什么用的

Trigger不是java特性也不是java内置的代码,而是其它程序的封装。
在Quartz中Trigger是触发器,Quartz中的触发器用来告诉调度程序作业什么时候触发
即Trigger对象是用来触发执行Job的

㈩ 请问一下怎么做一个定时触发器啊,我想要用java程序中做一个定时触发器,请各位高手指教,最好有源代码

final Timer machinetimer = new Timer();
machinetimer.schele(new TimerTask() {
@Override
public void run() {
//定时执行的方法
XXXX();
}
}, 1000, 1000);
第一个 1000 代表系统运行后,这个定时任务多久会执行。
第二个 1000 代表每次执行间隔时间
如果有不懂的可以再来问我

热点内容
c语言是啥意思啊 发布:2024-05-08 06:01:09 浏览:695
帝豪换压缩机 发布:2024-05-08 05:42:15 浏览:901
java文件时间 发布:2024-05-08 05:32:04 浏览:267
空气压强算法 发布:2024-05-08 05:29:47 浏览:293
c语言中double的范围 发布:2024-05-08 04:53:29 浏览:122
脏小豆服务器怎么加入 发布:2024-05-08 04:40:40 浏览:552
万立存储介质 发布:2024-05-08 04:33:02 浏览:637
ftppro特效复制方法 发布:2024-05-08 04:06:05 浏览:927
平板电脑编译软件 发布:2024-05-08 04:05:46 浏览:478
荣耀v6平板扩展存储 发布:2024-05-08 03:41:12 浏览:423