关于mysql中的触发器能调用JAVA吗的搜索推荐

关于mysql中的触发器能调用JAVA吗的搜索推荐,第1张

肯定不可以,mysql不能调用java代码,但是可以在java中创建触发器

1.使用SQL创建触发器

DELIMITER $$CREATE TRIGGER `catefiles_trigger` AFTER INSERT ON `catefiles` FOR EACH ROWbegin 

declare num1 int set num1 = (select num from est_client_catescan_status where cateid=new.cateId and taskid=new.taskId and clientid=new.clientId) if(num1>=0) then update catescan_status set num=num1+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$$

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()

3.可以看出区别:在java中创建触发器,不需要限定符DELIMITER ,加上的话执行过程中会报MySQL语法错误

连接数据库

import java.sql.Connection

import java.sql.DriverManager

import java.sql.ResultSet

import java.sql.SQLException

import java.sql.Statement

/**

* 提供数据库的连接以及关闭数据库资源

* @author Administrator

*

*/

public class DBManager {

//定义数据库连接的URL

private static final String URL="jdbc:sqlserver://localhost:1433database=j1105"

//定义数据库的用户名

private static final String USERNAME = "sa"

//定义数据库密码

private static final String PASSWORD = "sa"

//定义一个连接的引用,使用单例模式

private static Connection conn = null

//使用静态块来注册驱动

//类加载时自动执行代码块

static {

//反射com.microsoft.sqlserver.jdbc.SQLServerDriver.class

try {

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")

} catch (ClassNotFoundException e) {

e.printStackTrace()

}

}

//获得连接

//在程序使用过程中始终只有1个对象存在

//使用单例模式来给Connection赋值

public static Connection getConnection(){

if(conn == null){

try {

conn = DriverManager.getConnection(URL, USERNAME, PASSWORD)

} catch (SQLException e) {

e.printStackTrace()

}

}

return conn

}

/**

* 关闭的一些 *** 作 , 优化

* @param conn

* @param stat

* @param rs

*/

public static void close(Connection conn,Statement stat,ResultSet rs){

try{

if(conn != null){

conn.close()

}

if(stat != null){

stat.close()

}

if(rs != null){

rs.close()

}

}catch(SQLException e){

e.printStackTrace()

}

}

/**

* 重写上面的方法,在只有2个参数的情况下关闭

* @param conn

* @param stat

*/

public static void close(Connection conn,Statement stat){

try{

if(conn != null){

conn.close()

}

if(stat != null){

stat.close()

}

}catch(SQLException e){

e.printStackTrace()

}

}

public static void main(String[] args){

Connection conn = DBManager .getConnection()

System.out.println(conn)

}

}

新建一个类对应数据库中的表


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/8524567.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-17
下一篇2023-04-17

发表评论

登录后才能评论

评论列表(0条)

    保存