【存儲過程1】java
寫一個存儲過程 procedure實現兩數相加mysql
DELIMITER && CREATE PROCEDURE pr_add(xx INT,yy INT) BEGIN DECLARE zz INT; SET zz=xx+yy; SELECT zz AS SUM; END && DELIMITER ;
MySql調用存儲過程 CALL pr_add(3,5);sql
Java調用存儲過程ui
使用CallableStatementblog
package com.zhiqi.fir; import java.sql.*; public class Fir { private static String jdbcName="com.mysql.jdbc.Driver"; private static String dbUrl="jdbc:mysql://localhost:3306/Fruit"; private static String dbUser="root"; private static String dbPassword="123456"; public static void main(String[] args) throws Exception { Class.forName(jdbcName); Connection conn=DriverManager.getConnection(dbUrl,dbUser,dbPassword); CallableStatement cast=conn.prepareCall("call pr_add(?,?)"); cast.setInt(1, 5); cast.setInt(2, 1); ResultSet rs=cast.executeQuery(); while(rs.next()){ System.out.println(rs.getInt(1)); } conn.close(); } }
【存儲過程2】get
水果表,存儲過程查詢庫存量大於某數值的水果名稱it
DELIMITER && CREATE PROCEDURE pro_Fruit_kcl(IN xx INT) READS SQL DATA BEGIN SELECT NAME FROM Fruit WHERE Numbers>xx; END && DELIMITER ;
MySql調用存儲過程io
CALL pro_Fruit_kcl(99);
Java調用MySql調用存儲ast
package com.zhiqi.fir; import java.sql.*; public class Fir { private static String jdbcName="com.mysql.jdbc.Driver"; private static String dbUrl="jdbc:mysql://localhost:3306/Fruit"; private static String dbUser="root"; private static String dbPassword="123456"; public static void main(String[] args) throws Exception { Class.forName(jdbcName); Connection conn=DriverManager.getConnection(dbUrl,dbUser,dbPassword); CallableStatement cast=conn.prepareCall("CALL pro_Fruit_kcl(?)"); cast.setInt(1, 99); ResultSet rs=cast.executeQuery(); while(rs.next()){ System.out.println(rs.getString(1)); } conn.close(); } }
【存儲過程3】class