1: 从mongoDB读取数据并写入到mysql中
<span style="font-size:18px;">1:对于连接带有密码的MongoDB需要导入mongo-java-driver-3.2.1的jar包,否则调用
client = new MongoClient(new MongoClientURI(url))会提示查看配置项,等问题。
2:对于MongoDB查询出的数据,存为List<Document> 需要引入import org.bson.Document;
引入上述需要导入json.jar的包</span>
关于java mysql 的插入和 读取
<span style="font-size:18px;color:#000000;">import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.HashSet;
import java.util.List;
import org.bson.Document;
public class ClusterCenter {
public static void main(String[] args) {
HashSet<String> set = new HashSet<String>();
/* MongoDB的查询数据 */
List<Document> list = ConnecMongo.findColls("ClusterCenter");
/* Mysql的插入 */
String insert = "insert into ClusterCenter (_id,TbmId,op_num,x,y) values (?,?,?,?,?)";
String find = "select *from ClusterCenter";
Connection connection = DBUtil.getConnection();
set=DBUtil.find(find);
try {
PreparedStatement pre = connection.prepareStatement(insert);
for (int i = 0; i < list.size(); i++) {
Document dc = list.get(i);
if (!set.contains(dc.getString("_id"))) {
pre.setString(1, dc.getString("_id"));
pre.setString(2, dc.getString("TbmId"));
pre.setInt(3, dc.getInteger("op_num"));
pre.setDouble(4, dc.getDouble("x"));
pre.setDouble(5, dc.getDouble("y"));
pre.addBatch();
}
}
pre.executeBatch();
System.out.println("插入成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(connection);
}
}
}</span>
其中的查询代码为
<span style="font-size:18px;color:#000000;">/* 查询所有数据并仿制在set中*/
public static HashSet<String> find(String sql){
HashSet<String> set = new HashSet<String>();
Connection connection = DBUtil.getConnection();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
set.add(rs.getString("_id"));
}
} catch (Exception e) {
// TODO: handle exception
}
return set;
}</span>
(注意:在连接mysql的过程中,要使用Connection需要使用import java.sql.*;所属的。此外,mysql的连接操作完成后,最好进行关闭。)
2:文件的下载
<span style="color:#3333FF;">1: 前台js文档进行下面定义</span>
<span style="color:#000000;">$('#dg').datagrid({
toolbar: [
{
text: '导出',
iconCls: 'icon-download',
handler:function()
{
//下载事件
window.location.href = basePath + 'sysExecute/downLogAction.action';
}
}
]
})</span>
添加事件和导出的按钮
2: action部分进行数据下载和创建文件
<span style="color:#000000;">response.setContentType("application/x-xls");
resp.setHeader("Content-Disposition", "attachment;filename=123.xls"); //接下来考虑如何变成动态的
OutputStream os = resp.getOutputStream();
sysExecuteService.excel(os,list);
</span>
生成excel是调用jxl.jar的包来实现
<span style="color:#000000;">
public void excel(OutputStream os,List<Document> list) throws Exception{
WritableWorkbook workbook=null;
WritableSheet sheet= null;
workbook=Workbook.createWorkbook(os);
sheet=workbook.createSheet("sheetName", 0);
sheet.addCell(new Label(0, 0, "用户名"));
sheet.addCell(new Label(1, 0, "真实姓名"));
sheet.addCell(new Label(2, 0,"用户角色") );
sheet.addCell(new Label(3, 0,"公司" ));
sheet.addCell(new Label(4, 0, "时间"));
sheet.addCell(new Label(5, 0, "日志类型"));
sheet.addCell(new Label(6, 0, "文件名称"));
sheet.addCell(new Label(7, 0, "IP"));
sheet.addCell(new Label(8, 0, "消息"));
for (int i = 0; i < list.size(); i++) {
Document dc=list.get(i);
sheet.addCell(new Label(0, i+1, dc.getString("userName")));
sheet.addCell(new Label(1, i+1, dc.getString("useRealName")));
sheet.addCell(new Label(2, i+1, dc.getString("roleName")));
sheet.addCell(new Label(3, i+1, dc.getString("company")));
sheet.addCell(new Label(4, i+1, dc.getString("op_time")));
sheet.addCell(new Label(5, i+1, dc.getString("op_type")));
sheet.addCell(new Label(6, i+1, dc.getString("_id")));
sheet.addCell(new Label(7, i+1, dc.getString("ip")));
sheet.addCell(new Label(8, i+1, dc.getString("msg")));
}
workbook.write();
workbook.close();
}
}</span>
3:总结
两个action的方法不能直接调用。
对于echarts所做的图表格的显示问题。以下几点需要掌握
1: legend: {
left: 10,
data: ['登录', '上传', '下载']
},
series: [
{
name: '登录',
type: 'line',
data: login
},{
name: '上传',
type: 'line',
data: upload
},
{
name: '下载',
type: 'line',
data: download
}
]
};
其中data的名字和series的name应该一样。不然不显示指示栏。
2: 对于后台action使用了在action传入sql语句的方法。值得学习。多向同事请教。
编程需要激情和谨慎!!!!