TheJayyBe Posted April 23, 2012 at 04:36 AM Report #450651 Posted April 23, 2012 at 04:36 AM Meu problema é ao executar o método executeUpdate() da classe PreparedStatement, ele retorna o erro: Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1. Eis o código: 😉 */ package salão; import java.sql.*; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author josue */ public class Conexao { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mydb"; static private String query = null; static private Connection connection= null; static private Statement statement=null; static private PreparedStatement preparedstatement=null; Conexao(){ System.out.println("Conectado com sucesso"); try { Class.forName(JDBC_DRIVER); connection = DriverManager.getConnection(DATABASE_URL,"root","root23"); statement = connection.createStatement(); } catch (SQLException sqlException) { Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, sqlException); System.out.print("Erro conexão/Query SQL"); } catch (ClassNotFoundException ex) { Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex); System.out.print("Erro de classe não encontrada"); } } static void insert(String nomeTabela, ArrayList campos, ArrayList valores) throws SQLException { for(int i=0;i<campos.size();i++){ if(i==0) query= "Insert into " + nomeTabela + "(" + campos.get(i).toString() +","; else if(i==campos.size()-1) { query+=campos.get(i).toString() + ")"; } else { query+=campos.get(i).toString() + ","; } } query+=" values("; //COM INTERROGAÇÕES// for(int i=0;i<campos.size();i++){ if(i<campos.size()-1){ query+="?,"; } else query+="?);"; } System.out.println(query); System.out.println("Entrou aqui!"); preparedstatement = connection.prepareStatement(query); for(int i=0;i<campos.size();i++){ System.out.println(i); preparedstatement.setString(i+1, valores.get(i).toString()); System.out.println(valores.get(i).toString()); } //Até aqui tudo bem sem a próxima instrução (executeUpdate()) não dá erro algum. preparedstatement.executeUpdate(); fecharConexao(); } Erro completo: Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:604) at java.util.ArrayList.get(ArrayList.java:382) at salão.TelaCadastro.cadastrarProdutoActionPerformed(TelaCadastro.java:877) at salão.TelaCadastro.access$200(TelaCadastro.java:18) at salão.TelaCadastro$4.actionPerformed(TelaCadastro.java:461) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2713) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707) at java.awt.EventQueue.access$000(EventQueue.java:101) at java.awt.EventQueue$3.run(EventQueue.java:666) at java.awt.EventQueue$3.run(EventQueue.java:664) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:680) at java.awt.EventQueue$4.run(EventQueue.java:678) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:677) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) at java.awt.EventDispatchThread.run(EventDispatchThread.java:90) Acho estranho o fato de que o erro cita ArrayList no entanto o erro é no executeUpdate(). Qualquer ajuda agradeço. 😉
KTachyon Posted April 23, 2012 at 07:26 AM Report #450653 Posted April 23, 2012 at 07:26 AM Porque o teu erro não está aí. Vê esta linha: at salão.TelaCadastro.cadastrarProdutoActionPerformed(TelaCadastro.java:877) No teu ficheiro TelaCadastro.java, linha 877, o que é que tens lá? “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now