Advertisement
Guest User

Untitled

a guest
Oct 8th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. package neo4J;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.Scanner;
  13.  
  14. public class Neo4J {
  15.  
  16.     private static final String PASSWORD = "password";
  17.     private static final String USER = "neo4j";
  18.  
  19.     @SuppressWarnings("unused")
  20.     private static void borrarLaBBDD() throws SQLException {
  21.         ejecuta("match(n) detach delete n");
  22.     }
  23.  
  24.     private static void consulta1() throws SQLException {
  25.         System.out.println(
  26.                 "Listar todos los médicos Cuyo nombre comience por una subcadena dada\nIntroduce la subcadena");
  27.         String s = leeString();
  28.         Connection con = getConnection();
  29.         PreparedStatement pS = con
  30.                 .prepareStatement("match (n:medico) where n.nombre starts with ? return n.nombre, n.apellido");
  31.         pS.setString(1, s);
  32.         ResultSet rS = pS.executeQuery();
  33.         while (rS.next()) {
  34.             System.out.println(
  35.                     "Médico:\n\tnombre: " + rS.getString("n.nombre") + "\n\tapellido: " + rS.getString("n.apellido"));
  36.         }
  37.         rS.close();
  38.         pS.close();
  39.         con.close();
  40.     }
  41.  
  42.     private static void consulta2() throws SQLException {
  43.         System.out.println("Número de pacientes atendidos por el médico introducido"
  44.                 + "\nIntroduce el nombre del médico:" + "\nIntroduce los apellidos del médico");
  45.         String s = leeString();
  46.         Connection con = getConnection();
  47.         PreparedStatement pS = con
  48.                 .prepareStatement("match (n:medico)-[r:ATIENDE_A]->(p:paciente) where n.nombre = ? return count(r)");
  49.         pS.setString(1, s);
  50.         ResultSet rS = pS.executeQuery();
  51.         while (rS.next()) {
  52.             System.out.println("El número de pacientes atendidos por " + s + " es: " + rS.getInt(1));
  53.         }
  54.         rS.close();
  55.         pS.close();
  56.         con.close();
  57.     }
  58.  
  59.     private static void consulta3() throws SQLException {
  60.         System.out.println(
  61.                 "Listar todos los médicos que hayan atendido a un número mínimo de pacientes\nIntroduzca el número mínimo:");
  62.         int n = leeEntero();
  63.         Connection con = getConnection();
  64.         PreparedStatement pS = con.prepareStatement("match (n:medico)-[r:ATIENDE_A]->(p:paciente) "
  65.                 + "with n.nombre as nombre, n.apellido as apellido, count(r) as numero " + "where numero >= ? "
  66.                 + "return nombre, apellido, numero");
  67.         pS.setInt(1, n);
  68.         ResultSet rS = pS.executeQuery();
  69.         while (rS.next()) {
  70.             System.out.println("Nombre: " + rS.getString("nombre") + "\nApellido: " + rS.getString("apellido")
  71.                     + "\nNúmero de pacientes atendidos: " + rS.getString("numero") + "\n\n");
  72.         }
  73.  
  74.     }
  75.  
  76.     private static void ejecuta(String readLine) throws SQLException {
  77.         Connection con = getConnection();
  78.         Statement sT = con.createStatement();
  79.         sT.executeUpdate(readLine);
  80.         sT.close();
  81.         con.close();
  82.     }
  83.  
  84.     public static Connection getConnection() throws SQLException {
  85.         return DriverManager.getConnection("jdbc:neo4j:bolt://localhost", USER, PASSWORD);
  86.     }
  87.  
  88.     @SuppressWarnings("unused")
  89.     private static void imprimeNumeroDeNodos() throws SQLException {
  90.         Connection con = getConnection();
  91.         Statement sT = con.createStatement();
  92.         ResultSet rS = sT.executeQuery("match(n) return count(n)");
  93.         rS.next();
  94.         System.out.println(rS.getString(1));
  95.         sT.close();
  96.         con.close();
  97.     }
  98.  
  99.     @SuppressWarnings("resource")
  100.     private static int leeEntero() {
  101.         return new Scanner(System.in).nextInt();
  102.     }
  103.  
  104.     @SuppressWarnings("resource")
  105.     private static String leeString() {
  106.         return new Scanner(System.in).nextLine();
  107.     }
  108.  
  109.     public static void main(String[] args) throws IOException, SQLException {
  110.         // meterDatos();
  111.         try {
  112.             consulta1(); // Médicos cuyo nombre comience por ?
  113.             consulta2(); // Número de pacientes atendidos por el médico ?
  114.             consulta3(); // Médicos que hayan atendidos a N o más pacientes ?
  115.         } catch (SQLException e) {
  116.             e.printStackTrace();
  117.         }
  118.         // borrarLaBBDD();
  119.     }
  120.  
  121.     @SuppressWarnings("unused")
  122.     private static void meterDatos() throws IOException, SQLException {
  123.         BufferedReader bR = new BufferedReader(new FileReader("inserciones"));
  124.         while (bR.ready()) {
  125.             String readLine = bR.readLine();
  126.             if (readLine.charAt(0) != '/') {
  127.                 ejecuta(readLine);
  128.             }
  129.         }
  130.         bR.close();
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement