无参数
package com.javakc.test6;
import com.javakc.db.Utils;
import java.sql.CallableStatement;
import java.sql.Connection;
public class Call {
public static void call() throws Exception {
Connection connection = Utils.getConnection();
String sql = "call myproc()";
CallableStatement callableStatement = connection.prepareCall(sql);
callableStatement.execute();
connection.close();
}
public static void main(String[] args) throws Exception {
call();
}
}
输入参数
package com.javakc.test6;
import com.javakc.db.Utils;
import java.sql.CallableStatement;
import java.sql.Connection;
public class CallIn {
public static void call() throws Exception {
Connection connection = Utils.getConnection();
String sql = "call myproc_1(?)";
CallableStatement callableStatement = connection.prepareCall(sql);
callableStatement.setInt(1, 10);
callableStatement.execute();
connection.close();
}
public static void main(String[] args) throws Exception {
call();
}
}
输出参数
package com.javakc.test6;
import com.javakc.db.Utils;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
public class Callout {
public static void callOut() throws Exception {
Connection connection = Utils.getConnection();
String sql = "call myproc_2(?,?)";
CallableStatement callableStatement = connection.prepareCall(sql);
callableStatement.setInt(1, 10);
callableStatement.registerOutParameter(2, Types.INTEGER);
callableStatement.execute();
int result = callableStatement.getInt(2);
System.out.println(result);
callableStatement.close();
connection.close();
}
public static void main(String[] args) throws Exception {
callOut();
}
}
输入输出参数
package com.javakc.test6;
import com.javakc.db.Utils;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
public class CallInOut {
public static void callInOut() throws Exception {
Connection connection = Utils.getConnection();
String sql = "call myproc_3(?)";
CallableStatement callableStatement = connection.prepareCall(sql);
callableStatement.setInt(1, 10);
callableStatement.registerOutParameter(1, Types.INTEGER);
callableStatement.execute();
int result = callableStatement.getInt(1);
System.out.println(result);
callableStatement.close();
connection.close();
}
public static void main(String[] args) throws Exception {
callInOut();
}
}