Advertisement
Guest User

Untitled

a guest
Nov 29th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1.  
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. /*
  11. * To change this license header, choose License Headers in Project Properties.
  12. * To change this template file, choose Tools | Templates
  13. * and open the template in the editor.
  14. */
  15.  
  16. /**
  17. *
  18. * @author sergioelementary
  19. */
  20. public class Ej5_consultaSQLite {
  21. static String sqlite_jdbd_driver="org.sqlite.JDBC";
  22. static String prefix="jdbc:"+"sqlite:";
  23. static String hostName="";
  24. static String urlFolder="/home/sergioelementary/ZEjemploBD_AD/";
  25. static String dbName="ejemplo1.db";
  26.  
  27.  
  28.  
  29. static String driver=sqlite_jdbd_driver;
  30.  
  31. static String user=""; //"user";
  32. static String password="";
  33.  
  34. static String url=prefix+hostName+urlFolder+dbName;
  35.  
  36. public static void main(String[] args) {
  37. statementQueryExample();
  38.  
  39.  
  40. }
  41.  
  42. public static void statementQueryExample()
  43. // para cada NOMBRE de departamento el salario medio de sus empleados
  44. // y el Salario del empleado con mayor salario
  45. { String query="SELECT d.dnombre,"
  46. + "(SELECT AVG(salario)from empleados where dept_no=d.dept_no) salarioMedio,"
  47. + "(SELECT apellido FROM empleados WHERE dept_no=d.dept_no AND salario=(SELECT MAX(salario)from empleados where dept_no=d.dept_no)),"
  48. + "(SELECT MAX(salario)from empleados where dept_no=d.dept_no) salarioMax "
  49. + "FROM empleados e,departamentos d WHERE d.dept_no=e.dept_no GROUP BY d.dnombre";// GROUP BY para que muestre 1 por departamento
  50.  
  51. try{
  52. //Load the driver in RAM
  53. Class.forName(driver);
  54.  
  55. //Connect to DB
  56. Connection connection=DriverManager.getConnection(url,user,password);
  57.  
  58. Statement statement=connection.createStatement();
  59. ResultSet result=statement.executeQuery(query);
  60.  
  61. String departamento="";
  62. //Iterate on the 'ResultSet' to process each row
  63. while(result.next()){//There are still rows to get
  64.  
  65. System.out.println(result.getString(1)+"\t salario medio= "+result.getString(2)+"\t Apellido maxSalario= "+result.getString(3)
  66. +"\t"+result.getString(4));
  67. }
  68.  
  69. result.close(); //close ResultSet
  70. statement.close();//close Statement
  71. connection.close();//close Connection
  72.  
  73. }catch(ClassNotFoundException cnfe){
  74. System.out.printf("Not found the jdbc driver %s\n", driver);
  75. }catch (SQLException sqle){
  76. System.out.println("SQL Exception");
  77. }
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement