站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题JDBC 调用存储过程问题

JDBC 调用存储过程问题

摘要: JDBC 调用存储过程问题


SQLSERVER:
call.registerOutParameter(19,Types.VARCHAR);
callly.execute();
id=callly.getString(19);//这行报错[Microsoft][SQLServer JDBC Driver]The requested data is not available

存储过程在数据库里测试了。正常输出、、


public String executeProc(String p)
{
ResultSet myrs = null;
String sql = "{call PROC_AUTOID(?,?)}";
String temp = "";
String tempid = "";

try
{
conn = DriverManager.getConnection(sConnStr, UseName, PassWord);
CallableStatement callablestatement = conn.prepareCall(sql);
callablestatement.setString(1, p);
callablestatement.registerOutParameter(2, 1);
myrs = callablestatement.executeQuery();
myrs.next();
temp = myrs.getString(1);
tempid = callablestatement.getString(2);
myrs.close();
myrs = null;
callablestatement.close();
callablestatement = null;
}
catch(Exception e)
{
System.err.println("executeProc:" + e.getMessage());
}
finally
{
myrs = null;
}
return tempid;
}


同意楼上

==================================
= 自强不息,厚德载物!
==================================


我也是这么写的。。其他的都可以。。。这个存储过程就不行。。这个存储过程是接受参数,然后插入数据库,并返回插入的ID。


代码如下:
Connection cn = ConnectionPools.getConnection();
Connection ly = ConnectionPools.getConnection("0");
CallableStatement call,callly;
ResultSet rs=null;
String pid,id,xlh;
Statement st =null;
int count=0;
try {
call= cn.prepareCall("{call if_locale_p_matbdfpos}");
callly = ly.prepareCall("{call ghlycgdb.dbo.insDDMX(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
callly.registerOutParameter(2,Types.VARCHAR);

st = cn.createStatement();
rs=call.executeQuery();
ly.setAutoCommit(false);
cn.setAutoCommit(false);
while(rs.next()){

id=rs.getString("id");
pid=rs.getString("powerid");

try{
System.out.println(Integer.parseInt(pid));
callly.setInt(1,Integer.parseInt(pid));
callly.setString(3,rs.getString("mee"));
callly.setInt(4,rs.getInt("bstmen"));
callly.setString(5,rs.getString("berdtm"));
callly.setString(6,rs.getString("lifadr"));
callly.setString(7, rs.getString("aendtm"));
callly.setString(8,rs.getString("firknz"));
callly.setInt(9,rs.getInt("artnum"));
callly.setInt(10,rs.getInt("bdfnum"));
callly.setString(11,rs.getString("planname"));
callly.setString(12,rs.getString("plantype"));
callly.setString(13,rs.getString("bdfmel"));
callly.setString(14,rs.getString("orgknz"));
callly.setInt(15,rs.getInt("bdfmen"));
callly.setString(16,rs.getString("bdfprio"));
callly.setString(17,rs.getString("matbes"));
callly.setFloat(18,rs.getFloat("verpre"));
callly.setInt(19,rs.getInt("wlbh"));
callly.execute();
xlh=callly.getString(2);
st.execute("update if_cache_matbdfpos set loc_status=10,lyxlh=cast('"+xlh+"' as int) where id="+id);
ly.commit();
cn.commit();
}catch(Exception e){
e.printStackTrace();
ly.rollback();
cn.rollback();
}
count++;
}
ly.setAutoCommit(true);
cn.setAutoCommit(true);
call.close();
callly.close();
} catch (SQLException e) {
e.printStackTrace();
}

ConnectionPools.close("0",ly);
ConnectionPools.close(cn);


你的jdbc驱动的版本是不是配合的?


用的是MS的JDBC版本。。但是同样我写了好几个这样带输出参数的存储过程,其他都没有问题。这个只是参数多了而已。。
而且这个存储过程不复杂,插入数据,返回插入的ID号。。


你把你的程序完整贴出来


上面贴出来了啊。
在xlh=callly.getString(2);报错
[Microsoft][SQLServer JDBC Driver]The requested data is not available

服务器1的存储过程:if_locale_p_matbdfpos读取数据集合。。
服务器2的存储过程:insDDMX(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)。把服务器1的数据逐条插入第二个数据库中。并返回插入的成功的ID标识号


public class UITest extends TestCase {

public void testDD() throws Exception{


Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection cn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=tsdb","sa","at911");
String ls_sql = "{call insDDMX (218,?,'个',9,null,null,'2006-07-18 16:59:22.000',null,192183767,66154,null,'需求','bfs',null,5,null,null,2.0,92078)}";
java.sql.CallableStatement lp = cn.prepareCall( ls_sql ) ;
lp.registerOutParameter(1, java.sql.Types.VARCHAR );
lp.execute();
System.out.println(lp.getString(1));//这里出错[Microsoft][SQLServer JDBC Driver]The requested data is not available

lp.close();
cn.close();
}

}


call.registerOutParameter(19,Types.VARCHAR);
callly.execute();
id=callly.getString(19);//这行报错[Microsoft][SQLServer JDBC Driver]The requested data is not



上面贴出来了啊。
在xlh=callly.getString(2);报错
[Microsoft][SQLServer JDBC Driver]The requested data is not available

服务器1的存储过程:if_locale_p_matbdfpos读取数据集合。。
服务器2的存储过程:insDDMX(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)。把服务器1的数据逐条插入第二个数据库中。并返回插入的成功的ID标识号



你参数一会2 一会是19
是不是参数位置没对上



String ls_sql = "{call insDDMX (218,?,'个',9,null,null,'2006-07-18 16:59:22.000',null,192183767,66154,null,'需求','bfs',null,5,null,null,2.0,92078)}";

lp.registerOutParameter(1, java.sql.Types.VARCHAR );


输出参数呢


up


jokerjava(冷血) ( ) 我调换参数的位置了测试过。肯定都对上了。。

interpb(曾曾胡,深怕情多累美人!!!) 第二个参数是?.这个是返回值。。


public class UITest extends TestCase {

public void testDD() throws Exception{


Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection cn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=tsdb","sa","at911");
String ls_sql = "{call insDDMX (218,?,'个',9,null,null,'2006-07-18 16:59:22.000',null,192183767,66154,null,'需求','bfs',null,5,null,null,2.0,92078)}";
java.sql.CallableStatement lp = cn.prepareCall( ls_sql ) ;
lp.registerOutParameter(1, java.sql.Types.VARCHAR );
lp.execute();
System.out.println(lp.getString(1));//这里出错[Microsoft][SQLServer JDBC Driver]The requested data is not available

lp.close();
cn.close();
}

}


lp.registerOutParameter(1, java.sql.Types.VARCHAR );
。。这里应该是2吧
lp.registerOutParameter(2, java.sql.Types.VARCHAR );


是不是没有返回数据

你插入不成功的时候 是返回什么?



谢谢大家回复。。问题已经解决了:)

我的这个存储过程包含一条Insert into 语句。所以不能再用execute()方法执行存储过程,还是得用executeUpdate()方法执行 。。
我写的其他得存储过程都是select语句。所以可以用execute()方法执行。。


mark


↑返回目录
前一篇: 当extends一个类时,构造函数是否还需要super?
后一篇: 求算法。