Olá GUJ!
Bom, eu estou tendo problemas em inserir linhas em uma tabela usando o banco de dados H2. Estou até usando um exemplo do livro do Deitel. Ah, e isso não ocorre só quando vou inserir, mas quando vou criar tabelas também. Olha só o que acontece quando tento inserir algo:
org.h2.jdbc.JdbcSQLException: Method is only allowed for a query. Use execute or executeUpdate instead of executeQuery; SQL statement:
insert into pessoa (nome , idade) values ('teste2' , 12) [90002-153]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
at org.h2.message.DbException.get(DbException.java:167)
at org.h2.message.DbException.get(DbException.java:144)
at org.h2.message.DbException.get(DbException.java:133)
at org.h2.command.Prepared.query(Prepared.java:212)
at org.h2.command.CommandContainer.query(CommandContainer.java:78)
at org.h2.command.Command.executeQuery(Command.java:181)
at org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:76)
at util.TabelaModelo.inserirConsulta(TabelaModelo.java:217)
at senac.SProgramas.actionPerformed(SProgramas.java:123)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Meu código para executar SQL está assim:
public void inserirConsulta(String query)
{
// Se estiver conectado ao banco
if(this.bancoConectado)
{
try
{
// Realiza consulta
this.resultSet = this.statement.executeQuery(query);
// Obtém meta dados
this.metaData = this.resultSet.getMetaData();
// Move cursor da "tabela" do objeto ResultSet para última linha
this.resultSet.last();
// Utiliza objeto ResultSet movido para última linha para adquirir número de linhas em tabela
this.linhas = this.resultSet.getRow();
// Notifica a JTable que está usando este modelo que o modelo foi alterado
this.fireTableStructureChanged();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
// Se não estiver
else
{
System.out.println("Banco de dados não conectado em método inserirConsulta() da classe TabelaModelo.");
throw new IllegalStateException();
}
}
Lá na guia de erros do H2, tem dizendo isso:
"METHOD_ONLY_ALLOWED_FOR_QUERY = 90002
The error with code 90002 is thrown when Statement.executeQuery() was called for a statement that does not return a result set (for example, an UPDATE statement). This is not allowed according to the JDBC specs."
Mas caramba, o executeQuery retorna um objeto ResultSet. O que devo fazer?

