Results 1 to 2 of 2

Thread: SQL code

  1. #1
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default SQL code

    My one Userinfo class sends information to the SQL class to put it into the database but it gives errors that I can't figure out. I have tried everything. If I test it with jut my SQL class with Strings that I type in it runs, but the information isn't in the database. AArgh so frustrating!!!
    Here is the Userinfo class with the button that sends the information:
    Java Code:
    private void button1MouseClicked(MouseEvent e) 
          {
             String name1 = textField1.getText();
             String name2 = textField2.getText();
             String playup = textField3.getText();
             String rotate = textField4.getText();
             try
             {
                SQL suser = new SQL(name1, name2, playup, rotate);
             }
                 catch (SQLException ex)
                {
                   throw new RuntimeException(ex);
                }
             finally
             {
                Scorekeeper scrkper = new Scorekeeper("");
                this.setVisible(false);
             }
             
          }
    and here is my whole SQL class:
    Java Code:
       import java.sql.*;
       import java.io.*;
       import javax.swing.*;
       
        public class SQL
       {
          Connection conn = connect();
          Statement stmt;
       
          private String naam1;
          private String naam2;
          private String playup;
          private String rotate;
       
           SQL (String nm1, String nm2, String plyp, String rtte)throws SQLException
          {
             naam1 = nm1;
             naam2 = nm2;
             playup = plyp;
             rotate = rtte;
             System.out.println(naam1+naam2+playup+rotate);
             updateMthd("INSERT INTO Table1 VALUES (naam1,naam2,playup,rotate)");
             }
          
       	//Make a connection
           public Connection connect ()
          {
          
             //Load driver
             try
             {
                Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
             }
                 catch (ClassNotFoundException e)
                {
                   System.out.println ("Cannot load driver");
                }
          
             //Add name of database
             try
             {
                String dbName = "DSQL.mdb"; 
                conn = DriverManager.getConnection ("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + dbName);
                System.out.println ("Connected to database");
             }
                 catch (Exception e)
                {
                   System.out.println ("Unable to connect to database");
                }
             return conn;
          }
                  
       	//Create resultset and display information
           void displayMthd (String heading, String sqlStmt) throws SQLException
          {
             stmt = conn.createStatement();		//Standard method from Statement class
             ResultSet rs = stmt.executeQuery(sqlStmt);   //Method from Resultset class (also update query)
               
             System.out.println();
             System.out.println(heading);
             System.out.println();
             while (rs.next())
             {
             //
                System.out.println();
             }
             System.out.println();   
             stmt.close();
          }
       
       	//Update data in table
           void updateMthd (String update) throws SQLException
          {
             stmt = conn.createStatement();
             stmt.executeUpdate(update);
             stmt.close();
          }
          
          	//Method to create spaces for formatting display
           private String spaces (String s, int width)
          {
             String temp = "";
             for (int I = 0; I<= width - s.length(); I++)
             {
                temp = temp + " ";
             }
             return temp;
          }
             
          //Main method     
           public static void main (String [] args) throws SQLException
          {
            // new SQL ();
          }//End of main method
          
       } //End of class DMovies

  2. #2
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Default Re: SQL code

    The problem with the SQL class was that the database wasn't sored in 200-2003 format. But the other problem I can't figure out. It's when I push the button it gives me the following errors:
    Java Code:
     ----jGRASP exec: java Userinfo
    
    Connected to database
    PingPong212
    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 4.
    	at Userinfo.button1MouseClicked(Userinfo.java:39)
    	at Userinfo.access$100(Userinfo.java:12)
    	at Userinfo$2.mouseClicked(Userinfo.java:208)
    	at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    	at java.awt.Component.processMouseEvent(Component.java:6437)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6199)
    	at java.awt.Container.processEvent(Container.java:2203)
    	at java.awt.Component.dispatchEventImpl(Component.java:4790)
    	at java.awt.Container.dispatchEventImpl(Container.java:2261)
    	at java.awt.Component.dispatchEvent(Component.java:4616)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4803)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4472)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4393)
    	at java.awt.Container.dispatchEventImpl(Container.java:2247)
    	at java.awt.Window.dispatchEventImpl(Window.java:2674)
    	at java.awt.Component.dispatchEvent(Component.java:4616)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:662)
    	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)
    Caused by: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 4.
    	at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
    	at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    	at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3109)
    	at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:337)
    	at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:287)
    	at SQL.updateMthd(SQL.java:79)
    	at SQL.<init>(SQL.java:22)
    	at Userinfo.button1MouseClicked(Userinfo.java:34)
    	... 23 more
    
     ----jGRASP: operation complete.

Similar Threads

  1. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  2. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •