Results 1 to 3 of 3

Thread: Help with JDBC

  1. #1
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default Help with JDBC

    Could someone please tell me the correct syntax for making a Prepaired statement. I need to insert an int (i) into the sql, as well as sort by alphabetical order.

    This is what I figured it should be:
    Java Code:
    PreparedStatement sql = "SELECT Results.ID, Results.Race_Number, Results.Runner_ID, Results.Time, Results.Position, Runners.Runner_Title, Runners.Runner_FirstName, Runners.Runner_FamilyName, Runners.Runner_Points" +
     	   "FROM Runners INNER JOIN Results ON Runners.Runner_ID=Results.Runner_ID" +
     	   "WHERE (Race_Number ="+ i ") ORDER BY Runners.Runner_FamilyName";
    Eric

  2. #2
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default RE: Help with JDBC

    Check out this link Prepared Statements

    Also, I *think* your code should work, provided you change this:

    ..."+ i ")...

    to this:

    ....="+ i + ")......

    Marcus:cool:

  3. #3
    Daniel is offline Member
    Join Date
    Jun 2007
    Posts
    91
    Rep Power
    0

    Default RE: Help with JDBC

    Also, you may want to simplify things a bit if you're using PreparedStatements. It takes a little more code but, especially if you start dealing with Strings, it makes your life much simpler.

    Something instead like:
    Java Code:
    int raceNumber = 123;
     String sql = "SELECT Results.ID, Results.Race_Number, Results.Runner_ID, Results.Time, Results.Position, " + 
               "Runners.Runner_Title, Runners.Runner_FirstName, Runners.Runner_FamilyName, Runners.Runner_Points" +
      	   "FROM Runners INNER JOIN Results ON Runners.Runner_ID=Results.Runner_ID" +
      	   "WHERE (Race_Number =?) ORDER BY Runners.Runner_FamilyName";
     PreparedStatement stmt = connection.prepareStatement( sql );
     stmt.setInt( 1, raceNumber );
     stmt.executeQuery();
    Daniel:o

Similar Threads

  1. Replies: 0
    Last Post: 04-01-2008, 11:17 AM
  2. Jdbc
    By siwa in forum JDBC
    Replies: 4
    Last Post: 11-09-2007, 08:31 AM
  3. Replies: 0
    Last Post: 09-28-2007, 01:56 PM
  4. JDBC problem
    By Swamipsn in forum New To Java
    Replies: 1
    Last Post: 08-13-2007, 07:05 PM
  5. Ha-jdbc 2.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-17-2007, 06:40 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
  •