📄 querydatabase.java~1~
字号:
package mmsproject;
import java.sql.*;
import java.io.*;
import javax.swing.*;
import java.util.*;
/**
* <p>Title: 彩信发送接收项目</p>
* <p>Description: 用于查询数据库中有无需要发送的信息,如果有,则将其发送</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: data link</p>
* @author tomato
* @version 1.0
*/
public class QueryDataBase
implements Runnable {
private int nSendId = 0;
//控制线程执行的标记
public boolean boolFlag = true;
//创建用于发送彩信的类
MyMMSender sender = new MyMMSender();
public QueryDataBase() {
}
/**
* 用于不断查询数据库的线程执行的方法
*/
public void run() {
//不断查询数据库有无需要发送的彩信
while (boolFlag) {
searchForSend();
}
}
private void searchForSend() {
//创建查询语句
String strSqlSend = "SELECT * FROM TB_SEND WHERE FlagSend=?";
String strSqlBinary = "SELECT * FROM TB_BINARYCONTENT WHERE SendId=?";
String strSqlText = "SELECT * FROM TB_TXTCONTENT WHERE SendId=?";
Connection conn = null;
Connection connContent = null;
PreparedStatement stmt = null;
PreparedStatement stmtContent = null;
//创建结果集
ResultSet rsSend = null;
ResultSet rsBinaryContent = null;
ResultSet rsTextContent = null;
try {
conn = MySqlConnection.getSqlConnection();
stmt = conn.prepareStatement(strSqlSend);
stmt.setInt(1, 0);
rsSend = stmt.executeQuery();
//取得tb_send的数据
while (rsSend.next()) {
nSendId = rsSend.getInt("SendId");
sender.nSendId = nSendId;
sender.strServiceCode = rsSend.getString("ServiceCode");
sender.intMMContentType = rsSend.getInt("MMContentType");
sender.intMessageClass = rsSend.getInt("MessageClass");
sender.strTitle = rsSend.getString("Title");
sender.strRecvPhoneNum = rsSend.getString("RecvPhoneNum");
sender.strPrivacyNum = rsSend.getString("PrivacyNum");
sender.strCopyNum = rsSend.getString("CopyNum");
sender.boolIsShowPhone = rsSend.getBoolean("IsShowPhone");
//sender.dateTimeToSend = rsSend.getDate("TimeToSend");
//sender.dateSendTime = rsSend.getDate("SendTime");
//如果有需要发送的彩信数据
if (nSendId != 0) {
//获取需要发送的二进制数据
connContent = MySqlConnection.getSqlConnection();
stmtContent = connContent.prepareStatement(strSqlBinary);
stmtContent.setInt(1, nSendId);
rsBinaryContent = stmtContent.executeQuery();
//将数据存储以备传入Sender类发送
//1.创建hashtable存储数据
Hashtable hashContent = new Hashtable();
//2.将数据存入hashtable
while (rsBinaryContent.next()) {
int byteSum = 0;
int bytesRead = 0;
//创建128k缓存
byte[] buffer = new byte[128 * 1024];
//取得内容id
String strContentId = rsBinaryContent.getString("ContentId");
//取得内容数据
InputStream is = rsBinaryContent.getBinaryStream("BinaryContent");
while ( (bytesRead = is.read(buffer)) != -1) {
byteSum += bytesRead;
}
is.close();
//取得内容类型
String strContentType = rsBinaryContent.getString("ContentType");
//将内容封装再 hashtable中
//1.首先将数据以及数据类型放入vector中
Vector vectorContentAndType = new Vector();
vectorContentAndType.add(buffer);
vectorContentAndType.add(strContentType);
//2.将id以及vector存入hashtable
hashContent.put(strContentId, vectorContentAndType);
}
//3.将数据传入sender
sender.hashBinaryContentAndId = hashContent;
stmtContent.close();
//获取需要发送的文本数据
stmtContent = connContent.prepareStatement(strSqlText);
stmtContent.setInt(1, nSendId);
rsTextContent = stmtContent.executeQuery();
//将数据存储以备传入Sender类发送
//if(rsTextContent.getRow())
while(rsTextContent.next()) {
sender.strTextContentId = rsTextContent.getString("ContentId");
sender.strTextContent = rsTextContent.getString("TextContent");
}
stmtContent.close();
}
//发送信息
try{
sender.send();
}catch(Exception e){
e.printStackTrace();
}
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭数据库资源
MySqlConnection.closeSqlResultSet(rsBinaryContent);
MySqlConnection.closeSqlResultSet(rsTextContent);
MySqlConnection.closeSqlResultSet(rsSend);
MySqlConnection.closeSqlConnection(conn);
MySqlConnection.closeSqlConnection(connContent);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -