SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast kennyisnotdead's Avatar
    Join Date
    Apr 2002
    Location
    Massachusetts
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting a value, incrementing then updating

    I have been trying to figure how to get the value of an integer, incrementing it by 1. Then I wanted to update it in the table. I am using JSP and MySQL.

    I can get it, increment it. But thats as far as I can get it to work.
    Code::

    <%
    Class.forName("org.gjt.mm.mysql.Driver");
    Connection myConn = DriverManager.getConnection("MY_DATASOURCE);

    Statement stmt = myConn.createStatement();
    ResultSet RS = stmt.executeQuery("SELECT visits FROM counter");

    if (RS != null) {
    while (RS.next() ) {
    int total_visits = RS.getInt("visits");

    int addVisit = total_visits++;

    %>

    Hits .: <%= total_visits %>

    <%
    }
    }
    stmt.close();
    myConn.close();
    %>

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    why not do it all in sql?

    do an update --

    update counter set visits = visits + 1

    followed by a select --

    select visits from counter

    you could also do these in the reverse order, and get the pre-increment value, which is what you said you wanted...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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