java程序中调用存储过程

/*************************调用存储过程的主要方法********************************/
public static List<String> getcalloutrecord(long tvehicleid,long tuserid,String start_time,String endtime) {
Connection conn = null;
String testPrint = "";
List<String> list=null;
try {
list=new ArrayList<String>();
conn = getConnection();
CallableStatement proc = null;
  proc = conn.prepareCall("{ call   ptj_pandian2(?,?,?,?,?) }"); //在这里调用存储过程 ptj_pandian2是存储过程名称
  proc.setLong(1, tvehicleid);
  proc.setLong(2, tuserid);
  proc.setString(3,endtime); 
  proc.setString(4,start_time);
//1.2.3.4参数是IN类型的,5是out类型的
  proc.registerOutParameter(5, Types.VARCHAR);
  proc.execute();
  testPrint = proc.getString(5);//获取过程返回的值
  list.add(testPrint);
//   System.out.println("=存储过程返回值="+testPrint);
}   catch (SQLException e) {
try {
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return list; 
}
/***********************利用JDBC连接数据库*************************/
public static Connection getConnection() throws ClassNotFoundException, SQLException{
Connection con=null;
Class.forName("com.mysql.jdbc.Driver");
// String url="jdbc:mysql://localhost/car(数据库名称)";
// String user="root";
// String password="root";
con=DriverManager.getConnection(url,user,password);
return con;
}



























猜你喜欢

转载自0609xiaohua.iteye.com/blog/1317448