aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/server/AdsQueries.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-06-27 10:33:04 +0300
committerGravatar Vitaly Takmazov2016-06-27 10:33:04 +0300
commit9e740f48cca7332c67a165c6cf60e09bc549e686 (patch)
tree08b35a43e538bb50679080c9fc4ee42c716f18eb /src/main/java/com/juick/server/AdsQueries.java
parenteca30236892175d7071e6d0d39f474148d655ffa (diff)
more spring-jdbc
Diffstat (limited to 'src/main/java/com/juick/server/AdsQueries.java')
-rw-r--r--src/main/java/com/juick/server/AdsQueries.java40
1 files changed, 18 insertions, 22 deletions
diff --git a/src/main/java/com/juick/server/AdsQueries.java b/src/main/java/com/juick/server/AdsQueries.java
index ad9b7149..06590817 100644
--- a/src/main/java/com/juick/server/AdsQueries.java
+++ b/src/main/java/com/juick/server/AdsQueries.java
@@ -17,11 +17,8 @@
*/
package com.juick.server;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.SQLException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.jdbc.core.JdbcTemplate;
/**
*
@@ -29,27 +26,26 @@ import java.util.logging.Logger;
*/
public class AdsQueries {
- private static final Logger logger = Logger.getLogger(AdsQueries.class.getName());
-
- public static int getAdMID(Connection sql, int uid) {
+ public static int getAdMID(JdbcTemplate sql, int uid) {
if (uid > 0) {
- return SQLHelpers.getInt(sql, "SELECT message_id FROM ads_messages WHERE message_id NOT IN (SELECT message_id FROM ads_messages_log WHERE user_id=? AND ts>UNIX_TIMESTAMP()-60*60*24 GROUP BY message_id HAVING COUNT(*)>2) ORDER BY RAND() LIMIT 1", uid, 0);
+ try {
+ return sql.queryForObject("SELECT message_id FROM ads_messages " +
+ "WHERE message_id NOT IN (SELECT message_id FROM ads_messages_log WHERE user_id=? " +
+ "AND ts>UNIX_TIMESTAMP()-60*60*24 GROUP BY message_id HAVING COUNT(*)>2) ORDER BY RAND() LIMIT 1",
+ Integer.class, uid);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
+ }
} else {
- return SQLHelpers.getInt(sql, "SELECT message_id FROM ads_messages ORDER BY RAND() LIMIT 1", 0);
+ try {
+ return sql.queryForObject("SELECT message_id FROM ads_messages ORDER BY RAND() LIMIT 1", Integer.class);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
+ }
}
}
- public static void logAdMID(Connection sql, int uid, int mid) {
- PreparedStatement stmt = null;
- try {
- stmt = sql.prepareStatement("INSERT INTO ads_messages_log(user_id,message_id,ts) VALUES (?,?,UNIX_TIMESTAMP())");
- stmt.setInt(1, uid);
- stmt.setInt(2, mid);
- stmt.executeUpdate();
- } catch (SQLException e) {
- logger.log(Level.SEVERE, "sql exception", e);
- } finally {
- Utils.finishSQL(null, stmt);
- }
+ public static void logAdMID(JdbcTemplate sql, int uid, int mid) {
+ sql.update("INSERT INTO ads_messages_log(user_id,message_id,ts) VALUES (?,?,UNIX_TIMESTAMP())", uid, mid);
}
}