前两个 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. 1 bootstrapValidator使用

    1 如何使用 引入 <link href="bootstrapValidator.min.css" rel="stylesheet"> css文件 ...

  2. Mybatis 高级查询的小整理

    高级查询的整理 // resutlType无法帮助我们自动的去完成映射,所以只有使用resultMap手动的进行映射 resultMap: type 结果集对应的数据类型 id 唯一标识,被引用的时候 ...

  3. 【linux】ubuntu修改系统时间

    ubuntu修改时间步骤 ① 先把系统校验时间的程序停止掉 /lib/systemd/systemd-timesyncd systemd 开始,包括了一个名为systemd-timesyncd 的守护 ...

  4. Kinect视频中运用全身运动和人体测量统计学的人物识别技术

    摘要: 对于人物识别技术来说,动作和人体测量统计学对于光学差异并不敏感,甚至对于眼镜,头发,帽子的描述相当粗糙,现在的以步态为基础的识别技术都是基于对细节的精确描述和对步态周期的精确测量.这种方法需要 ...

  5. Lambda表达式语法进一步巩固

    上一次已经初步使用到了Lambda表达式了,这次再次对它的语法进行一下巩固,因为它实在是太重要的,所以多花时间彻底理解它是非常有必要的. 在"Java8 in Action"一书中 ...

  6. python豆知识: for和while的else语句。

    for语句,当可迭代对象耗尽后执行else语句. while循环,当条件为False后执行else. a = 1 while a != 10: a += 1 else: print(a)

  7. wordpress网站不正常显示解决办法

    第一种:自己在后台修改了wordpress网址,导致不能登陆后台. 解决办法: 1.首先我们登录MySql数据库,这个不用我教吧: 2.查看表”wp_options”的数据(你的表不一定是以”wp”开 ...

  8. CSS基础学习 16.CSS过渡

  9. 数据统计,包括mysql和MongoDB

    select ct.dt, COUNT(DISTINCT c.id) from tms_service_customer c, tms_dispatch_details d, (select DIST ...

  10. c#系统泛型委托

    Action<T> 无返回值的系统泛型委托 namespace ConsoleApp1 { public class UserInfo { public int Id { get; set ...