Error in Java Statement when inserting into MS Access DB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maas
    New Member
    • Aug 2007
    • 6

    #1

    Error in Java Statement when inserting into MS Access DB

    Hello

    I have a problem when coding the statement
    [PHP]stmt.execute("i nsert into Member values ("+mname+" ,"+mail+","+tel +")");[/PHP]

    where mname, mail,tel are variables
    which the user inputs them and stored in variables and then i want them to be stored in MS Access, everything is done successfully until it reached the previous statement

    Here is the code
    [PHP]try {

    System.out.prin tln("Beginning Connection");
    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
    String accessFileName = "jdbc:odbc:Proj ect";
    String connURL = "jdbc:odbc:;DRI VER=Microsoft Access Driver (*.mdb);DBQ="+a ccessFileName+" .mdb;PWD=";
    Connection con = DriverManager.g etConnection( accessFileName) ;
    Statement stmt = con.createState ment();
    System.out.prin tln("Connection done successfully");
    stmt.execute("C reate table Member(Name String,EMAIL String,Telephon e String)");
    System.out.prin tln("Table Created");

    System.out.prin tln("Please, Enter the Member Name : ");
    InputStreamRead er istream = new InputStreamRead er (System.in);
    BufferedReader br = new BufferedReader (istream);

    try{
    String mname= (br.readLine()) ;

    System.out.prin tln("Please, Enter the E-Mail Address : ");
    InputStreamRead er isr = new InputStreamRead er (System.in);
    BufferedReader bfr = new BufferedReader (isr);

    try
    {
    String mail= (bfr.readLine() );
    try{

    System.out.prin tln("Please, Enter the Telephone number : ");
    InputStreamRead er ism = new InputStreamRead er (System.in);
    BufferedReader bb = new BufferedReader (ism);

    String tel=String.valu eOf(bb.readLine ());
    stmt.execute("i nsert into Member values ("+mname+" ,"+mail+","+tel +")");
    System.out.prin tln("Insertion has been completed");
    stmt.execute(" select * from Member");
    ResultSet rs=stmt.getResu ltSet();

    if (rs != null)
    while (rs.next()){
    System.out.prin tln("Name: "+rs.getString( "Name")+ " E-MAIL: " + rs.getString("E MAIL")+" Telephone: "+rs.getString( "Telephone" ));
    }

    stmt.close();
    con.close();


    }

    catch (IOException err)
    {

    System.out.prin tln("Error in reading the numbers");
    }

    }

    catch (IOException err)
    {

    System.out.prin tln("Error in reading the numbers");
    }

    }
    catch (IOException err)
    {

    System.out.prin tln("Error in reading the numbers");
    }

    }


    catch (Exception err) {err.printStack Trace();}
    }[/PHP]
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Shouldn't literal string values be embraced by single quote characters in SQL?
    The String values are variables in your Java program but the values aren't just
    string literals in your SQL query.

    kind regards,

    Jos

    Comment

    • maas
      New Member
      • Aug 2007
      • 6

      #3
      well, sorry i didn't understand well
      how to modify the query??

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by maas
        well, sorry i didn't understand well
        how to modify the query??
        Just put single quotes around those literal strings in your SQL query.

        kind regards,

        Jos

        Comment

        • maas
          New Member
          • Aug 2007
          • 6

          #5
          i put single quote
          but it will print "+mname", "+mail", "+tel"
          not the values that were added by the user

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by maas
            i put single quote
            but it will print "+mname", "+mail", "+tel"
            not the values that were added by the user
            I borrowed your query and put single quotes around the string values; carefully
            read the positions of the double and single quotes.

            [code=java]
            stmt.execute("i nsert into Member values ('"+mname+"' ,'"+mail+"','"+ tel+"')");
            [/code]

            kind regards,

            Jos

            Comment

            • maas
              New Member
              • Aug 2007
              • 6

              #7
              Thank you sir
              the problem has been solved

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by maas
                Thank you sir
                the problem has been solved
                You're welcome of course; you might have a look at the PreparedStateme nt
                interface; you don't have to jump trough all those quoting hoops with that one
                and its more efficient as well when you use it repeatedly.

                kind regards,

                Jos

                Comment

                • maas
                  New Member
                  • Aug 2007
                  • 6

                  #9
                  well, i would like to learn in the future maybe because i am a little bit a beginner in java
                  and i have another problem
                  which the do while loop
                  where it will ask the user if he wants to insert more values and to do an iteration
                  but in my program i didnot

                  [PHP]import java.sql.*;
                  import java.io.*;
                  public class Project3A {

                  public static void main(String[] args) {
                  String mname="";
                  String mail="";
                  String tel="";
                  String ans="";
                  try {

                  System.out.prin tln("Beginning Connection");
                  Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
                  String accessFileName = "jdbc:odbc:Proj ect";
                  String connURL = "jdbc:odbc:;DRI VER=Microsoft Access Driver (*.mdb);DBQ="+a ccessFileName+" .mdb;PWD=";
                  Connection con = DriverManager.g etConnection( accessFileName) ;
                  Statement stmt = con.createState ment();
                  System.out.prin tln("Connection done successfully");
                  stmt.execute("C reate table Member(Name String,EMAIL String,Telephon e String)");
                  System.out.prin tln("Table Created");
                  do{


                  System.out.prin tln("Please, Enter the Member Name : ");
                  InputStreamRead er istream = new InputStreamRead er (System.in);
                  BufferedReader br = new BufferedReader (istream);

                  try{
                  mname= (br.readLine()) ;

                  System.out.prin tln("Please, Enter the E-Mail Address : ");
                  InputStreamRead er isr = new InputStreamRead er (System.in);
                  BufferedReader bfr = new BufferedReader (isr);

                  try
                  {
                  mail= (bfr.readLine() );
                  try{

                  System.out.prin tln("Please, Enter the Telephone number : ");
                  InputStreamRead er ism = new InputStreamRead er (System.in);
                  BufferedReader bb = new BufferedReader (ism);

                  tel=String.valu eOf(bb.readLine ());
                  stmt.execute("i nsert into Member (Name,EMAIL,Tel ephone) values ('"+mname+"','" +mail+"','"+tel +"')");
                  System.out.prin tln("Insertion has been completed");
                  stmt.execute(" select * from Member");
                  ResultSet rs=stmt.getResu ltSet();

                  if (rs != null)
                  while (rs.next()){
                  System.out.prin tln("Name: "+rs.getString( "Name")+ " E-MAIL: " + rs.getString("E MAIL")+" Telephone: "+rs.getString( "Telephone" ));
                  }
                  try{

                  System.out.prin tln("Do you want to insert more records? ");
                  InputStreamRead er is = new InputStreamRead er (System.in);
                  BufferedReader bbb = new BufferedReader (is);
                  ans=String.valu eOf(bbb.readLin e());

                  }
                  catch (IOException err)
                  {

                  System.out.prin tln("Error in reading the numbers");
                  }

                  stmt.close();
                  con.close();


                  }

                  catch (IOException err)
                  {

                  System.out.prin tln("Error in reading the numbers");
                  }

                  }

                  catch (IOException err)
                  {

                  System.out.prin tln("Error in reading the numbers");
                  }

                  }
                  catch (IOException err)
                  {

                  System.out.prin tln("Error in reading the numbers");
                  }
                  }while(ans=="ye s");
                  }


                  catch (Exception err) {err.printStack Trace();}

                  }
                  }[/PHP]

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by maas
                    well, i would like to learn in the future maybe because i am a little bit a beginner in java
                    and i have another problem
                    which the do while loop
                    where it will ask the user if he wants to insert more values and to do an iteration
                    but in my program i didnot

                    [PHP]import java.sql.*;
                    import java.io.*;
                    public class Project3A {

                    public static void main(String[] args) {
                    String mname="";
                    String mail="";
                    String tel="";
                    String ans="";
                    try {

                    System.out.prin tln("Beginning Connection");
                    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
                    String accessFileName = "jdbc:odbc:Proj ect";
                    String connURL = "jdbc:odbc:;DRI VER=Microsoft Access Driver (*.mdb);DBQ="+a ccessFileName+" .mdb;PWD=";
                    Connection con = DriverManager.g etConnection( accessFileName) ;
                    Statement stmt = con.createState ment();
                    System.out.prin tln("Connection done successfully");
                    stmt.execute("C reate table Member(Name String,EMAIL String,Telephon e String)");
                    System.out.prin tln("Table Created");
                    do{


                    System.out.prin tln("Please, Enter the Member Name : ");
                    InputStreamRead er istream = new InputStreamRead er (System.in);
                    BufferedReader br = new BufferedReader (istream);

                    try{
                    mname= (br.readLine()) ;

                    System.out.prin tln("Please, Enter the E-Mail Address : ");
                    InputStreamRead er isr = new InputStreamRead er (System.in);
                    BufferedReader bfr = new BufferedReader (isr);

                    try
                    {
                    mail= (bfr.readLine() );
                    try{

                    System.out.prin tln("Please, Enter the Telephone number : ");
                    InputStreamRead er ism = new InputStreamRead er (System.in);
                    BufferedReader bb = new BufferedReader (ism);

                    tel=String.valu eOf(bb.readLine ());
                    stmt.execute("i nsert into Member (Name,EMAIL,Tel ephone) values ('"+mname+"','" +mail+"','"+tel +"')");
                    System.out.prin tln("Insertion has been completed");
                    stmt.execute(" select * from Member");
                    ResultSet rs=stmt.getResu ltSet();

                    if (rs != null)
                    while (rs.next()){
                    System.out.prin tln("Name: "+rs.getString( "Name")+ " E-MAIL: " + rs.getString("E MAIL")+" Telephone: "+rs.getString( "Telephone" ));
                    }
                    try{

                    System.out.prin tln("Do you want to insert more records? ");
                    InputStreamRead er is = new InputStreamRead er (System.in);
                    BufferedReader bbb = new BufferedReader (is);
                    ans=String.valu eOf(bbb.readLin e());

                    }
                    catch (IOException err)
                    {

                    System.out.prin tln("Error in reading the numbers");
                    }

                    stmt.close();
                    con.close();


                    }

                    catch (IOException err)
                    {

                    System.out.prin tln("Error in reading the numbers");
                    }

                    }

                    catch (IOException err)
                    {

                    System.out.prin tln("Error in reading the numbers");
                    }

                    }
                    catch (IOException err)
                    {

                    System.out.prin tln("Error in reading the numbers");
                    }
                    }while(ans=="ye s");
                    }


                    catch (Exception err) {err.printStack Trace();}

                    }
                    }[/PHP]
                    Use the String.equals method to compare strings

                    Comment

                    Working...