zoukankan      html  css  js  c++  java
  • main连接数据库

    package cn.gson.oasys.common;

    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.jdbc.ScriptRunner;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.*;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;


    public class ReadExcelUtils {

    public static void run() {
    try {
    // 获取数据库相关配置信息
    Properties props = Resources.getResourceAsProperties("application.properties");

    // jdbc 连接信息: 注: 现在版本的JDBC不需要配置driver,因为不需要Class.forName手动加载驱动
    String url = props.getProperty("spring.datasource.url");
    String username = props.getProperty("spring.datasource.username");
    String password = props.getProperty("spring.datasource.password");

    // 建立连接
    Connection conn = DriverManager.getConnection(url, username, password);

    Statement stmt = conn.createStatement() ;
    Statement stmt1 = conn.createStatement() ;

    String sql = "select * from aoa_user";//要执行的SQL

    /*在询数据表时,需要用到ResultSet接口,它类似于一个数据表,通过该接口的实例可以获得检索结果集,以及对应数据表的接口信息。*/
    ResultSet rs = stmt.executeQuery(sql);//

    try {
    String filepath = "C:\Users\Michael\Downloads\Telegram Desktop\a04.xlsx";
    ReadExcelUtils excelReader = new ReadExcelUtils(filepath);

    // 对读取Excel表格内容测试
    Map<Integer, Map<Integer,Object>> map = excelReader.readExcelContent();
    System.out.println("获得Excel表格的内容:");
    while (rs.next()) {
    if (rs.getString(22).equals("admin")){
    continue;
    }
    for (int i = 1; i <= map.size(); i++) {
    String[] d = map.get(i).get(4).toString().split("\.");//工号
    String name = map.get(i).get(5).toString();//工号

    if (rs.getString(22).equals(name)){
    String upSql = "UPDATE aoa_user SET work_code = '"+d[0]+"' WHERE user_name = "+"'"+rs.getString(22)+"'";
    System.out.println(upSql);
    stmt1.executeUpdate(upSql);//
    }
    }

    }
    } catch (FileNotFoundException e) {
    System.out.println("未找到指定路径的文件!");
    e.printStackTrace();
    }catch (Exception e) {
    e.printStackTrace();
    }

    // 关闭连接
    conn.close();

    // 若成功,打印提示信息
    System.out.println("====== SUCCESS ======");
    } catch (IOException | SQLException e) {
    e.printStackTrace();
    }
    }


    private Logger logger = LoggerFactory.getLogger(ReadExcelUtils.class);
    private Workbook wb;
    private Sheet sheet;
    private Row row;

    public ReadExcelUtils(String filepath) {
    if(filepath==null){
    return;
    }
    String ext = filepath.substring(filepath.lastIndexOf("."));
    try {
    InputStream is = new FileInputStream(filepath);
    if(".xls".equals(ext)){
    wb = new HSSFWorkbook(is);
    }else if(".xlsx".equals(ext)){
    wb = new XSSFWorkbook(is);
    }else{
    wb=null;
    }
    } catch (FileNotFoundException e) {
    logger.error("FileNotFoundException", e);
    } catch (IOException e) {
    logger.error("IOException", e);
    }
    }

    /**
    * 读取Excel表格表头的内容
    *
    * @param InputStream
    * @return String 表头内容的数组
    * @author zengwendong
    */
    public String[] readExcelTitle() throws Exception{
    if(wb==null){
    throw new Exception("Workbook对象为空!");
    }
    sheet = wb.getSheetAt(0);
    row = sheet.getRow(0);
    // 标题总列数
    int colNum = row.getPhysicalNumberOfCells();
    System.out.println("colNum:" + colNum);
    String[] title = new String[colNum];
    for (int i = 0; i < colNum; i++) {
    // title[i] = getStringCellValue(row.getCell((short) i));
    title[i] = row.getCell(i).getCellFormula();
    }
    return title;
    }

    /**
    * 读取Excel数据内容
    *
    * @param InputStream
    * @return Map 包含单元格数据内容的Map对象
    * @author zengwendong
    */
    public Map<Integer, Map<Integer,Object>> readExcelContent() throws Exception{
    if(wb==null){
    throw new Exception("Workbook对象为空!");
    }
    Map<Integer, Map<Integer,Object>> content = new HashMap<Integer, Map<Integer,Object>>();

    sheet = wb.getSheetAt(0);
    // 得到总行数
    int rowNum = sheet.getLastRowNum();
    row = sheet.getRow(0);
    int colNum = row.getPhysicalNumberOfCells();
    // 正文内容应该从第二行开始,第一行为表头的标题
    for (int i = 1; i <= rowNum; i++) {
    row = sheet.getRow(i);
    int j = 0;
    Map<Integer,Object> cellValue = new HashMap<Integer, Object>();
    while (j < colNum) {
    Object obj = getCellFormatValue(row.getCell(j));
    cellValue.put(j, obj);
    j++;
    }
    content.put(i, cellValue);
    }
    return content;
    }

    /**
    *
    * 根据Cell类型设置数据
    *
    * @param cell
    * @return
    * @author zengwendong
    */
    private Object getCellFormatValue(Cell cell) {
    Object cellvalue = "";
    if (cell != null) {
    // 判断当前Cell的Type
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:// 如果当前Cell的Type为NUMERIC
    case Cell.CELL_TYPE_FORMULA: {
    // 判断当前的cell是否为Date
    if (DateUtil.isCellDateFormatted(cell)) {
    // 如果是Date类型则,转化为Data格式
    // data格式是带时分秒的:2013-7-10 0:00:00
    // cellvalue = cell.getDateCellValue().toLocaleString();
    // data格式是不带带时分秒的:2013-7-10
    Date date = cell.getDateCellValue();
    cellvalue = date;
    } else {// 如果是纯数字

    // 取得当前Cell的数值
    cellvalue = String.valueOf(cell.getNumericCellValue());
    }
    break;
    }
    case Cell.CELL_TYPE_STRING:// 如果当前Cell的Type为STRING
    // 取得当前的Cell字符串
    cellvalue = cell.getRichStringCellValue().getString();
    break;
    default:// 默认的Cell值
    cellvalue = "";
    }
    } else {
    cellvalue = "";
    }
    return cellvalue;
    }

    public static void main(String[] args) {
    run();

    }
    }
  • 相关阅读:
    PHP函数include include_once require和require_once的区别
    PHP替换回车换行的三种方法
    PHP获取绝对路径dirname(__FILE__)和__DIR__比较
    jQuery实现倒计时重新发送短信验证码功能示例
    js人民币转大写
    js前端数据验证JS工具
    安卓动画学习笔记
    ActivityNotFoundException: No Activity found to handle Intent
    Android笔记
    再次踩bug:遍历删除list(java.util.ConcurrentModificationException)
  • 原文地址:https://www.cnblogs.com/michaelcnblogs/p/12209890.html
Copyright © 2011-2022 走看看