前两个 insertTitle,insertOpening 是刚开始做的针对单一素材数据解析及写入的方法,后面一个 insertMaterial 是后期封装的一个可处理各种素材导入的方法,main方法中是它的一个测试用例。

package com.binye.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.junit.Test; public class InsertMeterial { private static final String driverClassName = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://192.168.17.11:3306/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8"; private static final String username = "root";
private static final String password = "****"; private static Connection conn;
public static Connection getConnection() {
if (conn == null)
{
try {
Class.forName(driverClassName);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} } return conn;
} public static void closeConnection() {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} @Test
public void insertTitle(){
QueryRunner query = new QueryRunner(); String getTagValue = "SELECT id,tag_value FROM tags";
String getTitleInfo = "SELECT `标题` FROM title"; // 获取所有标签类型
List<Object[]> tags = null;
try {
tags = query.query(getConnection(), getTagValue, new ArrayListHandler());
} catch (SQLException e) {
e.printStackTrace();
} // 获取标题
List<Object[]> titles = null;
try {
titles = query.query(getConnection(), getTitleInfo, new ArrayListHandler());
} catch (SQLException e) {
e.printStackTrace();
} // 准备标题插入素材表和关系表的预处理语句
PreparedStatement pstmtForTitle = null;
PreparedStatement pstmtForMaterialTag = null;
PreparedStatement pstmtForInsertTest = null; for (Object[] result1:titles){
String titleId = UUIDUtil.getOrigUUID();
String title = result1[0].toString(); // 向素材表中插入新标题
String titleSQL = "INSERT INTO material(id,type,value,create_time,status) VALUES( ?, 6, ?, NOW(), 0)";
try {
pstmtForTitle = (PreparedStatement) getConnection().prepareStatement(titleSQL);
pstmtForTitle.setString(1, titleId);
pstmtForTitle.setString(2, title);
pstmtForTitle.executeUpdate();
pstmtForTitle.close(); // 向标签与素材的关系表中插入数据
String tagSQL = "SELECT column FROM title WHERE `标题` = titleInfo";
// 遍历标题表中各个标签的取值情况(若等于1则插入相应关系)
for (Object[] result2:tags){
String tag = result2[1].toString();
String getStateOfEachTag = tagSQL.replace("column", "`"+ tag +"`").replace("titleInfo", "'" + title + "'");
List<Object[]> states = query.query(getConnection(), getStateOfEachTag, new ArrayListHandler());;
if (states.get(0)[0].toString().equals("1")){
String materialTagSQL = "INSERT INTO material_tags(material_id,tag_id) VALUES(?,?)";
pstmtForMaterialTag = (PreparedStatement) getConnection().prepareStatement(materialTagSQL);
pstmtForMaterialTag.setString(1, titleId);
pstmtForMaterialTag.setString(2, result2[0].toString());
pstmtForMaterialTag.executeUpdate();
pstmtForMaterialTag.close();
}
} // 查看插入情况
String insertTestSQL = "select m.id,m.status,m.type,m.value,t.tag_value" +
" from material m,material_tags mt,tags t" + " where m.id = mt.material_id and mt.tag_id=t.id and m.id=?";
pstmtForInsertTest = (PreparedStatement) getConnection().prepareStatement(insertTestSQL);
pstmtForInsertTest.setString(1,titleId);
ResultSet rs = pstmtForInsertTest.executeQuery();
int col = rs.getMetaData().getColumnCount();
System.out.println("============================");
// 逐行打印数据,字段之间以空格格开,所列字段依次是: 素材id,状态,素材类型,素材内容,标签
while (rs.next()) {
for (int i = 1; i <= col; i++) {
System.out.print(rs.getString(i) + " ");
}
System.out.println("");
}
System.out.println("============================");
pstmtForInsertTest.close(); } catch (SQLException e) {
e.printStackTrace();
} } } @Test
public void insertOpening(){
QueryRunner query = new QueryRunner(); String getTagValue = "SELECT id,tag_value FROM tags";
String getOpeningInfo = "SELECT `开场白` FROM opening"; // 获取所有标签类型
List<Object[]> tags = null;
try {
tags = query.query(getConnection(), getTagValue, new ArrayListHandler());
} catch (SQLException e) {
e.printStackTrace();
} // 获取开场白
List<Object[]> openings = null;
try {
openings = query.query(getConnection(), getOpeningInfo, new ArrayListHandler());
} catch (SQLException e) {
e.printStackTrace();
} PreparedStatement pstmtForOpening = null;
PreparedStatement pstmtForMaterialTag = null;
PreparedStatement pstmtForInsertTest = null; for (Object[] result1:openings){
String openingId = UUIDUtil.getOrigUUID();
String opening = result1[0].toString(); // 向素材表中插入新开场白
String openingSQL = "INSERT INTO material(id,type,value,create_time,status) VALUES( ?, 5, ?, NOW(), 0)";
try {
pstmtForOpening = (PreparedStatement) getConnection().prepareStatement(openingSQL);
pstmtForOpening.setString(1, openingId);
pstmtForOpening.setString(2, opening);
pstmtForOpening.executeUpdate();
pstmtForOpening.close(); // 找到相应的标签
String tagSQL = "SELECT column FROM opening WHERE `开场白` = openingInfo";
for (Object[] result2:tags){
String tag = result2[1].toString();
String getStateOfEachTag = tagSQL.replace("column", "`"+ tag +"`").replace("openingInfo", "'" + opening + "'");
List<Object[]> states = query.query(getConnection(), getStateOfEachTag, new ArrayListHandler());;
if (states.get(0)[0].toString().equals("1")){
String materialTagSQL = "INSERT INTO material_tags(material_id,tag_id) VALUES(?,?)";
pstmtForMaterialTag = (PreparedStatement) getConnection().prepareStatement(materialTagSQL);
pstmtForMaterialTag.setString(1, openingId);
pstmtForMaterialTag.setString(2, result2[0].toString());
pstmtForMaterialTag.executeUpdate();
pstmtForMaterialTag.close();
}
} // 查看插入情况
String insertTestSQL = "select m.id,m.status,m.type,m.value,t.tag_value" +
" from material m,material_tags mt,tags t" + " where m.id = mt.material_id and mt.tag_id=t.id and m.id=?";
pstmtForInsertTest = (PreparedStatement) getConnection().prepareStatement(insertTestSQL);
pstmtForInsertTest.setString(1,openingId);
ResultSet rs = pstmtForInsertTest.executeQuery();
int col = rs.getMetaData().getColumnCount();
System.out.println("============================");
// 逐行打印数据,字段之间以空格格开,所列字段依次是: 素材id,状态,素材类型,素材内容,标签
while (rs.next()) {
for (int i = 1; i <= col; i++) {
System.out.print(rs.getString(i) + " ");
}
System.out.println("");
}
System.out.println("============================");
pstmtForInsertTest.close(); } catch (SQLException e) {
e.printStackTrace();
} } } public static enum MaterialType {
TITLE,OPENING
} public static void insertMaterial(MaterialType materialType){
QueryRunner query = new QueryRunner(); String getTagValue = "SELECT id,tag_value FROM tags";
String getMaterialInfo = null;
String materialSQL = null;
String tagInMaterialSQL = null;
switch (materialType) {
case TITLE:
getMaterialInfo = "SELECT `标题` FROM title";
materialSQL = "INSERT INTO material(id,type,value,create_time,status) VALUES( ?, 6, ?, NOW(), 0)";
tagInMaterialSQL = "SELECT column FROM title WHERE `标题` = materialInfo";
break;
case OPENING:
getMaterialInfo = "SELECT `开场白` FROM opening";
materialSQL = "INSERT INTO material(id,type,value,create_time,status) VALUES( ?, 5, ?, NOW(), 0)";
tagInMaterialSQL = "SELECT column FROM opening WHERE `开场白` = materialInfo";
break;
default:
break;
} // 获取所有标签类型
List<Object[]> tags = null;
try {
tags = query.query(getConnection(), getTagValue, new ArrayListHandler());
} catch (SQLException e) {
e.printStackTrace();
} // 获取标题
List<Object[]> materials = null;
try {
materials = query.query(getConnection(), getMaterialInfo, new ArrayListHandler());
} catch (SQLException e) {
e.printStackTrace();
} // 准备标题插入素材表和关系表的预处理语句
PreparedStatement pstmtForMaterial = null;
PreparedStatement pstmtForMaterialTag = null;
PreparedStatement pstmtForInsertTest = null; for (Object[] result1:materials){
String materialId = UUIDUtil.getOrigUUID();
String material = result1[0].toString(); try {
// 向素材表中插入新标题
pstmtForMaterial = (PreparedStatement) getConnection().prepareStatement(materialSQL);
pstmtForMaterial.setString(1, materialId);
pstmtForMaterial.setString(2, material);
pstmtForMaterial.executeUpdate();
pstmtForMaterial.close(); // 向标签与素材的关系表中插入数据
// 遍历标题表中各个标签的取值情况(若等于1则插入相应关系)
for (Object[] result2:tags){
String tag = result2[1].toString();
String getStateOfEachTag = tagInMaterialSQL.replace("column", "`"+ tag +"`").replace("materialInfo", "'" + material + "'");
List<Object[]> states = query.query(getConnection(), getStateOfEachTag, new ArrayListHandler());;
if (states.get(0)[0].toString().equals("1")){
String materialTagSQL = "INSERT INTO material_tags(material_id,tag_id) VALUES(?,?)";
pstmtForMaterialTag = (PreparedStatement) getConnection().prepareStatement(materialTagSQL);
pstmtForMaterialTag.setString(1, materialId);
pstmtForMaterialTag.setString(2, result2[0].toString());
pstmtForMaterialTag.executeUpdate();
pstmtForMaterialTag.close();
}
} // 查看插入情况
String insertTestSQL = "select m.id,m.status,m.type,m.value,t.tag_value" +
" from material m,material_tags mt,tags t" + " where m.id = mt.material_id and mt.tag_id=t.id and m.id=?";
pstmtForInsertTest = (PreparedStatement) getConnection().prepareStatement(insertTestSQL);
pstmtForInsertTest.setString(1,materialId);
ResultSet rs = pstmtForInsertTest.executeQuery();
int col = rs.getMetaData().getColumnCount();
System.out.println("============================");
// 逐行打印数据,字段之间以空格格开,所列字段依次是: 素材id,状态,素材类型,素材内容,标签
while (rs.next()) {
for (int i = 1; i <= col; i++) {
System.out.print(rs.getString(i) + " ");
}
System.out.println("");
}
System.out.println("============================");
pstmtForInsertTest.close(); } catch (SQLException e) {
e.printStackTrace();
} } } public static void main(String[] args) {
System.out.println("数据插入开始");
Long start = new Date().getTime(); InsertMeterial.insertMaterial(MaterialType.TITLE);
InsertMeterial.insertMaterial(MaterialType.OPENING); //执行结束,关闭连接
closeConnection(); Long end = new Date().getTime();
System.out.println("执行时间 "+ (end - start) +" ms;"+ (end - start)/1000 +"s;"+ (end - start)/1000/60 +"m"); } }

jdbc结合commons-dbutils-1.6.jar做素材数据处理的例子的更多相关文章

  1. 高性能jdbc封装工具 Apache Commons DbUtils 1.6(转载)

    转载自原文地址:http://gao-xianglong.iteye.com/blog/2166444 前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更多的 ...

  2. java JDBC (六) org.apache.commons.dbutils 增删改

    dbutils是apache封装了JDBC的工具类,比mysql-connector更方便些 下载地址:http://commons.apache.org/proper/commons-dbutils ...

  3. java JDBC (七) org.apache.commons.dbutils 查询

    package cn.sasa.demo1; import java.sql.Connection; import java.sql.SQLException; import java.util.Li ...

  4. 写一个ORM框架的第一步(Apache Commons DbUtils)

    新一次的内部提升开始了,如果您想写一个框架从Apache Commons DbUtils开始学习是一种不错的选择,我们先学习应用这个小“框架”再把源代码理解,然后写一个属于自己的ORM框架不是梦. 一 ...

  5. Apache Commons DbUtils 快速上手

    原文出处:http://lavasoft.blog.51cto.com/62575/222771 Hibernate太复杂,iBatis不好用,JDBC代码太垃圾,DBUtils在简单与优美之间取得了 ...

  6. 【JDBC&Dbutils】JDBC&JDBC连接池&DBUtils使用方法(重要)

    -----------------------JDBC---------- 0.      db.properties文件 driver=com.mysql.jdbc.Driver url=jdbc: ...

  7. 《笔者带你剖析Apache Commons DbUtils 1.6》(转)

    前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更 多的是依靠Hibernate.Ibatis.Spring JDBC.JPA等大厂提供的持久层技术解决方案, ...

  8. JDBC工具类-DButils(QueryRunner-ResultSetHandler)

    简述: DBUtils是Java编程中的数据库操作实用工具,小巧简单实用. DBUtils封装了对JDBC的操作,简化了JDBC操作,可以少写代码. DBUtils三个核心功能: QUeryRunne ...

  9. JDBC 学习笔记(四)—— 自定义JDBC框架+Apache—DBUtils框架+事务管理+操作多表

    本文目录:       1.自定义JDBC框架 ——数据库元数据:DataBaseMetaData        2.自定义JDBC框架 ——数据库元数据:DataBaseMetaData       ...

随机推荐

  1. requests模块高级操作之proxies

    一.代理proxy 概念:代理服务器 作用:请求和响应的转发 免费代理 www.goubanjia.com 快代理 西祠代理 代理精灵(付费) 匿名度: 透明:对方服务器知道你使用代理也知道你真实ip ...

  2. C++ STL 之 函数对象

    重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象,也叫仿函数(functor),其实就是重载“()”操作符,使得类对象可以像函数那样调用.注意 ...

  3. vue cli创建脚手架

    1.用vscode打开一个文件夹.在菜单栏 点击 查看-集成终端.这里可以用其他的方法比如cmd命令符调开这个界面,但是要用cd 切到要放文件的文件夹下. 2.安装好node.js  和淘宝镜像 3. ...

  4. 13.MySQL锁机制

    锁的分类 从对数据的类型 (读\写)分: 1.读锁(共享锁):针对同一份数据,多个读操作可以同时进行而不会互相影响 2.写锁(排它锁):当前写操作没有完成前,它会阻断其他写锁和读锁 从对数据操作的粒度 ...

  5. 3.MySQL的架构介绍

    MySQL简介: 高级MySQL:mysql 内核 sql优化工程师 mysql服务器的优化 各种参数常量设定 查询语句优化 主从复制 软硬件升级 容灾备份 sql编程 完整的mysql优化需要很深的 ...

  6. ASE19团队项目alpha阶段model组 scrum7 记录

    本次会议于11月11日,19时整在微软北京西二号楼sky garden召开,持续15分钟. 与会人员:Jiyan He, Kun Yan, Lei Chai, Linfeng Qi, Xueqing ...

  7. idea自动在文件头中添加作者和创建时间

    设置路径 : File -> Settings -> Editor -> File and Code Templates 定制头模板: /** * @Author: chancy * ...

  8. 一图一知-TS之symbol

  9. HTML5新特性——1 HTML5音频

    注意:<source>同一个音乐可以添加不同格式的音频,这样就可以满足不同浏览器都能够正常播放,兼容不同浏览器. 代码示例: <!doctype html> <html ...

  10. linux基础_使用指令

    1.指令运行级别 (0)关机 (1)单用户(找回丢失密码) (2)多用户无网络服务 (3)多用户有网络服务 (4)保留 (5)图形界面 (6)重启 /etc/inittab:系统的运行级别配置之文件 ...