7 Replies - 3835 Views - Last Post: 09 June 2011 - 10:57 PM Rate Topic: -----

#1 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Update statement in Java Problem

Posted 09 June 2011 - 08:07 AM

Hello My mentors and friends,

I am tryin gto update my table using java but i am getting an error. I have looked through the code several times, but it looks like the code is not eager to show me the error.


Here's my code:


import java.sql.*;
import java.util.*;
import javax.swing.*;


public class SimpleJdbc {
  public static void main(String[] args)
      throws SQLException, ClassNotFoundException {
      
	  Scanner input = new Scanner(System.in);
	  String FName = "", LName ="";
      String DeleteName = "";
	  
	 System.out.print(" Enter the first name:"); 
	 FName = input.next(); 
     
	 System.out.print(" Enter the last name:"); 
	 LName = input.next(); 
	 
// Load the JDBC driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.println("Driver loaded");
    
  try{
       
	
	// Establish a connection
    Connection connection = DriverManager.getConnection
      ("jdbc:odbc:JUMAWIND");
    System.out.println("Database connected"); 

    // Create a statement
    Statement statement = connection.createStatement();

   
     // Searching for particular contact
	  String queryString = ("select firstname,lastname from Student where firstname"
		+ " = 'eliot'");
		
		
      ResultSet rset = statement.executeQuery(queryString);
	  
	  
      if (rset.next()) 
	  {
        String firstName = rset.getString("firstname");
        String lastName = rset.getString("lastname");
         
		 
        // Display result in a dialog box
        JOptionPane.showMessageDialog(null, firstName + " " +
          " " + lastName);
      } 
	  else 
	  {
        // Display result in a dialog box
        JOptionPane.showMessageDialog(null, "Not found");
      }
	  
	   // Inserting a new contact 
        statement.executeUpdate(" INSERT INTO Student(firstname,lastname)" + 
	    "VALUES('" + FName + "', '" + LName +"')");
    
	 // Deleting a contact 
	 System.out.print(" Enter the name of a person you want to delete: " ) ; 
	 DeleteName = input.next(); 
	 
	 statement.executeUpdate(" DELETE FROM Student where firstname ='" + DeleteName + "'");
	 statement.executeUpdate("UPDATE FROM Student SET firstname ='" + sally + "' WHERE firstname = '" + eliot + "'");
	
    connection.close();
	 
	}
	 
	catch (SQLException ex) 
	{
      ex.printStackTrace();
    }
	
 }
}



Here's the error :


C:\JAVA2>javac SimpleJdbc.java
SimpleJdbc.java:69: cannot find symbol
symbol : variable sally
location: class SimpleJdbc
statement.executeUpdate("UPDATE FROM Student SET firstname ='" + sally
+ "' WHERE firstname = '" + eliot + "'");
^
SimpleJdbc.java:69: cannot find symbol
symbol : variable eliot
location: class SimpleJdbc
statement.executeUpdate("UPDATE FROM Student SET firstname ='" + sally
+ "' WHERE firstname = '" + eliot + "'");

^
2 errors


Any help will be very much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Update statement in Java Problem

#2 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 144
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Update statement in Java Problem

Posted 09 June 2011 - 08:11 AM

You did not declare nor define any variables named sally or eliot.
Was This Post Helpful? 0
  • +
  • -

#3 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Update statement in Java Problem

Posted 09 June 2011 - 09:26 AM

I am sorry but i dont think it has to do with the declaration of the two names. The name eliot is in my database. All i need to do is replace is with the sally. I think i have a problem with my quotes in the update statement.
I am saying this because if you take a look at my select statement, you will see that i am selecting eliot from the databse while eliot is not defined anywher ein the program .

Any iseas will be appreciated.
Was This Post Helpful? 0
  • +
  • -

#4 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 144
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Update statement in Java Problem

Posted 09 June 2011 - 09:39 AM

Actually, the line
  statement.executeUpdate("UPDATE FROM Student SET firstname ='" + sally + "' WHERE firstname = '" + eliot + "'");

says that you want to select the entry in the database where the firstname equals the value contained by the variable eliot.

If you want to use the String literal "Sally" or "Eliot" you have to put these into ".

Edit: Or, if you always want to select these entries, why do you even bother splitting the string into pieces? Just use:
	statement.executeUpdate("UPDATE FROM Student SET firstname ='sally' WHERE firstname = 'eliot'");


This post has been edited by n8schatten: 09 June 2011 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#5 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Update statement in Java Problem

Posted 09 June 2011 - 11:00 AM

I have made the required changes as per your suggestions but i am still getting the same error.

Here's the updated version:


import java.sql.*;
import java.util.*;
import javax.swing.*;


public class SimpleJdbc {
  public static void main(String[] args)
      throws SQLException, ClassNotFoundException {
      
	  Scanner input = new Scanner(System.in);
	  String FName = "", LName ="";
      String DeleteName = "";
	  
	 System.out.print(" Enter the first name:"); 
	 FName = input.next(); 
     
	 System.out.print(" Enter the last name:"); 
	 LName = input.next(); 
	 
// Load the JDBC driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.println("Driver loaded");
    
  try{
       
	
	// Establish a connection
    Connection connection = DriverManager.getConnection
      ("jdbc:odbc:JUMAWIND");
    System.out.println("Database connected"); 

    // Create a statement
    Statement statement = connection.createStatement();

   
     // Searching for particular contact
	  String queryString = ("select firstname,lastname from Student where firstname"
		+ " = 'eliot'");
		
		
      ResultSet rset = statement.executeQuery(queryString);
	  
	  
      if (rset.next()) 
	  {
        String firstName = rset.getString("firstname");
        String lastName = rset.getString("lastname");
         
		 
        // Display result in a dialog box
        JOptionPane.showMessageDialog(null, firstName + " " +
          " " + lastName);
      } 
	  else 
	  {
        // Display result in a dialog box
        JOptionPane.showMessageDialog(null, "Not found");
      }
	  
	   // Inserting a new contact 
        statement.executeUpdate(" INSERT INTO Student(firstname,lastname)" + 
	    "VALUES('" + FName + "', '" + LName +"')");
    
	 // Deleting a contact 
	 System.out.print(" Enter the name of a person you want to delete: " ) ; 
	 DeleteName = input.next(); 
	 
	 statement.executeUpdate(" DELETE FROM Student where firstname ='" + DeleteName + "'");
	 //statement.executeUpdate("UPDATE FROM Student SET firstname =' sally "'"  where firstname""' eliot "'");
	 statement.executeUpdate("UPDATE FROM Student SET firstname ='sally' WHERE firstname = 'eliot'");


    connection.close();
	 
	}
	 
	catch (SQLException ex) 
	{
      ex.printStackTrace();
    }
	
 }
}




Here's the error:


C:\JAVA2>javac SimpleJdbc.java

C:\JAVA2>java SimpleJdbc
Enter the first name:mahmoud
Enter the last name:esmail
Driver loaded
Database connected
Enter the name of a person you want to delete: eliot
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in
UPDATE statement.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(Unknown Source)
at SimpleJdbc.main(SimpleJdbc.java:70)
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Update statement in Java Problem

Posted 09 June 2011 - 12:36 PM

First, you should really be using a PreparedStatement. So much easier to work with and they have other benefits I cant quite think of at the moment.
As for your problem
statement.executeUpdate("UPDATE FROM Student SET firstname ='sally' WHERE firstname = 'eliot'");


Where did FROM come from? An Update should be
statement.executeUpdate("UPDATE Student SET firstname ='sally' WHERE firstname = 'eliot'");


Let us know if you get anymore errors. There may be an issue with the spacing. Thats one reason why its better to use PreparedStatements.
Was This Post Helpful? 0
  • +
  • -

#7 PudgeCo  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 18
  • Joined: 04-March 11

Re: Update statement in Java Problem

Posted 09 June 2011 - 12:41 PM

Looking at what you have currently:

	 statement.executeUpdate("UPDATE FROM Student SET firstname ='sally' WHERE firstname = 'eliot'");



I do not believe that the error is in your Java, I think the issue is the Update statement itself, try removing the word from, as follows:

	 statement.executeUpdate("UPDATE Student SET firstname ='sally' WHERE firstname = 'eliot'");



View Postnick2price, on 09 June 2011 - 12:36 PM, said:

First, you should really be using a PreparedStatement. So much easier to work with and they have other benefits I cant quite think of at the moment.
As for your problem
statement.executeUpdate("UPDATE FROM Student SET firstname ='sally' WHERE firstname = 'eliot'");


Where did FROM come from? An Update should be
statement.executeUpdate("UPDATE Student SET firstname ='sally' WHERE firstname = 'eliot'");


Let us know if you get anymore errors. There may be an issue with the spacing. Thats one reason why its better to use PreparedStatements.


Looks like you beat me to the punch; After replying, I noticed your post right before mine.

This post has been edited by PudgeCo: 09 June 2011 - 12:38 PM

Was This Post Helpful? 0
  • +
  • -

#8 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: Update statement in Java Problem

Posted 09 June 2011 - 10:57 PM

It worked, thanks alot for your help. I would definetely sit down and learn about prepared statements.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1