Preset conditions:
Log in to SQL Server Management Studio using the SA account, right-click Security-Login-database username properties, set server role to sysadmin.
Delete A stored procedure that already exists
String job_bcp_delete_sql = "IF EXISTS (SELECT * from sys.objects WHERE name = ' procedure_bcp_out ' and type in (n ' P ', n ' PC '= conn.preparestatement (job_bcp_delete_sql);p re.execute ();
Create a stored procedure
String sql = "Set @sql = N ' BCP ' + @tableName + ' out ' + @filePath + '-c-t-u \" ' + @userName + ' \ "-P \" ' + @password + ' \ ' s \ ' + @dbIp + ' \ ' \ n '; StringBuffer Createsqlbuf=NewStringBuffer (); Createsqlbuf.append ("CREATE PROCEDURE procedure_bcp_out \ n"). Append ("@dbIp nvarchar, \ n"). Append ("@userName nvarchar, \ n"). Append ("@password nvarchar, \ n"). Append ("@tableName nvarchar, \ n"). Append ("@filePath nvarchar \ n"). Append ("As\n"). Append ("Begin\n"). Append ("EXEC master.sys.sp_configure ' show advanced options ', 1\n"). Append ("Reconfigure\n"). Append ("EXEC master.sys.sp_configure ' xp_cmdshell ', 1\n"). Append ("Reconfigure\n"). Append ("Declare @sql nvarchar \ n"). append (SQL). Append ("EXEC Master. xp_cmdshell @sql \ n "). Append ("EXEC master.sys.sp_configure ' show advanced options ', 1\n"). Append ("Reconfigure\n"). Append ("EXEC master.sys.sp_configure ' xp_cmdshell ', 0\n"). Append ("Reconfigure\n"). Append ("END"); String Createsql=createsqlbuf.tostring ();p re=conn.preparestatement (createsql);p re.execute ();
Executing stored procedures
New StringBuffer (); Execsqlbuf.append ("EXECUTE procedure_bcp_out \ n") . Append ("@dbIp =?,"). Append ("@userName =?,"). Append ("@password =?,"). Append ("@tableName =?,"). Append ("@filePath =?"). == conn.preparestatement (execsql);p re.setstring (1, Dbip);p re.setstring (2 , Dbusername);p re.setstring (3, Dbpassword);p re.setstring (4, TableName);p re.setstring ( 5, file);p Re.execute ();
SQL Server uses BCP to import and export large volumes of data