Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ejercicio4 {
- public static void main(String[] args){
- try{
- //MySQL
- Class.forName("com.mysql.jdbc.Driver");
- Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost/ejemplo","ejemplo", "ejemplo");
- String drop="DROP FUNCTION IF EXISTS Loz";
- String proc=
- "delimiter //n"+
- "CREATE FUNCTION Loz(nomEquipo VARCHAR(20)) n"+
- "RETURNS INT n"+
- "BEGIN n"+
- " DECLARE numJugadores INT; n"+
- " SET numJugadores=0; n"+
- " select count(codigo) into numJugadores from jugadores where nombre_equipo=nomEquipo; n"+
- " RETURN numJugadores; n"+
- "ENDn"+
- "//";
- Statement sentencia=conexion.createStatement();
- sentencia.executeUpdate(drop);
- int filas=sentencia.executeUpdate(proc);
- System.out.println("salida: "+ filas);
- //recuperar parametros main
- String nom=args[0];
- //construir orden de llamada
- String sql="{?=call Loz(?)}";
- //Preparamos la llamada
- CallableStatement llamada=conexion.prepareCall(sql);
- //valor devuelto-->parametro de salida OUT
- llamada.registerOutParameter(1, Types.INTEGER);
- //valor que recibe-->parametro de entrada INT
- llamada.setString(2, nom);
- //ejecutar el procedimiento
- llamada.executeUpdate();
- //mostrar por pantalla valores devueltos
- System.out.println("Equipo: "+nom+", Numero de jugadores: ==>"+llamada.getInt(1));
- llamada.close();
- conexion.close();
- }
- catch (ClassNotFoundException cn) {
- cn.printStackTrace();
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement