Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. public class Ejercicio4 {
  2. public static void main(String[] args){
  3. try{
  4. //MySQL
  5. Class.forName("com.mysql.jdbc.Driver");
  6. Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost/ejemplo","ejemplo", "ejemplo");
  7.  
  8. String drop="DROP FUNCTION IF EXISTS Loz";
  9.  
  10. String proc=
  11. "delimiter //n"+
  12. "CREATE FUNCTION Loz(nomEquipo VARCHAR(20)) n"+
  13. "RETURNS INT n"+
  14. "BEGIN n"+
  15. " DECLARE numJugadores INT; n"+
  16. " SET numJugadores=0; n"+
  17. " select count(codigo) into numJugadores from jugadores where nombre_equipo=nomEquipo; n"+
  18. " RETURN numJugadores; n"+
  19. "ENDn"+
  20. "//";
  21.  
  22. Statement sentencia=conexion.createStatement();
  23. sentencia.executeUpdate(drop);
  24. int filas=sentencia.executeUpdate(proc);
  25. System.out.println("salida: "+ filas);
  26.  
  27. //recuperar parametros main
  28. String nom=args[0];
  29.  
  30. //construir orden de llamada
  31. String sql="{?=call Loz(?)}";
  32.  
  33. //Preparamos la llamada
  34. CallableStatement llamada=conexion.prepareCall(sql);
  35.  
  36. //valor devuelto-->parametro de salida OUT
  37. llamada.registerOutParameter(1, Types.INTEGER);
  38.  
  39. //valor que recibe-->parametro de entrada INT
  40. llamada.setString(2, nom);
  41.  
  42. //ejecutar el procedimiento
  43. llamada.executeUpdate();
  44.  
  45. //mostrar por pantalla valores devueltos
  46. System.out.println("Equipo: "+nom+", Numero de jugadores: ==>"+llamada.getInt(1));
  47.  
  48. llamada.close();
  49. conexion.close();
  50. }
  51. catch (ClassNotFoundException cn) {
  52. cn.printStackTrace();
  53. }
  54. catch (SQLException e) {
  55. e.printStackTrace();
  56. }
  57. catch(Exception e){
  58. e.printStackTrace();
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement