Advertisement
Guest User

Untitled

a guest
Nov 29th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.27 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package Ej8;
  7.  
  8. import java.sql.CallableStatement;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11.  
  12. import java.sql.SQLException;
  13. import java.sql.Types;
  14. import java.util.Scanner;
  15.  
  16. /** -Eliminar departamento y todos sus empleados, dado el código de departamento
  17. * delimiter //
  18. CREATE OR REPLACE PROCEDURE eliminar_ej8 (IN ndept INT)
  19.  
  20. BEGIN
  21. DELETE FROM departamentos WHERE dept_no=ndept;
  22. END //
  23. delimiter ;
  24. *
  25. *
  26. -Obtener la suma de los salarios de un departamento dado su código
  27. * CREATE OR REPLACE PROCEDURE sumaSal_ej8 (d INT, OUT sumaSal FLOAT)
  28. * BEGIN
  29. * SELECT SUM(salario) into sumaSal FROM empleado WHERE dept_no=d;
  30. * END //
  31. * delimiter;
  32.  
  33. * -Cambiar la localización de un departamento, dado el código del departamento y la nueva ubucación Hay que devolver la
  34. antigua localización y un valor booleano que indique si ha cambiado la localización
  35. *
  36. delimiter //
  37. CREATE OR REPLACE PROCEDURE nuevaLoc_ej8 (d INT,nuevaLoc VARCHAR(15) , INOUT antiguaLoc VARCHAR(15))
  38. BEGIN
  39. SELECT loc into antiguaLoc FROM departamentos WHERE dept_no=d;
  40.  
  41. UPDATE departamentos set loc=nuevaLoc;
  42. END //
  43. delimiter ;
  44.  
  45. Utilizando objetos CallableStatement, realizar una aplicación en java que muestre un menú que permita realizar las acciones
  46. indicadas
  47. *
  48. * @author sergioelementary
  49. */
  50. public class Ejercicio8_CallableStatement {
  51. //---------------PARA SQL--------------------------------//
  52.  
  53. static String dbName="pruebaAD";
  54. static String parameters="";
  55.  
  56. static String MySQL_jdbcDriver="com.mysql.jdbc.Driver";
  57. static String protocol="jdbc:"+"mysql:";
  58. static String hostName="localhost";
  59.  
  60. //static parameters= "noAccessToPocedureBodies=true";
  61. static String MySQL_url="jdbc:mysql://localhost/"+dbName;
  62.  
  63.  
  64.  
  65.  
  66.  
  67. //Actual DB parameters
  68. static String driver=MySQL_jdbcDriver;
  69. static String url=MySQL_url;
  70.  
  71. static String user="root";
  72. static String password="";
  73.  
  74. //---------------------SQLITE---------------------
  75. /*
  76. static String sqlite_jdbd_driver="org.sqlite.JDBC";
  77. static String prefix="jdbc:"+"sqlite:";
  78. static String hostName="";
  79. static String urlFolder="/home/sergioelementary/ZEjemploBD_AD/";
  80. static String dbName="ejemplo1.db";
  81.  
  82.  
  83.  
  84. static String driver=sqlite_jdbd_driver;
  85.  
  86. static String user="";
  87. static String password="";
  88.  
  89. static String url=prefix+hostName+urlFolder+dbName;
  90. */
  91.  
  92. public static void main(String[] args) {
  93. Scanner sc = new Scanner(System.in);
  94.  
  95. System.out.println("Introduzca una opcion");
  96. System.out.println("1 Eliminar departamento y todos sus empleados, dado el código de departamento");
  97. System.out.println("2 Obtener la suma de los salarios de un departamento dado su código");
  98. System.out.println("3 Cambiar la localización de un departamento, dado el código del departamento y la nueva ubucación");
  99.  
  100. int opc=sc.nextInt();
  101.  
  102. switch (opc){
  103. case 1:
  104. EliminarProcedureExample();
  105. break;
  106. case 2:
  107. ObtenerSumaDepartamento();
  108. break;
  109. case 3:
  110. CambiarLoc();
  111. break;
  112. default:
  113. System.out.println("No existe esa opcion.");
  114. break;
  115.  
  116. }
  117. //procedureOutExample(args);
  118.  
  119. //procedureInOutExample();
  120. //functionExample(args);
  121. }
  122.  
  123. public static void EliminarProcedureExample()
  124. {
  125.  
  126. try{
  127. Scanner sc2=new Scanner (System.in);
  128.  
  129.  
  130. //Load the driver in RAM
  131. Class.forName(driver);
  132.  
  133. //Connect to DB
  134. Connection connection=DriverManager.getConnection(url,user,password);
  135.  
  136. //Get command line parameters and create a call statement string
  137. System.out.println("Introduzca el cod de departamento");
  138.  
  139. int dept=sc2.nextInt();
  140. //es un procedimiento porque no tiene nada que devolver.
  141.  
  142. String sql="{ call eliminar_ej8 (?)}";
  143.  
  144. //Create the CallableStatement object
  145.  
  146. CallableStatement call= connection.prepareCall(sql);
  147.  
  148. //Set values to parameters
  149.  
  150. call.setInt(1, dept);
  151.  
  152.  
  153.  
  154. //Execute procedure
  155.  
  156. int numeroDeFilasBorradas=call.executeUpdate();
  157.  
  158.  
  159. System.out.println("Eliminacion realizada");
  160.  
  161. call.close();
  162. connection.close();
  163.  
  164. System.out.println("Filas borradas = "+numeroDeFilasBorradas);
  165.  
  166.  
  167.  
  168. }catch(ClassNotFoundException cnfe){
  169. System.out.printf("Not found the jdbc driver %s\n", driver);
  170. }catch (SQLException sqle){
  171. System.out.println("SQL Exception");
  172. }
  173. }
  174.  
  175. public static void ObtenerSumaDepartamento(){
  176.  
  177. try{
  178. Scanner sc2=new Scanner (System.in);
  179.  
  180.  
  181. //Load the driver in RAM
  182. Class.forName(driver);
  183.  
  184. //Connect to DB
  185. Connection connection=DriverManager.getConnection(url,user,password);
  186.  
  187. //Get command line parameters and create a call statement string
  188. System.out.println("Introduzca el cod de departamento");
  189.  
  190. int dept=sc2.nextInt();
  191. //es un procedimiento porque no tiene nada que devolver.
  192.  
  193. // hacer la variable que vamos a devolver INOUT para poder recogerlo
  194. String sql="{ call sumaSal_ej8 (?,?)}";
  195.  
  196.  
  197. //Create the CallableStatement object
  198.  
  199. CallableStatement call= connection.prepareCall(sql);
  200.  
  201. //Register OUT parameters and set IN parameters
  202.  
  203. // ¡IMPORTANTE!
  204.  
  205. call.setInt(1,dept);
  206. // los INOUT no hace falta ponerle valor y actuan como out.
  207. call.setDouble(2, 0.0);
  208. call.registerOutParameter(2, Types.DOUBLE);
  209.  
  210. //Execute function
  211.  
  212. call.executeUpdate();
  213.  
  214.  
  215.  
  216.  
  217. double sumaSalarioEmpleados=call.getDouble(2);
  218.  
  219.  
  220. System.out.println("Consulta realizada\n");
  221.  
  222. call.close();
  223. connection.close();
  224.  
  225. System.out.println("Suma del salario de los empleados de ese departamento = "+sumaSalarioEmpleados);
  226.  
  227.  
  228.  
  229. }catch(ClassNotFoundException cnfe){
  230. System.out.printf("Not found the jdbc driver %s\n", driver);
  231. }catch (SQLException sqle){
  232. System.out.println("SQL Exception");
  233. }
  234.  
  235.  
  236. }
  237.  
  238. public static void CambiarLoc(){
  239. try{
  240. Scanner sc2=new Scanner (System.in);
  241.  
  242.  
  243. //Load the driver in RAM
  244. Class.forName(driver);
  245.  
  246. //Connect to DB
  247. Connection connection=DriverManager.getConnection(url,user,password);
  248.  
  249. //Get command line parameters and create a call statement string
  250. System.out.println("Introduzca el cod de dep departamento");
  251.  
  252. int dept=sc2.nextInt();
  253.  
  254. System.out.println("Introduzca el localizacion dep departamento");
  255. sc2.nextLine();
  256. String localizacion=sc2.nextLine();
  257.  
  258. //es un procedimiento porque no tiene nada que devolver.
  259.  
  260. // hacer la variable que vamos a devolver INOUT para poder recogerlo
  261. String sql="{call nuevaLoc_ej8 (?,?,?)}";
  262.  
  263.  
  264. //Create the CallableStatement object
  265.  
  266. CallableStatement call= connection.prepareCall(sql);
  267.  
  268. //Register OUT parameters and set IN parameters
  269.  
  270. // ¡IMPORTANTE!
  271.  
  272. call.setInt(1,dept);
  273. call.setString(2, localizacion);
  274. // los INOUT no hace falta ponerle valor y actuan como out.
  275. //call.setString(3,"");
  276. call.registerOutParameter(3, Types.VARCHAR);
  277.  
  278. //Execute function
  279.  
  280. int numeroFilasAfectadas=call.executeUpdate();
  281.  
  282.  
  283.  
  284.  
  285. String antiguaLoc=call.getString(3);
  286.  
  287. boolean actualizacionRealizada=false;
  288. if(numeroFilasAfectadas>0){actualizacionRealizada=true;}
  289.  
  290. System.out.println("Actualizacion realizada= "+actualizacionRealizada+"\n");
  291.  
  292. call.close();
  293. connection.close();
  294.  
  295. System.out.println("Antigua loc= "+antiguaLoc);
  296.  
  297.  
  298.  
  299.  
  300.  
  301. }catch(ClassNotFoundException cnfe){
  302. System.out.printf("Not found the jdbc driver %s\n", driver);
  303. }catch (SQLException sqle){
  304. System.out.println("SQL Exception");
  305. }
  306.  
  307.  
  308. }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement