1 Reply Latest reply on Feb 24, 2007 9:29 AM by JoachimSauer

    parameter passing in sql statement

    807606
      hello..
      i want to execute a value insertion query in jdbc..where the values are all parameterized string variables.but it failed to execute the query...
      and generating a sql exception "too few parameter.."
      but it succesfully executes with a direct string like'hello'....
      heres my sample code for this...

      public void settablevalue(String pn,String pid,String time)
      {
      try
      {
      Statement s1=con.createStatement();
      System.out.println(pn+" "+pid+" "+time);
      char []pname=new char[50];
      char []prcsid=new char[50];
      char []timecrtn=new char[100];
      pname=pn.toCharArray();
      prcsid=pid.toCharArray();
      timecrtn=time.toCharArray();
      s1.execute("insert into ProcessEvent values(pname[],prcsid[],timecrtn[])");
      System.out.println("Executed");
      s1.close();
      }
      catch (Exception err)
      {
      System.out.println("ERROR: " + err);
      }
      }

      now do i insert string values in the table...
      pls help...thnx in advance
        • 1. Re: parameter passing in sql statement
          JoachimSauer
          You should use a PreparedStatement. Read the JavaDoc of PreparedStatement.

          Usually you prepare a statmenet like this: "insert into ProcessEvent values (?, ?, ?)" and then use setString(), setDate(), ... to set the concrete values.

          Additionally ('though this doesn't have to do anything with your problem): You don't need to create char-arrays, and even if you do, you don't have to initialize them (using "char[]pname=new char[50]") if you later assign a new value (using "pname=pn.toCharArray()").