Haber si alguien consigue ver en que estoy fallando, soy bastante nuevo en java y no tengo mucha idea por eso no se por donde cogerlo. Os dejo los codigos que he usado.
Creación de Tablas y Datos
Código:
public class Derby {
public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
public static final String JDBC_URL = "jdbc:derby:zadb;create=true";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName(DRIVER);
Connection connection = DriverManager.getConnection(JDBC_URL);
connection.createStatement().execute("create table channels(channel varchar(20),topic varchar(20), videoclip varchar(20))");
connection.createStatement().execute("insert into channels values "
+ "('oodp', 'creational', 'singleton'),"
+ "('oodp', 'creational', 'factory method'),"
+ "('oodp', 'creational', 'abstract factory')");
System.out.println("Channels table created and records successfully inserted...");
}
}
Consulta a base de datos
Código:
public class QueryDB {
public static final String SQL_STATEMENT = "select * from channels";
public static void main(String[] args) throws SQLException {
Connection connection = DriverManager.getConnection(Derby.JDBC_URL);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SQL_STATEMENT);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnCount = resultSetMetaData.getColumnCount();
for (int x = 1; x <= columnCount; x++) System.out.format("%20s", resultSet.getString(x) + " | ");
while (resultSet.next()){
System.out.println("");
for (int x = 1; x <= columnCount;x++) System.out.format("%20s", resultSet.getString(x) + " | ");
}
if (statement != null) statement.close();
if (connection != null) connection.close();
}
}
Error que me tira
Código:
Exception in thread "main" java.sql.SQLException: Estado del cursor no válido: sin fila actual.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.newSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedResultSet.checkOnRow(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedResultSet.getColumnType(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedResultSet.getString(Unknown Source)
at derby.QueryDB.main(QueryDB.java:19)
Caused by: ERROR 24000: Estado del cursor no válido: sin fila actual.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 10 more
Java Result: 1
Muchas gracias de antemano a todos por prestarme atención.