怎样写一个向表中插入数据的存储过程并用JAVA代码调用该存储过程?
怎样写一个向表中插入数据的存储过程并用JAVA代码调用该存储过程?
下面是我写的一个存储过程,但有错误,欢迎指出错误!
创建存储过程save_rating
CREATE DEFINER=`root`@`localhost` PROCEDURE `save_rating`(IN p1 VARCHAR(10),IN p2 VARCHAR(30),IN p3 VARCHAR(30))
BEGIN
insert into stock values(p1,p2,p3);
END
调用它的JSP代码:
<%@ page import= "java.sql.*, com.apress.jdbc.* " %>
<html>
<head>
<title> INVOKE PROCEDURE EXAMPLE </title>
</head>
<body>
<h1> Save Ratings </h1>
<%
Connection conn = null;
try {
conn = JdbcManager.getConnection();
String sql= "{?=save_rating(?,?,?)} ";
CallableStatement cs=conn.prepareCall(sql);
cs.setString(2, 'ZAP ');
cs.setString(3, 'Frid Smith ');
cs.setString(4, 'good ');
cs.registerOutParameter(1,java.sql.Types.INTEGER);
cs.execute();
int ss=cs.getInt(1);
out.println( "ss= "+ss);
} catch (SQLException e) {
%>
<%= e.getMessage() %>
<%
e.printStackTrace();
} finally {
JdbcManager.close(conn);
}
%>
</body>
</html>
当执行该jsp代码时产生如下错误;
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 15 in the jsp file: /invokeprocedure.jsp
Invalid character constant
12: conn = JdbcManager.getConnection();
13: String sql= "{?=save_rating(?,?,?)} ";
14: CallableStatement cs=conn.prepareCall(sql);
15: cs.setString(2, 'ZAP ');
16: cs.setString(3, 'Frid Smith ');
17: cs.setString(4, 'good ');
18: cs.registerOutParameter(1,java.sql.Types.INTEGER);
------解决方案--------------------
http://blog.csdn.net/echott/archive/2006/03/28/641772.aspx
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.aiyiweb.com/