jdbc 程序使用可调用语句(存储过程)求数字的平方

问题描述 投票:0回答:3
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 jdbc syntax
3个回答
0
投票
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;

0
投票

在mysql存储过程中,记得使用'set'关键字。


0
投票

/在 mySQL 中,您应该使用以下代码,而不是使用 y:=xx。在创建存储过程之前还要更改分隔符*/ 如果存在 fileproc,则删除过程; 分隔符%% 创建过程 fileproc(IN x INT,OUT y INT) 开始 将x * x选入y; 结尾%% 分隔符; 调用 fileproc(5,@y);
-- 调用函数。使用@(变量名)在其中存储值 选择@y as sqrnum -- 显示num的sqr

© www.soinside.com 2019 - 2024. All rights reserved.