Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package neo4J;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class Neo4J {
- private static final String PASSWORD = "password";
- private static final String USER = "neo4j";
- @SuppressWarnings("unused")
- private static void borrarLaBBDD() throws SQLException {
- ejecuta("match(n) detach delete n");
- }
- private static void consulta1() throws SQLException {
- System.out.println(
- "Listar todos los médicos Cuyo nombre comience por una subcadena dada\nIntroduce la subcadena");
- String s = leeString();
- Connection con = getConnection();
- PreparedStatement pS = con
- .prepareStatement("match (n:medico) where n.nombre starts with ? return n.nombre, n.apellido");
- pS.setString(1, s);
- ResultSet rS = pS.executeQuery();
- while (rS.next()) {
- System.out.println(
- "Médico:\n\tnombre: " + rS.getString("n.nombre") + "\n\tapellido: " + rS.getString("n.apellido"));
- }
- rS.close();
- pS.close();
- con.close();
- }
- private static void consulta2() throws SQLException {
- System.out.println("Número de pacientes atendidos por el médico introducido"
- + "\nIntroduce el nombre del médico:" + "\nIntroduce los apellidos del médico");
- String s = leeString();
- Connection con = getConnection();
- PreparedStatement pS = con
- .prepareStatement("match (n:medico)-[r:ATIENDE_A]->(p:paciente) where n.nombre = ? return count(r)");
- pS.setString(1, s);
- ResultSet rS = pS.executeQuery();
- while (rS.next()) {
- System.out.println("El número de pacientes atendidos por " + s + " es: " + rS.getInt(1));
- }
- rS.close();
- pS.close();
- con.close();
- }
- private static void consulta3() throws SQLException {
- System.out.println(
- "Listar todos los médicos que hayan atendido a un número mínimo de pacientes\nIntroduzca el número mínimo:");
- int n = leeEntero();
- Connection con = getConnection();
- PreparedStatement pS = con.prepareStatement("match (n:medico)-[r:ATIENDE_A]->(p:paciente) "
- + "with n.nombre as nombre, n.apellido as apellido, count(r) as numero " + "where numero >= ? "
- + "return nombre, apellido, numero");
- pS.setInt(1, n);
- ResultSet rS = pS.executeQuery();
- while (rS.next()) {
- System.out.println("Nombre: " + rS.getString("nombre") + "\nApellido: " + rS.getString("apellido")
- + "\nNúmero de pacientes atendidos: " + rS.getString("numero") + "\n\n");
- }
- }
- private static void ejecuta(String readLine) throws SQLException {
- Connection con = getConnection();
- Statement sT = con.createStatement();
- sT.executeUpdate(readLine);
- sT.close();
- con.close();
- }
- public static Connection getConnection() throws SQLException {
- return DriverManager.getConnection("jdbc:neo4j:bolt://localhost", USER, PASSWORD);
- }
- @SuppressWarnings("unused")
- private static void imprimeNumeroDeNodos() throws SQLException {
- Connection con = getConnection();
- Statement sT = con.createStatement();
- ResultSet rS = sT.executeQuery("match(n) return count(n)");
- rS.next();
- System.out.println(rS.getString(1));
- sT.close();
- con.close();
- }
- @SuppressWarnings("resource")
- private static int leeEntero() {
- return new Scanner(System.in).nextInt();
- }
- @SuppressWarnings("resource")
- private static String leeString() {
- return new Scanner(System.in).nextLine();
- }
- public static void main(String[] args) throws IOException, SQLException {
- // meterDatos();
- try {
- consulta1(); // Médicos cuyo nombre comience por ?
- consulta2(); // Número de pacientes atendidos por el médico ?
- consulta3(); // Médicos que hayan atendidos a N o más pacientes ?
- } catch (SQLException e) {
- e.printStackTrace();
- }
- // borrarLaBBDD();
- }
- @SuppressWarnings("unused")
- private static void meterDatos() throws IOException, SQLException {
- BufferedReader bR = new BufferedReader(new FileReader("inserciones"));
- while (bR.ready()) {
- String readLine = bR.readLine();
- if (readLine.charAt(0) != '/') {
- ejecuta(readLine);
- }
- }
- bR.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement