0

I would like to make a java code that has different outcomes that are determined by data inside of a MySQL column. I have everything set up and I can connect to the database and view data. I don't know how I would use "If" with a mysql column. Here is my code: http://pastebin.com/UsJC7Qzx

What I'm trying to do specifically: I want to make the code print "Thanks for voting" if the MySQL column "given" is equal to 0 and then it will set the column to 1. And if the column is equal to 1 it will say "Thanks again for voting."

This is just a simple base for a voting reward system I'm doing for my video game.

If you don't have very good understanding of what I am trying to say read my notes inside of the code.

4

2 回答 2

1

它看起来像这样:

while (given.next()) {
    if (given.getInt("given") > 0) {
        System.out.println("Thanks again for voting");
    } else {
        System.out.println("Thanks for voting");
    }
}

建议您将given结果集重命名为“resultSet”。

于 2012-07-24T22:37:38.627 回答
0

我想你快到了。只需要多几行。

st.executeQuery(give);返回一个结果集。如果你保证你的查询只会返回一个结果,你可以简单地做

ResultSet result = st.executeQuery(give);
if ( result.next() ) { // advances the resultset to the first result.
     int actualVal = given.getInt('given');
     if ( actualVal == 0 ) {
        System.out.println("Thanks for voting");
        // do the update here
        st.executeUpdate("update has_voted set given = 1 where ...........");
     }
     else
        System.out.println("Thanks again for voting");
}
于 2012-07-24T22:40:45.933 回答