SQL SERVER中有这个存储过程,我想在程序中获得titles中的纪录,不知该如何写CREATE PROCEDURE titles_cursor @titles_cursor CURSOR VARYING OUTPUT
AS
SET @titles_cursor = CURSOR
FORWARD_ONLY STATIC FOR
SELECT *
FROM titlesOPEN @titles_cursor
GO

解决方案 »

  1.   

    给你一个测试例子。存储过程可以在MSSQLServer调试一下/*
     * Created on 2005-7-15
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package test;import java.sql.CallableStatement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Types;
    import java.util.Hashtable;import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.sql.DataSource;/**
     * @author liang liping
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class ProcessTest {

    /*public ProcessTest(){
            
    }*/

        public ProcessTest(){
            Connection con = null;
           
                try {
                   // Context ct = getContext();
    //          look up datasource
    //                Object obj = ct.lookup("WF_DEFINE DATASOURCE");
    //                DataSource ds = (DataSource)obj;                //get connection
                  String url = "jdbc:microsoft:sqlserver://192.168.1.200:1433";
                        Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加载驱动程序
                        try {
                          con = DriverManager.getConnection(url, "PROCESS","123456");
                        }
                        catch (SQLException e) {
                          throw e;
                        }
                        
                    //con = ds.getConnection();
                    
                    CallableStatement csmt = con.prepareCall("{?=call SP_GET_URL_ID(?)}");
                    long l = System.currentTimeMillis();
                   //for(int i=0;i<100;i++){                
                    csmt.registerOutParameter(1,Types.INTEGER);
                    csmt.registerOutParameter(2,Types.INTEGER);
                    //csmt.setInt(1,2);
                    csmt.execute();
                    
                    String returnvalue = csmt.getString(1);
                    String out = csmt.getString(2);
                    
                    
                    System.out.println("returnvalue--> "+returnvalue
                     +" out--> "+out);
                    
                  // }
                   long l2 = System.currentTimeMillis()-l;
                   System.out.println("l2--> "+l2+"\n");
     

                   
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch(Exception e){
                    e.printStackTrace();
                }
           
        }
        public static void main(String[] args) {
         try {
         new ProcessTest();
    } catch (RuntimeException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
        }
        public void runThread(){
         ThreadTest tt = new ThreadTest();
         tt.start();
        }
       /* private InitialContext getContext() throws NamingException {
            Hashtable props = new Hashtable();        props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
                    "weblogic.jndi.WLInitialContextFactory");
            props.put(InitialContext.PROVIDER_URL, "t3://192.168.1.200:7001");        // This establishes the security for authorization/authentication
            // props.put(InitialContext.SECURITY_PRINCIPAL,"username");
            // props.put(InitialContext.SECURITY_CREDENTIALS,"password");        InitialContext initialContext = new InitialContext(props);
            return initialContext;
        }
        */
        class ThreadTest extends Thread{
         public ThreadTest(){
        
         }
         public void run(){
         try {
         for(int i=0;i<100;i++){      
     //Thread.sleep(500);
     long l = System.currentTimeMillis();
     //new ProcessTest(1);
     long l2 = System.currentTimeMillis()-l;
     System.out.println("l2--> "+l2+"\n");
         }
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
         }
        }
    }
      

  2.   

    谢谢,jacbo
    不过,这个不是我想要的,我不是要得到某个值 ,
    我要得到的是一个查询结果,是一个集合
      

  3.   

    修改存储过程,
    CREATE PROCEDURE titles_cursor
    AS
    SELECT *
    FROM titles
    GO直接ResultSet rt = st.executeQuery();