· 7 years ago · Feb 19, 2019, 07:42 PM
1./Jbdc.java:18: error: introducir() in Jbdc cannot implement introducir() in InterfazBD
2public void introducir() throws Exception
3
4overridden method does not throw Exception
5 1 error
6
7(Principal)
8public class App{
9
10public static void main(String arg[]){
11
12 JsonRead json = new JsonRead();
13 Jbdc sqlite = new Jbdc();
14 grabarJson(json);
15 introducirSQL(sqlite);
16}
17
18 public static void grabarJson(InterfazGrabar fichero){
19 fichero.grabar();
20 }
21
22 public static void grabarTxt(InterfazGrabar fichero){
23 fichero.grabar();
24 }
25
26 public static void introducirSQL(InterfazBD fichero){
27 fichero.introducir();
28 }
29}
30
31Jbdc (this file enter the data in a database)
32
33public class Jbdc implements InterfazBD{
34
35private static final String URL = "jdbc:sqlite:test.db";
36 public void introducir() throws Exception{
37 createDb();
38 createTable();
39 Aula a = null;
40 int contador = 0;
41 try {
42 FileInputStream inFile = new FileInputStream("aula.dat");
43 ObjectInputStream in = new ObjectInputStream(inFile);
44 while (inFile.available()>0) {
45 a = (Aula)in.readObject();
46 String materiaslista ="";
47 String nombre = a.getNombre();
48 String grupo = a.getGrupo();
49 int tutor= a.getTutor();
50 ArrayList<String> materias = a.getMaterias();
51 for (int counter = 0; counter < materias.size(); counter++) {
52 materiaslista = materiaslista + materias.get(counter) + ",";
53 }
54 insertDatos(nombre,grupo,tutor,materiaslista);
55 }
56
57 }
58 catch(IOException e)
59 {
60 System.err.println("ERROR");
61 }
62 System.out.println("¡Listo!");
63 }
64
65 private static void insertDatos(String nombre,String grupo, int tutor,String materiaslista) {
66 final String SQL = "INSERT INTO datos VALUES(?,?,?,?)";
67 try (Connection con = getConnection(); PreparedStatement ps = con.prepareStatement(SQL);) {
68 ps.setString(1, nombre);
69 ps.setString(2, grupo);
70 ps.setInt(3, tutor);
71 ps.setString(4, materiaslista);
72 ps.executeUpdate();
73 } catch (SQLException e) {
74 e.printStackTrace();
75 }
76 }
77
78 private static void createTable() {
79 final String SQL = "CREATE TABLE IF NOT EXISTS datos (nombre TEXT,grupo TEXT, tutor INTEGER, materiaslista TEXT);";
80 // This SQL Query is not "dynamic". Columns are static, so no need to use
81 // PreparedStatement.
82 try (Connection con = getConnection(); Statement statement = con.createStatement();) {
83 statement.executeUpdate(SQL);
84 } catch (SQLException e) {
85 e.printStackTrace();
86 }
87 }
88
89 private static void createDb() {
90 try (Connection conn = getConnection()) {
91 if (conn != null) {
92 conn.getMetaData();
93 }
94 } catch (SQLException e) {
95 e.printStackTrace();
96 }
97 }
98
99 public static Connection getConnection() throws SQLException {
100 return DriverManager.getConnection(URL);
101 }
102}