日期:2014-05-16 浏览次数:20509 次
package procudureTest; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class BaseDB { private String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private String url = "jdbc:sqlserver://localhost:1433;DatabaseName=CityInfo"; private String user = "sa"; private String pwd = "123456"; private Connection con = null; public BaseDB() { try { Class.forName(driverClass); con = DriverManager.getConnection(url, user, pwd); } catch (ClassNotFoundException e) { System.out.println("加载驱动失败"); e.printStackTrace(); } catch (SQLException e) { System.out.println("获得连接失败"); e.printStackTrace(); } } // 调用储存过程插入记录 public void insertInfoTest() { try { String callSql = "{call pc_insertInfo(?,?,?,?)}"; CallableStatement call = con.prepareCall(callSql); call.setInt(1, 100); call.setString(2, "title6"); call.setString(3, "content6"); call.setString(4, "linkman6"); call.execute(); } catch (SQLException e) { System.out.println("调用存储过程失败"); e.printStackTrace(); } } // 调用储存过程获得记录 public void showInfoTest() { String callSql = "{call pc_selectInfo(?,?,?,?)}"; try { CallableStatement call = con.prepareCall(callSql); call.setInt(1, 101); // 注册输出的参数,可以使用序列号,或命名参数,对应储存过程中的输出参数名称 call.registerOutParameter(2, java.sql.Types.VARCHAR); call.registerOutParameter(3, java.sql.Types.VARCHAR); call.registerOutParameter(4, java.sql.Types.VARCHAR); call.execute(); String type = call.getString(2); String title = call.getString(3); String content = call.getString(4); System.out.println("type:" + type); System.out.println("title:" + title); System.out.println("content:" + content); } catch (SQLException e) { System.out.println("调用pc_selectInfo储存过程出错"); e.printStackTrace(); } } public static void main(String args[]) { BaseDB db = new BaseDB(); // db.insertInfoTest(); db.showInfoTest(); } }
?