📄 noticeserviceimpl.java
字号:
/*
* Created on 2005-10-5
*
*/
package net.mcool.www.impl;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import net.mcool.www.bean.MessageBean;
import net.mcool.www.factory.ServiceFactory;
import net.mcool.www.infc.INoticeService;
import net.mcool.www.util.db.DbOperation;
/**
* @author lbj
*
*/
public class NoticeServiceImpl implements INoticeService {
/*
* (non-Javadoc)
*
* @see net.mcool.www.infc.INoticeService#addMessage(net.mcool.www.bean.MessageBean)
*/
public boolean addMessage(MessageBean message) {
DbOperation dbOp = new DbOperation();
if (!dbOp.init()) {
return false;
}
String query = "INSERT INTO shortmessage(mobile, type, content, url, addtime, status) VALUES(?, ?, ?, ?, now(), ?)";
if (!dbOp.prepareStatement(query)) {
dbOp.release();
return false;
}
PreparedStatement pstmt = dbOp.getPStmt();
try {
pstmt.setString(1, message.getMobile());
pstmt.setInt(2, message.getType());
pstmt.setString(3, message.getContent());
pstmt.setString(4, message.getUrl());
pstmt.setInt(5, message.getStatus());
} catch (SQLException e) {
e.printStackTrace();
dbOp.release();
return false;
}
if (!dbOp.executePstmt()) {
dbOp.release();
return false;
}
dbOp.release();
return true;
}
/*
* (non-Javadoc)
*
* @see net.mcool.www.infc.INoticeService#addMessageHistory(net.mcool.www.bean.MessageBean)
*/
public boolean addMessageHistory(MessageBean message) {
DbOperation dbOp = new DbOperation();
if (!dbOp.init()) {
return false;
}
String query = "INSERT INTO shortmessage_history(mobile, type, content, url, addtime, status) VALUES(?, ?, ?, ?, ?, ?)";
if (!dbOp.prepareStatement(query)) {
dbOp.release();
return false;
}
PreparedStatement pstmt = dbOp.getPStmt();
try {
pstmt.setString(1, message.getMobile());
pstmt.setInt(2, message.getType());
pstmt.setString(3, message.getContent());
pstmt.setString(4, message.getUrl());
pstmt.setString(5, message.getAddTime());
pstmt.setInt(6, message.getStatus());
} catch (SQLException e) {
e.printStackTrace();
dbOp.release();
return false;
}
if (!dbOp.executePstmt()) {
dbOp.release();
return false;
}
dbOp.release();
return true;
}
/*
* (non-Javadoc)
*
* @see net.mcool.www.infc.INoticeService#deleteMessage(java.lang.String)
*/
public boolean deleteMessage(String condition) {
DbOperation dbOp = new DbOperation();
if (!dbOp.init()) {
return false;
}
String query = "DELETE FROM shortmessage";
if (condition != null) {
query += " WHERE " + condition;
}
if (!dbOp.executeUpdate(query)) {
dbOp.release();
return false;
}
dbOp.release();
return true;
}
/*
* (non-Javadoc)
*
* @see net.mcool.www.infc.INoticeService#getMessage(java.lang.String)
*/
public MessageBean getMessage(String condition) {
MessageBean message = null;
DbOperation dbOp = new DbOperation();
if (!dbOp.init()) {
return null;
}
String query = "SELECT * FROM shortmessage";
if (condition != null) {
query += " WHERE " + condition;
}
query += " LIMIT 0, 1";
ResultSet rs = dbOp.executeQuery(query);
try {
if (rs.next()) {
message = new MessageBean();
message.setId(rs.getInt("id"));
message.setAddTime(rs.getString("addtime"));
message.setContent(rs.getString("content"));
message.setMobile(rs.getString("mobile"));
message.setStatus(rs.getInt("status"));
message.setUrl(rs.getString("url"));
message.setType(rs.getInt("type"));
}
} catch (SQLException e) {
e.printStackTrace();
}
dbOp.release();
return message;
}
public static void main(String[] args) {
INoticeService service = ServiceFactory.createNoticeService();
for (int i = 0; i < 50; i++) {
MessageBean message = new MessageBean();
message.setContent("测试信息测试信息测试信息测试信息");
message.setMobile("13521262171");
if (i % 2 == 0) {
message.setType(MessageBean.WAP_PUSH);
} else {
message.setType(MessageBean.SMS);
}
message.setUrl("wap.joycool.net");
if (service.addMessage(message)) {
System.out.println("success.");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -