Results 1 to 7 of 7
  1. #1
    dking is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default Data Inconsistency

    hello guys, i have a little challenge with these:

    // first try/catch block
    try {
    String query = "insert into HeartlandAuthentication (username) values ('John')";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:MyDataSourc e", "sa", "");
    Statement stm = con.createStatement();
    int rs = stm.executeUpdate(query);
    con.close();
    }
    catch(Exception e) {
    e.printStackTrace();
    }

    // second try/catch block
    try {
    String query = "insert into HeartlandAuthentication (username) values (?)";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:MyDataSourc e", "sa", "");
    PreparedStatement pstmt = con.prepareStatement(query);
    pstmt.setString(1, this.username.getText());
    int rs = pstmt.executeUpdate();
    con.close();
    }
    catch(Exception e) {
    e.printStackTrace();
    }


    the username field in my database is varchar(15) and a JTextField username in my app where i entered the name "john". But when i query my table then

    rs.getString("username").length();

    first try/catch block = 4
    second try/catch block = 15

    why is that??

  2. #2
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Java Code:
    String query = "insert into HeartlandAuthentication (username) values (?)";
    You sql statement in second try catch is incorrect

  3. #3
    dking is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    @RichersooN, pls what is the correct statement?

  4. #4
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    It is a basic sql syntax, your (?) should be ('?')
    Enclosed with single quote

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Your sql syntax is correct. Your question however is not clear.
    To know why the length is 4 in one case and 15 on the other case is easily investigated by printing out the string that you got from the database so you know you are comparing the right results. Wrap the result in > < characters like so Sysount.out.println(">"+result+"<"); so that you can see if there are extra spaces.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by RichersooN View Post
    It is a basic sql syntax, your (?) should be ('?')
    Enclosed with single quote
    No, No, No! That ? is an parameter for a PreparedStatement is is entered as it should be. See the JDBC tutorials on PreparedStatement.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by r035198x View Post
    Your sql syntax is correct. Your question however is not clear.
    To know why the length is 4 in one case and 15 on the other case is easily investigated by printing out the string that you got from the database so you know you are comparing the right results. Wrap the result in > < characters like so Sysount.out.println(">"+result+"<"); so that you can see if there are extra spaces.
    And do the same for the result of "this.username.getText()" to be sure of what you're entering, as well.

Similar Threads

  1. Getting problem inserting data in data base
    By anupama in forum New To Java
    Replies: 4
    Last Post: 12-15-2010, 10:03 PM
  2. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 05:48 AM
  3. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 04:10 PM
  4. Replies: 0
    Last Post: 10-31-2008, 02:13 PM
  5. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:52 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
  •