Java handles excel to import data to SqlServer

My needs are:

Import users = "some users in the database do not need to import:: the pinyin corresponding to the imported name without an account
insert image description here

Solution steps

How to solve null values ​​in Excel

Excel locates null values

Excel Chinese conversion pinyin

Excel Chinese to Pinyin

Java Chinese conversion pinyin

The pinyin4j.jar package needs to be imported.
insert image description here

package www.yzq.com.tool;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class HanToPin {
    
    

	    /**
	     * 测试main方法
	     * @param args
	     */
	    public static void main(String[] args) {
    
    
	        System.out.println(ToFirstChar("汉字转换为拼音").toUpperCase()); //转为首字母大写
	        System.out.println(ToPinyin("汉字转换为拼音")); 
	    }
	    /**
	     * 获取字符串拼音的第一个字母
	     * @param chinese
	     * @return
	     */
	    public static String ToFirstChar(String chinese){
    
             
	        String pinyinStr = "";  
	        char[] newChar = chinese.toCharArray();  //转为单个字符
	        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 
	        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
	        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
	        for (int i = 0; i < newChar.length; i++) {
    
      
	            if (newChar[i] > 128) {
    
      
	                try {
    
      
	                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);  
	                } catch (BadHanyuPinyinOutputFormatCombination e) {
    
      
	                    e.printStackTrace();  
	                }  
	            }else{
    
      
	                pinyinStr += newChar[i];  
	            }  
	        }  
	        return pinyinStr;  
	    }  
	   
	    /**
	     * 汉字转为拼音
	     * @param chinese
	     * @return
	     */
	    public static String ToPinyin(String chinese){
    
              
	        String pinyinStr = "";  
	        char[] newChar = chinese.toCharArray();  
	        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();  
	        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
	        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
	        for (int i = 0; i < newChar.length; i++) {
    
      
	            if (newChar[i] > 128) {
    
      
	                try {
    
      
	                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];  
	                } catch (BadHanyuPinyinOutputFormatCombination e) {
    
      
	                    e.printStackTrace();  
	                }  
	            }else{
    
      
	                pinyinStr += newChar[i];  
	            }  
	        }  
	        return pinyinStr;  
	    }  
	    
}

java connection excel output data

Need to import jxl.jar package
insert image description here

package www.yzq.com.tool;

import java.io.File;

import jxl.Sheet;
import jxl.Workbook;

public class ExcelImport {
    
    
	public static void main(String[] args) {
    
    
		ExcelImport excelImport = new ExcelImport();
		excelImport.getAllByExcel("c://dfs.xls");
	}

	/**
	 * 查询指定目录中电子表格中所有的数据
	 * 
	 * @param file
	 *            文件完整路径
	 * @return
	 */
	public static void getAllByExcel(String file) {
    
    
		try {
    
    
			Workbook rwb = Workbook.getWorkbook(new File(file));
			Sheet rs = rwb.getSheet(0);// 或者rwb.getSheet(0)
			int clos = rs.getColumns();// 得到所有的列
			int rows = rs.getRows();// 得到所有的行

			for (int i = 1; i < rows; i++) {
    
    
				for (int j = 0; j < clos; j++) {
    
    
					// 第一个是列数,第二个是行数
					String id = rs.getCell(j++, i).getContents();// 默认最左边编号也算一列 所以这里得j++
					String name = rs.getCell(j++, i).getContents();
					String sex = rs.getCell(j++, i).getContents();
					String num = rs.getCell(j, i).getContents();
					
					System.out.println("id:" + id + " name:" + name + " sex:" + sex + " num:" + num);
				}
			}
		} catch (Exception e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

java connection database

Need to import the jar package
insert image description here

package www.yzq.com.tool;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class LinkSqlserver {
    
    
	    public static void main(String[] args) {
    
    
	        String user = "cczu";
	        String password = "****";
	        Connection conn;
	        Statement stmt;
	        ResultSet rs;
	        String url = "jdbc:sqlserver://192.168.1.99:1433;DatabaseName=QY_NTXC;";
	        String sql = "select * from t_user";
	        try {
    
    
	            // 连接数据库
	            conn = DriverManager.getConnection(url, user, password);
	            // 建立Statement对象
	            stmt = conn.createStatement();
	            // 执行数据库查询语句
	            rs = stmt.executeQuery(sql);
	            while (rs.next()) {
    
    
	                String id = rs.getString("LOGIN_NAME");
	                String name = rs.getString("NAME");
	                String score = rs.getString("EMAIL");
	                String sex = rs.getString("PHONE");
	                System.out.println("登录名: "+id+"昵称"+name+"邮箱 "+score+"电话"+sex);
	            }
	            if (rs != null) {
    
    
	                rs.close();
	                rs = null;
	            }
	            if (stmt != null) {
    
    
	                stmt.close();
	                stmt = null;
	            }
	            if (conn != null) {
    
    
	                conn.close();
	                conn = null;
	            }
	        } catch (SQLException e) {
    
    
	            e.printStackTrace();
	            System.out.println("数据库连接失败");
	        }
	    }
}

Build a relationship

Connect the above method to complete the connection

Guess you like

Origin blog.csdn.net/weixin_44077556/article/details/109240385