在開始本文的時候,我們先了解SQL SERVER中的一個命令
SET NOCOUNT ON;
執行該命令 表示不返回計數行,什麼是計數行了,比如我們執行 DELETE ,UPDATE,INSERT的時候,對多少條數據進行了修改,計數行的值就是多少
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
在JDBC的操作數據庫的過程中,你可以把Statement理解成爲指向ResultSet的指針,如果數據庫允許返回記數行的話,Statement將指向該計數行
比如
SET NOCOUNT ON;
update TABLEA SET A='aa';--假設共100條數據被修改
SELECT * FROM TABLEA;
調用
callableStatement.execute();後callableStatement指向受影響的計數行,當你再調用
rs = callableStatement.getResultSet();
的時候,結果集rs 爲空。 無法查詢出表TABLEA 的數據
Statement提供了一個getMoreResults()的方法,該方法能將當前Statement "指針" 移動到下一個結果集.
如果 callableStatement.getUpdateCount()==-1&&getMoreResults()==true的話表明當前statement對象正指向一個真正的結果集
For Examle:
package xx.qq.app;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* @author Jack Zhang Email:[email protected]
* @date 2011-08-22
*/
public class AppTest {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
BeanFactory factory = (BeanFactory) context;
ComboPooledDataSource dataSource = (ComboPooledDataSource) factory
.getBean("dataSource");
Connection con = dataSource.getConnection();
CallableStatement callableStatement = con
.prepareCall("{call GetBasics(?,?)}");
callableStatement.setString(1, "w");
callableStatement.registerOutParameter(2, java.sql.Types.INTEGER);
ResultSet rs=null;
// 是否有結果集返回
boolean hasResultSet = callableStatement.execute();
// callableStatement--------->update
System.out.println("執行存儲過程後Statement是否指向真正的結果集:"+hasResultSet);
System.out.println("受影響的行數:"+callableStatement.getUpdateCount());
callableStatement.getMoreResults();//------->select
rs = callableStatement.getResultSet();
System.out.println("受影響的行:"+callableStatement.getUpdateCount());
while (rs.next()) {
//System.out.println(rs.getObject(1));
}
callableStatement.getMoreResults();//-------->update
System.out.println("受影響的行:"+callableStatement.getUpdateCount());
callableStatement.getMoreResults();//-------->update
System.out.println("受影響的行:"+callableStatement.getUpdateCount());
callableStatement.getMoreResults();//-------->select
System.out.println("受影響的行:"+callableStatement.getUpdateCount());
rs = callableStatement.getResultSet();// 獲取到真實的結果集
while (rs.next()) {
//System.out.println(rs.getObject(1));
}
callableStatement.getMoreResults();//--------->update
System.out.println("受影響的行:"+callableStatement.getUpdateCount());
if (rs != null)
rs.close();
if (callableStatement != null)
callableStatement.close();
if (con != null)
con.close();
}
}
輸出
執行存儲過程後是否返回結果集:false
受影響的行數:262
受影響的行:-1 ,此處返回結果集
受影響的行:262
受影響的行:262
受影響的行:-1,此處返回結果集
受影響的行:262
存儲過程
ALTER PROCEDURE GetBasics(
@PERSON_NAME VARCHAR(32),
@COUNT INT OUT
)
AS
BEGIN
SET NOCOUNT ON;
update TABLE_A SET NAME='aa';
SELECT @COUNT = COUNT(*) FROM TABLE_A;
update TABLE_A SET NAME='aa';
SELECT * FROM TABLE_A;
update TABLE_A SET NAME='aa';
update TABLE_A SET NAME='aa';
SELECT * FROM ORGS;
update TABLE_A SET NAME='aa';
END
GO