Back to project page droidBBpush.
The source code is released under:
This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...
If you think the Android project droidBBpush listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.arg3.examples.push.dao; /* w w w . ja va 2 s .c o m*/ import com.arg3.examples.push.enums.DeviceType; import com.arg3.examples.push.model.Device; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.inject.Inject; /** * Created by c0der78 on 2014-09-19. */ public class DeviceDAO { private Connection conn; @Inject public DeviceDAO(Connection conn) { assert (conn != null); this.conn = conn; } private static Device queryToDevice(ResultSet query) throws SQLException { String deviceType = query.getString("type"); Device device = new Device(); device.setId(query.getInt("id")); device.setType(DeviceType.get(deviceType)); device.setToken(query.getString("token")); return device; } public boolean remove(int deviceId) throws SQLException { PreparedStatement stmt = null; try { stmt = conn.prepareStatement("delete from devices where id = ?"); stmt.setInt(1, deviceId); return stmt.executeUpdate() != 0; } finally { if (stmt != null) stmt.close(); } } public List<Device> findAllDevices() throws SQLException { Statement stmt = null; List<Device> results = new ArrayList<Device>(); try { stmt = conn.createStatement(); ResultSet query = stmt.executeQuery("select * from devices"); while (query.next()) { Device device = queryToDevice(query); results.add(device); } } finally { if (stmt != null) stmt.close(); } return results; } public boolean exists(int id) throws SQLException { if (id == 0) return false; PreparedStatement stmt = null; try { stmt = conn.prepareStatement("select count(*) from devices where id = ?"); stmt.setInt(1, id); ResultSet set = stmt.executeQuery(); return set.getInt(0) > 0; } finally { if (stmt != null) stmt.close(); } } public Device findByTypeAndToken(DeviceType type, String token) throws SQLException { if (token == null || token.isEmpty()) return null; PreparedStatement stmt = null; try { stmt = conn.prepareStatement("select * from devices where type = ? and token = ?"); stmt.setString(1, type.toString()); stmt.setString(2, token); ResultSet set = stmt.executeQuery(); if (set.next()) { return queryToDevice(set); } } finally { if (stmt != null) stmt.close(); } return null; } public Device find(int id) throws SQLException { if (id == 0) return null; PreparedStatement stmt = null; try { stmt = conn.prepareStatement("select * from devices where id = ?"); stmt.setInt(1, id); ResultSet set = stmt.executeQuery(); if (set.next()) { return queryToDevice(set); } } finally { if (stmt != null) stmt.close(); } return null; } public void save(Device device) throws SQLException { PreparedStatement stmt = null; try { boolean deviceExists = exists(device.getId()); if (deviceExists) { stmt = conn.prepareStatement("update devices set type=?, token=? where id=?"); stmt.setInt(3, device.getId()); } else { stmt = conn.prepareStatement("insert into devices(type, token) values(?,?)", Statement.RETURN_GENERATED_KEYS); } stmt.setString(1, device.getType().toString()); stmt.setString(2, device.getToken()); int id = stmt.executeUpdate(); device.setId(id); } finally { if (stmt != null) stmt.close(); } } }