Results 1 to 5 of 5
  1. #1
    pbaudru is offline Member
    Join Date
    Jan 2010
    Location
    Athus - Belgium
    Posts
    23
    Rep Power
    0

    Smile SQL statement doesn't work....

    Hi all I'm new on JAVA and I'd like your help please.

    I have this....

    public static String connection2(String nom, String prenom){

    String retchemincv = null;
    Connection conn = null;
    Statement stmt = null;
    ResultSet rsch = null;
    System.out.println(nom);
    System.out.println(prenom);

    try{
    Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver");

    String connectionString = "jdbc:sqlserver://localhost;database=eureka;user=pbaudru;"
    + "password=testlogin";

    conn = DriverManager.getConnection(connectionString);
    stmt = conn.createStatement();

    int nullnom = nom.length();
    System.out.println(nom);//THIS PRINT WILL SHOW BAUDRU

    if(nullnom != 0){

    rsch = stmt.executeQuery("SELECT chemin_CV FROM CANDIDAT WHERE NOM = '"+nom+"'");

    //THE PROBLEM IS THAT WHEN I TRY TO USE THE nom VARIABLE I CAN NOT READ THE EXPECTED ROW ON DB.


    while (rsch.next()){

    retchemincv=rsch.getString("chemin_CV");

    }

    return retchemincv;

    ---------------------------

    If I use 'BAUDRU' on my SQL statement directly it works well.....

    rsch = stmt.executeQuery("SELECT chemin_CV FROM CANDIDAT WHERE NOM = 'BAUDRU' ");

    So how can I use the variable 'nom' in my SQl statement ?

    Best regards, Pierre.

  2. #2
    slavamk is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    Java Code:
    PreparedStatement stmt = conn.prepareStatement("SELECT chemin_CV FROM CANDIDAT WHERE NOM = ?");
    stmt.setString(1, nom);
    ResultSet rs = stmt.executeQuery();

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    use trim() for nom...
    Ramya:cool:

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

    Default

    ...as well as the aforementioned prepared statement...:)

    For the whitespace (ie trim()) possibility, try:
    System.out.println("##" + nom + "##");
    insetad, just to see if there's anything non-obvious hiding in there.

  5. #5
    pbaudru is offline Member
    Join Date
    Jan 2010
    Location
    Athus - Belgium
    Posts
    23
    Rep Power
    0

    Smile

    Hi guys, thanks a lot for your messages....

    Now its working !

    Regards. Pierre.

Similar Threads

  1. add an If Else statement and......uh????
    By sonny in forum New To Java
    Replies: 6
    Last Post: 03-04-2010, 06:57 PM
  2. The if Statement, need some help!
    By Keno777 in forum New To Java
    Replies: 6
    Last Post: 10-24-2009, 12:53 AM
  3. If statement, please help??
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 11-23-2008, 02:58 PM
  4. Help with if statement
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:53 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

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