Java调用存储过程

存储过程
create procedure add_mobilenum
		@numberbegin numeric(11, 0),
		@numberend numeric(11, 0),
		@mobile_type varchar(20)
		as 
		declare @i  numeric(11, 0)
		set @i=@numberbegin
		while @i<@numberend+1
		begin
		insert into t_mobiles values(@i,@mobile_type,'','Y')
		set @i=@i+1
		end
		
		go
		exec  add_mobilenum  10000000000,11111111111,'SIM'
		
		drop procedure add_mobilenum
		
		select * from t_mobiles
		
		delete from t_mobiles


public boolean addnum(long numbegin,long numend,String mobile_type  ) {
		boolean flag = false;
		try {

			Connection conn = DBUtil.getConnection();
			CallableStatement cstmt =conn.prepareCall("{call add_mobilenum(?,?,?)}");
			cstmt.setLong(1, numbegin);
			cstmt.setLong(2, numend);
			cstmt.setString(3, mobile_type);
			if(!cstmt.execute()){
				flag = true;
			}

			DBUtil.closeConnection(conn);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return flag;
	}

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
public class DBUtil 
{
	private static String driverString = "net.sourceforge.jtds.jdbc.Driver";
	private static String connString = "jdbc:jtds:sqlserver://localhost:1433/cnc";
	private static String username = "sa";
	private static String password = "";
	
	public static Connection getConnection()
	{
		Connection conn = null;
		try {
			Class.forName(driverString);
			
			conn = DriverManager.getConnection(connString,username,password);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return conn;
	}
		
	public static void closeConnection(Connection conn)
	{
		try
		{
			conn.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

	
}



阅读更多
个人分类: Java技术
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭