Results 1 to 3 of 3
  1. #1
    rushhour is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default Prepared Statement problem in my servlet's data manager

    Hi all,

    I am having trouble with a prepared statement in my Java Servlet's data manager where it inserts a member into the database. I believe the syntax is correct, but I always seem to get the error:

    SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)' at line 1

    Here is my code
    Java Code:
    // data manager
    public class DataManager
    {
    	private DBCon dbc;  
    	private Connection con; 
    	private Statement stmt;  
    	private PreparedStatement ps = null; 
    	
    	public DataManager()
    	{
    		dbc = new DBCon(); 
    		con = dbc.getConnection();
    		stmt = dbc.getStatement();
    	}
    
            public void insertMember( String first, String last, String birth, String line1, String line2, String town, String province, String zip, String eadd, 
    	                           String contactno, String user, String pass, String quest, String ans, String agree ) 
        {
            try
            {
                
    	    
    			String strUpdate = "INSERT INTO mdb VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    			ps = conn.prepareStatement(strUpdate);
    			ps.setString(1, first);
    			ps.setString(2, last);
    			ps.setString(3, birth);
    			ps.setString(4, line1);
    			ps.setString(5, line2);
    			ps.setString(6, town);
    			ps.setString(7, province);
    			ps.setString(8, zip);
    			ps.setString(9, eadd);
    			ps.setString(10, contactno);
    			ps.setString(11, user);
    			ps.setString(12, pass);
    			ps.setString(13, quest);
    			ps.setString(14, ans);
    			ps.setString(15, agree);
             
    			ps.executeUpdate( strUpdate ) ;
            } 
            catch (SQLException ex) 
            {        
                System.out.println("SQLException: " + ex.getMessage());
            }
    Java Code:
    // connector
    public class DBCon
    {
    	private Connection con;  
    	private Statement stmt;   
    	
    	public DBCon()
    	{
    		try
    		{
    			Class.forName( "com.mysql.jdbc.Driver" );
    		}
    		catch ( Exception ex )
    		{
    			System.out.println( "Exception is: " + ex.toString( ) ) ;
    		}
    		
    		try
    		{
    			conn = DriverManager.getConnection( "jdbc:mysql://localhost/theauctiondb?user=root&password=mypass" );
    			
    			stmt = conn.createStatement( );
    			
    			//ps = conn.prepareStatement();
    		}
    		catch ( SQLException ex )
    		{
    			System.out.println( "SQLException: " + ex.getMessage( ) );
    		}
    	}
    	
    	public Connection getConnection( )
    	{
    		return conn ;
    	}
    	
    	public Statement getStatement( )
    	{
    		return stmt ;
    	}
    	
    	public void closeConnection()
    	{
    		try
    		{
    			if ( stmt != null )
    			{
    				stmt.close( ) ;
    			}
    			
    			if ( conn != null )
    			{
    				conn.close( ) ;
    			}
    		}
    		catch ( SQLException ex )
    		{
    			System.out.println( "SQLException: " + ex.getMessage( ) );
    		}
    	}
    }
    Hope you can help me solve this problem.
    Last edited by Fubarable; 05-01-2011 at 11:47 PM.

  2. #2
    sanchi is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Check your MySql limitation if there is anything like number parameters allowed from jdbc.
    Personally, I never worked in that as I used oracle most of the time.
    But rest of the code looks fine.

    San
    Beyond Java

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Bit late here, but I've been on holday...
    Java Code:
    ps.executeUpdate( strUpdate ) ;
    That isn't executing the prepared statement with the parameters set, but is simply executing the String given to it...which is obviously wrong. Just use the executeUpdate() that has no parameters.

Similar Threads

  1. Jdbc Prepared Statement execute()
    By nitishjtm in forum JDBC
    Replies: 37
    Last Post: 12-16-2010, 08:43 AM
  2. JDBC Prepared Statement
    By Floetic in forum JDBC
    Replies: 4
    Last Post: 05-20-2009, 11:53 PM
  3. MySQL/JDBC prepared statement problem
    By thelinuxguy in forum Advanced Java
    Replies: 3
    Last Post: 02-11-2009, 11:21 PM
  4. Prblem in Prepared Statement
    By haneeshrawther in forum JDBC
    Replies: 2
    Last Post: 04-25-2008, 09:49 AM
  5. Using Prepared Statement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-06-2008, 09:22 AM

Posting Permissions

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