错误:预期的类,接口或枚举(实现java)

时间:2019-02-19 18:28:49

标签: java sqlite implements

我一直在练习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 :(得分:1)

这里:

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";