當前位置:首頁 » 操作系統 » 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 代表每次執行間隔時間
如果有不懂的可以再來問我

熱點內容
卡宴怎麼看配置 發布:2024-04-27 00:41:08 瀏覽:941
央視影音緩存視頻怎麼下載視頻 發布:2024-04-27 00:25:55 瀏覽:583
手機緩存的視頻怎麼看 發布:2024-04-27 00:11:05 瀏覽:57
shell腳本平方計算公式 發布:2024-04-26 23:29:26 瀏覽:187
比較實惠的雲伺服器 發布:2024-04-26 23:24:57 瀏覽:974
怎麼增加電腦緩存 發布:2024-04-26 23:23:46 瀏覽:451
android調試gdb 發布:2024-04-26 23:22:27 瀏覽:99
androidsocket服務 發布:2024-04-26 22:49:53 瀏覽:980
python編譯時加密 發布:2024-04-26 22:49:20 瀏覽:246
買車看哪些配置參數 發布:2024-04-26 22:45:50 瀏覽:835