p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Java Databases (http://p2p.wrox.com/forumdisplay.php?f=114)
-   -   Problem Updating MS Access database (http://p2p.wrox.com/showthread.php?t=57044)

javlet April 24th, 2007 06:11 AM

Problem Updating MS Access database
 
I am getting an error when updating an MS Access database. Can someone please help.

The error is:

Error: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few
parameters. Expected 9.

Code:

 private class Calculate implements ActionListener
         {
            public void actionPerformed (ActionEvent ae)
               {

            try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            // set this to a MS Access DB you have on your machine
            String filename = "F:/Bernnie/java/referencingsys.mdb";
            String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";

            // add on to the end
            database+= filename.trim() + ";DriverID=22;READONLY=true}";

            // now we can get the connection from the DriverManager
            Connection con = DriverManager.getConnection( database ,"","");


           // try and create a java.sql.Statement so we can run queries
           Statement s = con.createStatement();

           //Assign contents in textboxes to varialbles for the purpose of sending to database
           Surname = txtSurname.getText();
       Initials = txtInitials.getText();
           Year = txtYear.getText();
           Title = txtTitle.getText();
           Volume = txtVolume.getText();
           Edition = txtEdition.getText();
           Publisher = txtPublisher.getText();
           City =txtPubCity.getText();
           Pages = txtNo_of_Pages.getText();



           // Update the data from the table
           s.executeUpdate("insert into tblBook VALUES (Surname, Initials, Year, Title, Volume, Edition, Publisher, City, Pages)");
 

           // get any ResultSet that came from our query
           //ResultSet rs = s.getResultSet();
           //if (rs != null) // if rs == null, then there is no ResultSet to view
           //while ( rs.next() ) // this will step through our data row-by-row

           {
            /* the next line will get the first column in our current row's ResultSet
            as a String ( getString( columnNumber) ) and output it to the screen */

            //System.out.println("Harvard Referencing Style: " + rs.getString("") );
           }

            // close the Statement to let the database know we're done with it
            s.close();

            // close the Connection to let the database know we're done
            con.close();



        }


            catch (Exception e) {
            System.out.println("Error: " + e);
        }

               } 
         }


iamrakesh22 April 24th, 2007 07:16 AM

Hi,
I think you've missed few fundamentals.
  1. You forgot to pass the parameters to the inser statement.
  2. To read data from a table first we'll issue a select query and use execureQuery() method of the statement class which'll return the resultset object.

Try to go through some JDBC material.
http://www.javaskyline.com/learnjdbc.html
http://www.freeprogrammingresources.com/jdbc.html

Regards,
Rakesh


All times are GMT -4. The time now is 09:52 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.