8 Replies - 2554 Views - Last Post: 08 June 2011 - 07:38 AM Rate Topic: -----

#1 imu_1  Icon User is offline

  • D.I.C Regular

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

Storing data in the database table in a java program

Posted 08 June 2011 - 01:34 AM

Hello,
I would like to request you all out there to help me out solve this problem. Your valuable inputs are very helpful. i have this program which prompts the user for firstname and lastname and stores the data in a MSAccess DataBase. I am getting an error when i run my code. The error is somehow confusing because it needs only one parameter which doesnt make sense to me.

Here's the error:

C:\JAVA2>java SimpleJdbc
Enter the first name:mary
Enter the last name:allen
Driver loaded
Database connected
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few paramet
ers. Expected 1.
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:33)

Here's my code:

import java.sql.*;
import java.util.*;
public class SimpleJdbc {
  public static void main(String[] args)
      throws SQLException, ClassNotFoundException {
      
	  Scanner input = new Scanner(System.in);
	  String FName = "", LName ="";

	 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();

    // Execute a statement
     
      statement.executeUpdate(" INSERT INTO Student(firstname,lastname)" + 
	    "VALUES(" + FName + ", '" + LName +"')");
    
// Close the connection
    }
	catch (SQLException ex) {
      ex.printStackTrace();
    }
	
 }
}



As you can see, there is NO connection or loading problem. The only problem is in the syntax. I would appreciate your feedback.
Thanx, Allen.

Is This A Good Question/Topic? 0
  • +

Replies To: Storing data in the database table in a java program

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Storing data in the database table in a java program

Posted 08 June 2011 - 04:07 AM

You are missing single for the FName.
"VALUES ('" + FNAME + "' ,...

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: Storing data in the database table in a java program

Posted 08 June 2011 - 06:51 AM

Thanks for your reply. I am not getting nay runtime error now but the data is not stored in the table.
Am i missing anything ?

Here's my updated version:

import java.sql.*;
import java.util.*;
public class SimpleJdbc {
  public static void main(String[] args)
      throws SQLException, ClassNotFoundException {
      
	  Scanner input = new Scanner(System.in);
	  String FName = "", LName ="";

	 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();

    // Execute a statement
     
      statement.executeUpdate(" INSERT INTO Student(firstname,lastname)" + 
	    "VALUES('" + FName + "', '" + LName +"')");
    
// Close the connection
    }
	catch (SQLException ex) {
      ex.printStackTrace();
    }
	
 }
}



I will very much appreciate your help.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,646
  • Joined: 20-September 08

Re: Storing data in the database table in a java program

Posted 08 June 2011 - 07:07 AM

Quote

// Close the connection



That's exactly what you DON'T do, and the reason for your problem
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: Storing data in the database table in a java program

Posted 08 June 2011 - 07:21 AM

But that is just a comment, comments are ignored by the compiler. Are you pointing towards something else?

I am sorry but i didnt get your point.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,646
  • Joined: 20-September 08

Re: Storing data in the database table in a java program

Posted 08 June 2011 - 07:24 AM

Close the connection
Was This Post Helpful? 0
  • +
  • -

#7 imu_1  Icon User is offline

  • D.I.C Regular

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

Re: Storing data in the database table in a java program

Posted 08 June 2011 - 07:32 AM

I tried that too but it doesnt work.

Here's the updated version:

import java.sql.*;
import java.util.*;
public class SimpleJdbc {
  public static void main(String[] args)
      throws SQLException, ClassNotFoundException {
      
	  Scanner input = new Scanner(System.in);
	  String FName = "", LName ="";

	 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();

    // Execute a statement
     
      statement.executeUpdate(" INSERT INTO Student(firstname,lastname)" + 
	    "VALUES('" + FName + "', '" + LName +"')");
    

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









I will very appreciate your further inputs. I need to get this done by today.

This post has been edited by macosxnerd101: 08 June 2011 - 07:32 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,646
  • Joined: 20-September 08

Re: Storing data in the database table in a java program

Posted 08 June 2011 - 07:35 AM

Close the Connection
Was This Post Helpful? 0
  • +
  • -

#9 imu_1  Icon User is offline

  • D.I.C Regular

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

Re: Storing data in the database table in a java program

Posted 08 June 2011 - 07:38 AM

I am sorry....it worked.Thanks alot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1