java中如何使用存储过程?

lanfanghelanfanghe 2004-09-13 03:57:14
1.我用向导创建了三个存储过程,包括增加、删除和修改;
存储过程名称分别为:insert_mytable
update_mytable
delete_mytable


2.我如何在java程序中执行这三个过程
...全文
220 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanfanghelanfanghe 2004-09-13
  • 打赏
  • 举报
回复
多谢!!!
Ronanljy 2004-09-13
  • 打赏
  • 举报
回复
cstmt = conn.prepareCall({ call insert_mytable (?,?,?)});
cstmt.setInt(1, Integer.parseInt(value1.toString()));
cstmt.setInt(2, Integer.parseInt(value2.toString()));
cstmt.setInt(3, Integer.parseInt(value3.toString()));

如果有三个参数就是上面这样。传参数的方法要区别出参数的类型。
如果有返回值,就要这样:
Resule result = cstmt.executeQuery();
Ronanljy 2004-09-13
  • 打赏
  • 举报
回复
忘记说用法了:

CallProcedure callPrc = new CallProcedure("prc_AddClass", 4);
callPrc.addParameter(1, Types.CHAR, infoClass.getClassId());
callPrc.addParameter(2, Types.CHAR, infoClass.getClassName());
callPrc.addParameter(3, Types.CHAR, infoClass.getDescription());
callPrc.addParameter(4, Types.CHAR, infoClass.getOwnerClassId());
callPrc.execute();


还有Types是一个自己定义的类:

package com.longtop.xxk.common;

/**
* <p>Title: XXK</p>
* <p>Description: Information Library</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: LongTop</p>
* @authorCh chaossoft
* @version 1.0
*/

public class SqlType {

public static final int BIG_INT = 10;
public static final int INT = 11;
public static final int SMALL_INT = 12;
public static final int TINY_INT = 13;
public static final int BIT = 14;

public static final int NUMERIC = 20;
public static final int DECIMAL = 21;

public static final int MONEY = 30;
public static final int SMALL_MONEY = 31;

public static final int FLOAT = 40;
public static final int REAL = 41;

public static final int CHAR = 50;
public static final int VARCHAR = 51;
public static final int TEXT = 52;
public static final int NCHAR = 53;
public static final int NVARCHAR = 54;
public static final int NTEXT = 55;
public static final int STRING = 56;

public static final int BINARY = 60;
public static final int VARBINARY = 61;

public static final int DATETIME = 70;
public static final int SMALL_DATETIME = 71;

public static final int TIME_STAMP = 80;

public static final int IMAGE = 90;

public static final int ARRAY = 100;

public static final int OTHERS = -1;
}
lanfanghelanfanghe 2004-09-13
  • 打赏
  • 举报
回复
多谢,但是能给出类的说明吗
Ronanljy 2004-09-13
  • 打赏
  • 举报
回复
公共类:


package com.longtop.xxk.common;

import java.sql.*;

/**
* <p>Title: XXK</p>
* <p>Description: Information Library</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: LongTop</p>
* @authorCh chaossoft
* @version 1.0
*/

public class CallProcedure {
private CallableStatement cstmt;

public CallProcedure(String prcName, int paramNum) throws SQLException {
Connection conn = Database.getConnection();
cstmt = conn.prepareCall(this.InitialCallString(prcName, paramNum));
}

public void execute() throws SQLException {
cstmt.execute();
cstmt.close();
}

public ResultSet executeQuery() throws SQLException {
return cstmt.executeQuery();
}

public void addParameter(int paramIndex, int type, Object value) {
if (paramIndex < 0) {
return;
}
try {
switch (type) {
case Types.INTEGER:
if (value == null) {
value = new Integer(0);
}
cstmt.setInt(paramIndex, Integer.parseInt(value.toString()));
break;
case Types.CHAR:
case Types.VARCHAR:
if (value == null) {
value = new String("");
}
cstmt.setString(paramIndex, value.toString());
break;
case Types.DATE:
java.util.Date date = (java.util.Date) value;
cstmt.setDate(paramIndex, new java.sql.Date(date.getTime()));
break;
case Types.ARRAY:
cstmt.setArray(paramIndex, (Array) value);
break;
default:
cstmt.setObject(paramIndex, value);
}
}
catch (Exception ex) {
ex.printStackTrace();
return;
}
}

private String InitialCallString(String prcName, int paramNum) {
StringBuffer sql = new StringBuffer("{ call " + prcName + "(");
if (paramNum == 0) {
return " { call " + prcName + " } ";
}
for (int i = 0; i < paramNum; i++) {
sql.append("?,");
}
sql.deleteCharAt(sql.length() - 1);
sql.append(") }");
// Print
System.out.println(sql.toString());
return sql.toString();
}
}
Ronanljy 2004-09-13
  • 打赏
  • 举报
回复
不好意思,按错键,上面一条发错了。
你的存储过程有没有参数?这是没有参数,没有返回值的情况:
Connection conn = Database.getConnection();
CallableStatement cstmt;
cstmt = conn.prepareCall({ call insert_mytable });
cstmt.execute();
cstmt.close();
Ronanljy 2004-09-13
  • 打赏
  • 举报
回复
Connection conn = Database.getConnection();
cstmt = conn.prepareCall({ call insert_mytable () })
cstmt.setInt(paramIndex, Integer.parseInt(value.toString()));
lanfanghelanfanghe 2004-09-13
  • 打赏
  • 举报
回复
最好是公共类
但是我怕看不明白
临时方法也好
flyforlove 2004-09-13
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=197732
Ronanljy 2004-09-13
  • 打赏
  • 举报
回复
你要临时的方法,还是要封装好的公用类?
lanfanghelanfanghe 2004-09-13
  • 打赏
  • 举报
回复
请大家帮忙,十万火急!!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧