ошибка: ожидается класс, интерфейс или перечисление (реализовать Java) - PullRequest
0 голосов
/ 19 февраля 2019

Я практиковал использование интерфейсов в Java, но у меня есть небольшая проблема, когда я хочу запустить один из моих классов.Это ошибка:

./Jbdc.java:18: error: introducir() in Jbdc cannot implement introducir()  in InterfazBD
public void introducir() throws Exception

overridden method does not throw Exception
 1 error

Большинство ошибок, которые мне показывает компилятор, происходят оттуда.Я говорю вам, я изучаю использование интерфейсов, и я действительно не знаю, как их правильно использовать ... поэтому я не знаю, как я мог бы решить этот тип ошибки .. это мой код:

 (Principal)
public class App{

public static void main(String arg[]){

    JsonRead json = new JsonRead();
    Jbdc sqlite = new Jbdc();
    grabarJson(json);
    introducirSQL(sqlite);
}

    public static void grabarJson(InterfazGrabar fichero){
    fichero.grabar();
    }

    public static void grabarTxt(InterfazGrabar fichero){
    fichero.grabar();
    }

    public static void introducirSQL(InterfazBD fichero){
    fichero.introducir();
    }
}

и файл Jbdc

 Jbdc (this file enter the data in a database)

public class Jbdc implements InterfazBD{

private static final String URL = "jdbc:sqlite:test.db";
    public void introducir() throws Exception{
        createDb();
        createTable();
        Aula a = null;
        int contador = 0;
        try {
            FileInputStream inFile = new FileInputStream("aula.dat");
            ObjectInputStream in = new ObjectInputStream(inFile);
            while (inFile.available()>0) {
                a = (Aula)in.readObject();
                String materiaslista ="";
                String nombre = a.getNombre();
                String grupo = a.getGrupo();
                int tutor= a.getTutor();
                ArrayList<String> materias = a.getMaterias();
                for (int counter = 0; counter < materias.size(); counter++) {             
                    materiaslista = materiaslista + materias.get(counter) + ",";
                }
                insertDatos(nombre,grupo,tutor,materiaslista);
            }

    }
    catch(IOException e)
    {
        System.err.println("ERROR");
    }
        System.out.println("¡Listo!");
    }

    private static void insertDatos(String nombre,String grupo, int tutor,String materiaslista) {
        final String SQL = "INSERT INTO datos VALUES(?,?,?,?)";
        try (Connection con = getConnection(); PreparedStatement ps = con.prepareStatement(SQL);) {
            ps.setString(1, nombre); 
            ps.setString(2, grupo);
            ps.setInt(3, tutor);
            ps.setString(4, materiaslista); 
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void createTable() {
        final String SQL = "CREATE TABLE IF NOT EXISTS datos (nombre TEXT,grupo TEXT, tutor INTEGER, materiaslista TEXT);";
        // This SQL Query is not "dynamic". Columns are static, so no need to use
        // PreparedStatement.
        try (Connection con = getConnection(); Statement statement = con.createStatement();) {
            statement.executeUpdate(SQL);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void createDb() {
        try (Connection conn = getConnection()) {
            if (conn != null) {
                conn.getMetaData();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL);
    }
}

1 Ответ

0 голосов
/ 19 февраля 2019

Здесь:

public void introducir() throws Exception{
  private static final String URL = "jdbc:sqlite:test.db";

Вы не можете объявить статическое поле внутри метода.Либо переместите объявление поля за пределы метода, либо просто сделайте его локальной переменной, поэтому либо

private static final String URL = "jdbc:sqlite:test.db";
public void introducir() throws Exception{

, либо

public void introducir() throws Exception{
  String URL = "jdbc:sqlite:test.db";
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...