import java .sql.*;
class Pro
{
public static void main(String args[]) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection conn = DriverManager.getConnection (url,"root","password");
CallableStatement cst=conn.prepareCall("{call fileproc(?,?)}");
cst.setInt(1,10);
cst.registerOutParameter(2,Types.INTEGER);
cst.execute();
int i=cst.getInt(2);
System.out.println("the number is :" +i);
cst.close();
conn.close();
}
}
在 MYSQL 中:
fileproc:
CREATE PROCEDURE fileproc(IN x INT,OUT y INT)
BEGIN
y := x * x;
END;
/
在 fileproc 中输入 END 之前,我在第 3 行遇到错误
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near ':= x
* x' at line 3.
正确的语法是什么? 请帮忙。 谢谢你。
mysql>create database <database-name>;
->use <database-name>;
delimiter //
Create Procedure fileproc(in x int, out y int)
begin
set y = x * X;
end //
mysql> delimiter ;
mysql> call(10,@y);
mysql> Select @y;
在mysql存储过程中,记得使用'set'关键字。
/在 mySQL 中,您应该使用以下代码,而不是使用 y:=xx。在创建存储过程之前还要更改分隔符*/
如果存在 fileproc,则删除过程;
分隔符%%
创建过程 fileproc(IN x INT,OUT y INT)
开始
将x * x选入y;
结尾%%
分隔符;
调用 fileproc(5,@y);
-- 调用函数。使用@(变量名)在其中存储值
选择@y as sqrnum -- 显示num的sqr