Newsgroup: it.comp.java
Titolo: Access e JDBC

Sto scrivendo una applicazione basata su Database MS Acess 97, ma ho
incontrato un problema insormontabile (spero solo apparentemente!!). Ho
scritto questo metodo
private void Connect() throws Exception {
  System.out.println("Connecting to database....");
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  String url=new String("jdbc:odbc:Allevamento");
  String SQL=new String("SELECT * FROM tori");
  connection=DriverManager.getConnection(url);
  statement=connection.createStatement();
  boolean hasResults=statement.execute(SQL);
  if (hasResults){
  result=statement.getResultSet();
  if (result!=null){ editNome.setText(result.getString(0));}
  }
 // result=statement.executeQuery(SQL);
  System.out.println("Connected with success");
}
con l'intento di visualizzare in un TextField il risultato della query
record per  record ma ogni volta mi viene dato sempre lo stesso messaggio di
errore:
java.sql.SQLException: [Microsoft] [ODBC Driver Manager] Indice del
descrittore non valido.
  at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:4089)
  at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java.4246)
  at sun.jdbc.odbc.JdbcOdbc.SQLColAttributes(JdbcOdbc.java:699)
  at
sun.jdbc.odbc.JdbcOdbcResultSet.getColAttribute(JdbcOdbcResultSet.java:1524)
  at
sun.jdbc.odbc.JdbcOdbcResultSet.getColumnType(JdbcOdbcResultSet.java:1989)
  at
sun.jdbc.odbc.JdbcOdbcResultSet.getMaxCharLen(JdbcOdbcResultSet.java:1550)
 at sun.jdbc.odbc.JdbcOdbcResultSet.getString((JdbcOdbcResultSet.java:157)

Ecco l'intera classe postata:
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import java.awt.event.*;
public class InsFattrici extends JFrame {
  BorderLayout borderLayout1 = new BorderLayout();
  JToolBar jToolBar1 = new JToolBar();
  JPanel jPanel1 = new JPanel();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  JButton jButton4 = new JButton();
  JTextField editNome = new JTextField();
  JTextField editIDFattrice = new JTextField();
  JLabel jLabel2 = new JLabel();
  JLabel jLabel1 = new JLabel();
  JTextField editNGroppa = new JTextField();
  JTextField editNAPA = new JTextField();
  JLabel jLabel3 = new JLabel();
  JLabel jLabel4 = new JLabel();
  Connection connection;
  Statement statement;
  ResultSet result;
  public InsFattrici() {
    try {
      jbInit();
      Connect();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception {
    jLabel1.setBounds(new Rectangle(9, 38, 97, 27));
    jLabel1.setText("Nome fattrice:");
    jLabel2.setBounds(new Rectangle(13, 6, 99, 25));
    jLabel2.setText("ID:");
    editIDFattrice.setBounds(new Rectangle(110, 5, 169, 24));
    editIDFattrice.setBorder(BorderFactory.createLoweredBevelBorder());
    editNome.setBounds(new Rectangle(110, 36, 169, 23));
    editNome.setBorder(BorderFactory.createLoweredBevelBorder());
    jButton4.setText(" >>");
    jButton3.setText(" > ");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        next_actionPerformed(e);
      }
    });
    jButton2.setText(" < ");
    jButton1.setText("<< ");
    jPanel1.setLayout(null);
    this.getContentPane().setLayout(borderLayout1);
    editNGroppa.setBorder(BorderFactory.createLoweredBevelBorder());
    editNGroppa.setBounds(new Rectangle(111, 64, 167, 25));
    editNAPA.setBorder(BorderFactory.createLoweredBevelBorder());
    editNAPA.setToolTipText("");
    editNAPA.setBounds(new Rectangle(112, 95, 166, 25));
    jLabel3.setText("N° groppa:");
    jLabel3.setBounds(new Rectangle(8, 64, 105, 25));
    jLabel4.setText("N° APA");
    jLabel4.setBounds(new Rectangle(7, 95, 94, 26));
    this.getContentPane().add(jToolBar1, BorderLayout.NORTH);
    this.setSize(640,480);
    jToolBar1.add(jButton1, null);
    jToolBar1.add(jButton2, null);
    jToolBar1.add(jButton3, null);
    jToolBar1.add(jButton4, null);
    this.getContentPane().add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(jLabel3, null);
    jPanel1.add(jLabel2, null);
    jPanel1.add(editIDFattrice, null);
    jPanel1.add(editNome, null);
    jPanel1.add(jLabel1, null);
    jPanel1.add(editNGroppa, null);
    jPanel1.add(editNAPA, null);
    jPanel1.add(jLabel4, null);
  }
private void Connect() throws Exception {
  System.out.println("Connecting to database....");
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  String url=new String("jdbc:odbc:Allevamento");
  String SQL=new String("SELECT * FROM tori");
  connection=DriverManager.getConnection(url);
  statement=connection.createStatement();
  boolean hasResults=statement.execute(SQL);
  if (hasResults){
  result=statement.getResultSet();
  if (result!=null){ editNome.setText(result.getString(0));}
  }
 // result=statement.executeQuery(SQL);
  System.out.println("Connected with success");
}
  void next_actionPerformed(ActionEvent e) {
 try{
    result.next();
    editIDFattrice.setText(result.getString(0));
    editNome.setText(result.getString(1));
    this.repaint();
     }catch(SQLException ifgh){
     ifgh.printStackTrace();
    }
  }
}

 wrote in message
8jnioq$e85$1...@lacerta.tiscalinet.it...


   boolean hasResults=statement.execute(SQL);
se usi statement.executeQuery ti restituisce un ResulSet
ResultSet result=statement.executeQuery(SQL);
quindi ...
if(result.next())
    editNome.setText(result.getString(1));
2 cose:
1-result.next fa avanzare il puntatore del resultset
2-il getString di ResultSet come indice parte da 1 e non da 0
Ciao

Grazie tante... avevo capito un errore guardando una classe che avevo
scritto 1 anno fa, ma che avevo perso, ma non sapevo che il getString di
ResultSet come indice partiva da 1 e non da 0!!!! Mannaggia
webm...@bytecode.it
 ha scritto nel messaggio ...



