My program does not insert data in Mariadb

0
 import java.nio.channels.SelectableChannel;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.JOptionPane;

public class Test {
    static LinkedList<AtencionMédica> lAtencionMédicas;

    public static void main(String[] args) {
        System.out.println("MENU");
        System.out.println("1.Ver atenciones medicas \n2.Agregar atencion medica");
        Scanner scanner = new Scanner(System.in);
        short opc = scanner.nextShort();
        switch (opc) {
        case 2:
            agregarConsulta();
            break;

        default:
            break;
        }
    }

    public static void agregarConsulta() {
        try {
            Scanner leer = new Scanner(System.in);
            int opc, opc2;
            Connection connection = DriverManager.getConnection(
                    "jdbc:mariadb://localhost:3306/bdclinica?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC",
                    "root", "root");
            Statement statement = connection.createStatement();
            ResultSet resultados = statement.executeQuery("select idMedico, nombreMedico, apellidoMedico from medico");

            System.out.println("Escoja medico");
            while (resultados.next()) {
                System.out.println(resultados.getInt(1) + " " + resultados.getString(2) + " " + resultados.getString(3));

            }
            opc = leer.nextInt();

            resultados = statement.executeQuery("select idPaciente, nombrePaciente, apellidoPaciente from paciente");
            System.out.println("Escoja paciente");
            while (resultados.next()) {
                System.out.println(resultados.getInt(1) + " " + resultados.getString(2) + " " + resultados.getString(3));

            }
            opc2 = leer.nextInt();

            System.out.println("Agregue fecha atencion (YYYY-MM-DD)");
            String fecha = leer.next();

            PreparedStatement pStatement = connection.prepareStatement("insert into atencionmedica values (?,?,?);");

            pStatement.setInt(1, opc);
            pStatement.setInt(2, opc2);
            pStatement.setString(3, fecha);
            pStatement.executeUpdate();
            System.out.println("Listo");

        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());        
        }
    }
}

My program works fine until it reads the date from the keyboard, then the console is "running", I do not know that it may not add anything to the database. I have tried changing the date string to Date type but nothing happens either. The medical attention table has 3 fields, id Patient (int) (primary key), idMedic (int), date (Date)

    
asked by CHRISTIAN MARCELO HERRERA ROJA 24.11.2018 в 00:14
source

0 answers