sysbase过程调用方式

package minusyhd;

import java.io.UnsupportedEncodingException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
public class CopyOfMinusYHD {
    private static String driverName= "com.sybase.jdbc4.jdbc.SybDataSource";
    //192.168.40.159,4018
    private static String url = "jdbc:sybase:Tds:127.0.0.1:7001/card?charset=cp850";
    private static String username = "sa";
    private static String password = "";
    static
    {
        try{
            Class.forName(driverName);
        }catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }
    public Connection getConnection()
    {
        Connection myConnection  = null;
        try {
            myConnection  = DriverManager.getConnection(url, username, password);
        }catch (Exception ex) {
            ex.printStackTrace();
        }
        return myConnection;
    }
    
    
    /**
     * 释放数据库连接
     */
    public static void release(Connection conn, Statement cs, ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(cs != null){
            try {
                cs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * @param args
     * @throws UnsupportedEncodingException 
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        getDataFromMasterMultiResultSet();
    }
/**
    IF OBJECT_ID ('dbo.Pro_getSudent') IS NOT NULL
    DROP PROCEDURE dbo.Pro_getSudent
GO

create procedure Pro_getSudent    
(
@startIndex INT,
@endIndex INT ,
@errorCode INT output,
@errorMsg VARCHAR(255) output
) 
as     
begin     
    PRINT 'create temp table'
    create table #pageTemp       
    (       
    ID numeric(15,0) IDENTITY,
    nid int 
    ) 
    set rowcount @endIndex
    insert into #pageTemp(nid) select ID from T_STUDENT ORDER BY ID
    select @errorCode=1,@errorMsg='用于测试的error message 信息'           
    select O.ID,O.name from T_STUDENT O,#pageTemp t where O.ID=t.nid
    and t.ID>=@startIndex 
end     
GO
     */
    public static void getDataFromMasterInBetweenOutAndSelect() throws UnsupportedEncodingException
    {
        CopyOfMinusYHD mySybaseTest = new CopyOfMinusYHD();
        Connection conn = null;
        CallableStatement cs = null;
        ResultSet rs = null;
        try
        {
            conn = mySybaseTest.getConnection();
            cs = conn.prepareCall("{call Pro_getSudent(?,?,?,?)}");
            cs.setInt(1, 1);
            cs.setInt(2, 5);
            cs.registerOutParameter(3, Types.INTEGER);
            cs.registerOutParameter(4, Types.VARCHAR);
            cs.execute();
            System.out.println("3:"+cs.getInt(3));
            System.out.println("4:"+new String(cs.getString(4).getBytes("ISO-8859-1")));
            rs = cs.executeQuery();
            while(rs.next())
            {
                System.out.println(new String(rs.getString(2).getBytes("ISO-8859-1")));
            }
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            release(conn,cs,rs);
        }
    }
 
    
    
/**
    IF OBJECT_ID ('dbo.Pro_getSudent2') IS NOT NULL
    DROP PROCEDURE dbo.Pro_getSudent2
GO

create procedure Pro_getSudent2    
(
@startIndex INT,
@endIndex INT ,
@errorCode INT output,
@errorMsg VARCHAR(255) output
) 
as     
DECLARE
@aaa VARCHAR(1)
begin     
    PRINT 'create temp table'
    
    create table #pageTemp       
    (       
    ID numeric(15,0) IDENTITY,
    nid int 
    ) 
    --SELECT @aaa = @startIndex
    set rowcount @endIndex
    insert into #pageTemp(nid) select ID from T_STUDENT ORDER BY ID
     
    select @errorCode=1,@errorMsg='用于测试的error message 信息'           

    select O.ID,O.name from T_STUDENT O,#pageTemp t where O.ID=t.nid
    and t.ID>=@startIndex 
     
    select O.ID,O.name from T_STUDENT O,#pageTemp t where O.ID=t.nid
    and t.ID>=@startIndex  ORDER BY O.name DESC
end     
GO
     */
    public static void getDataFromMasterMultiResultSet() throws UnsupportedEncodingException
    {
        CopyOfMinusYHD mySybaseTest = new CopyOfMinusYHD();
        Connection conn = null;
        CallableStatement cs = null;
        ResultSet rs = null;
        try
        {
            conn = mySybaseTest.getConnection();
            cs = conn.prepareCall("{call Pro_getSudent2(?,?,?,?)}");
            cs.setInt(1, 1);
            cs.setInt(2, 5);
            cs.registerOutParameter(3, Types.INTEGER);
            cs.registerOutParameter(4, Types.VARCHAR);
//            cs.execute();
            cs.executeQuery();//或者cs.execute();
            System.out.println("ee:"+cs.getInt(3));
            System.out.println("ff:"+cs.getString(4));
            rs = cs.executeQuery();
            while(rs.next())
            {
                System.out.println(rs.getInt(1));
                System.out.println(rs.getString(2));
            }
            System.out.println("=================");
            if(cs.getMoreResults())
            {
                System.out.println("ddd");
                rs = cs.getResultSet();
                while(rs.next())
                {
                    System.out.println(rs.getInt(1));
                    System.out.println(rs.getString(2));
                }
            }
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            release(conn,cs,rs);
        }
    }
 
    
    public static void getStudent()
    {
        CopyOfMinusYHD mySybaseTest = new CopyOfMinusYHD();
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try
        {
            conn =  mySybaseTest.getConnection();
            st = conn.createStatement();
            rs = st.executeQuery("SELECT * FROM T_STUDENT");
            while(rs.next())
            {
                System.out.println(rs.getInt(1));
                if(null==rs.getString(2))
                {
                    System.out.println("dddddddddddd");
                }
                System.out.println(rs.getString(2));
                System.out.println(rs.getString(3));
            }
        }
        catch(Exception e)
        {
            System.out.println("搬数据异常");
            e.printStackTrace();
        }
        finally
        {
            release(conn,st,rs);
        }
    }
}



http://blog.csdn.net/rubyzhudragon/article/details/5049571

你可能感兴趣的:(SYS)