Ms Sql 和 Oracle 调用返回多条记录的存储过程
2008-04-08 12:14
471 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package oracle;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.Connection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.DriverManager;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.CallableStatement;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.ResultSet;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.microsoft.jdbc.sqlserver.SQLServerDriver;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class InvokeProcedure ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private static Connection getConnection() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Connection conn = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String url ="jdbc:oracle:thin:@127.0.0.1:1521:demo";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Class.forName("oracle.jdbc.driver.OracleDriver");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
conn = DriverManager.getConnection(url, "scott", "tiger");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return conn;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private static Connection getsqlConnection() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Connection conn = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String url ="jdbc:microsoft:sqlserver://localhost:1433;databaseName=y2";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
conn = DriverManager.getConnection(url, "sa", "");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return conn;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//调用oracle的存储过程 ---> 查询数据得到游标
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void invokeProcedureResutl()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Connection conn =null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CallableStatement callableStatement =null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
conn = getConnection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第一步:获得CallableStatemen的实例;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
callableStatement = conn.prepareCall("{call p_result.pro_get_result(?,?,?,?)}");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第二步:为输出参数注册数据类型,为输入参数赋值;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
callableStatement.registerOutParameter(1,oracle.jdbc.driver.OracleTypes.CURSOR);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
callableStatement.setString(2, "emp");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
callableStatement.setInt(3, 2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
callableStatement.setInt(4, 4);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第三步:执行存贮过程和获得返回值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
callableStatement.execute();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ResultSet result =(ResultSet)callableStatement.getObject(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (result!=null)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while(result.next())...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//result.get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//System.out.println(result);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//InvokeProcedure.getConnection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InvokeProcedure.invokeProce();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//调用sqlserver的存储过程 --- 查询数据 select * from 表 where date > 2005.12.14
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void invokeProce()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Connection conn =null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CallableStatement callableStatement =null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
conn = getsqlConnection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第一步:获得CallableStatemen的实例;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
callableStatement = conn.prepareCall("{call pop_pro(?)}");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第二步:为输出参数注册数据类型,为输入参数赋值;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
callableStatement.setString(1, "2005.12.14");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//第三步:执行存贮过程和获得返回值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
callableStatement.execute();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ResultSet result =callableStatement.getResultSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (result!=null)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while(result.next())...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//result.get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//System.out.println(result);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 利用游标返回结果集的的例子(Oracle 存储过程)JAVA调用方法和.NET调用方法
- [Oracle]高效的PL/SQL程序设计(五)--调用存储过程返回结果集
- hibernate里使用jdbc方式调用oracle带游标返回参数的简单分页存储过程
- 学习记录 asp.net c#连接sql server 2012 调用存储过程 用返回的记录填充CheckBoxList 都是常用的,特此记录
- mybatis 调用 oracle 返回结果集 存储过程
- myabatis oracle 调用存储过程返回list结果集
- jpa 调用oracle 存储过程 无返回值 ,有返回值。
- [Oracle]高效的PL/SQL程序设计(五)--调用存储过程返回结果集
- Java 调用 Oracle 存储过程返回结果集
- 其实iBatis可以在调用新增记录的方法后 返回主键值 (Oracle,Sql Server,Mysql)
- [Oracle]高效的PL/SQL程序设计(五)--调用存储过程返回结果集
- [Oracle]高效的PL/SQL程序设计(五)--调用存储过程返回结果集
- Mybaits结合Oracle调用存储过程返回游标的两种方法
- oracle中存储过程调用存储过程(带有返回结果集)
- java程序调用Oracle 存储过程 获取返回值(无返回,非结果集,结果集)
- oracle 动态调用存储过程,返回游标类型
- oracle创建存储过程并返回结果集(附C#调用代码)
- Oracle创建表语句(Create table)语法详解及示例、、 C# 调用Oracle 存储过程返回数据集 实例
- jdbc调用存储过程-oracle版 返回游标
- java调用oracle返回结果集的存储过程