/**
* 读取EXCEL信息生成sql脚本
*
* @param sheetNumber 从Excel的第一个sheet页开始
* @param CellCount 列数
* @param filePath Excel 文件存放路径
* @param excelName Excel文件名称
* @param sqlName 生成的SQL文件名称
* @param insertSQL 生成的sql数据库表语句前缀
*
*/
public static void createInsertSqlByExcelUtil(int sheetNumber,int cellCount,String filePath ,String excelName,String sqlName,String insertSQL) {
HSSFWorkbook workbook = null;
String prefix = SqlUtils.class.getResource("/").getPath();
String xlsPath = (new java.io.File(prefix)).getParent()+filePath;
try {
// 获取工作薄workbook
workbook = new HSSFWorkbook(new FileInputStream(xlsPath
+ excelName));
} catch (FileNotFoundException e) {
logger.error(" 文件无法找到 :"+filePath+excelName);
e.printStackTrace();
} catch (IOException e) {
logger.error(" 文件读取失败 :"+filePath+excelName);
e.printStackTrace();
}
// 获得指定的sheet
HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
// 获得sheet总行数 从第1行开始读起
int rowCount = getExeceRowNumber(1,sheet);
logger.info("Found excel rows count:" + rowCount);
if (rowCount < 1) {
return;
}
FileWriter fw;
BufferedWriter writer;
try {
fw = new FileWriter(xlsPath + sqlName);
writer = new BufferedWriter(fw);
StringBuffer outsf = new StringBuffer();
StringBuffer inner = new StringBuffer("");
outsf.append(insertSQL);
// 遍历行row
for (int rowIndex = 1; rowIndex <= rowCount; rowIndex++) {
StringBuffer tmpsb = new StringBuffer();
// 获得行对象
HSSFRow row = sheet.getRow(rowIndex);
// 判断空行 如果行第一个元素为空则退出
if (row == null)
break;
if (row.getCell(0) == null)
break;
inner.append("(");
// 遍历列cell
for (int cellIndex = 0; cellIndex < cellCount; cellIndex++) {
HSSFCell cell = row.getCell(cellIndex);
Object cellStr = getCellString(cell);
// 判断最后列单元格是否为空
//if (cellStr == null)
//break;
tmpsb.append(cellStr);
if (cellIndex < cellCount)
tmpsb.append(",");
}
String ss = tmpsb.toString();
if(ss.length()>1)
inner.append(ss.substring(0, ss.length() - 1));
if (rowIndex == rowCount) {
inner.append(");");
} else {
inner.append("),");
}
}
writer.write(outsf.append(inner.toString()).toString() + "\r\n");
writer.flush();
writer.close();
logger.info(" 数据库脚本生成成功 :"+filePath+sqlName);
} catch (Exception e) {
logger.error(" 数据库脚本生成失败 !!! : "+filePath+excelName);
e.printStackTrace();
}
}
getExcelRowNumber代码:
/**
* 获得EXCEL 非空行的真正行数
* @param statRow 从第几行开始读起
* @param sheet excel sheet页
* @return
*/
private static int getExeceRowNumber(int statRow ,HSSFSheet sheet){
int rowNumber = 0;
int tmpRowCount = sheet.getLastRowNum();
if (tmpRowCount < 1) {
return rowNumber;
}
for (int rowIndex = statRow; rowIndex <= tmpRowCount; rowIndex++) {
HSSFRow row = sheet.getRow(rowIndex);
// 判断空行 如果行第一个元素为空则退出
if (row == null)
break;
Cell firstCell = row.getCell(0);
if (firstCell != null){
if(!"".equals(firstCell.toString().trim())){
rowNumber = rowIndex;
}
}
}
return rowNumber;
}getCellString方法代码:
/**
* Excel cell 类型解析
*
* @param cell
* @return
*/
private static Object getCellString(HSSFCell cell) {
// TODO Auto-generated method stub
Object result = null;
if (cell != null) {
// 单元格类型:Numeric:0,String:1,Formula:2,Blank:3,Boolean:4,Error:5
int cellType = cell.getCellType();
switch (cellType) {
case HSSFCell.CELL_TYPE_STRING:
result = "'" + cell.getRichStringCellValue().getString() + "'";
break;
case HSSFCell.CELL_TYPE_NUMERIC:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = null;
break;
case HSSFCell.CELL_TYPE_ERROR:
result = null;
break;
default:
System.out.println("枚举了所有类型");
break;
}
}
return result;
}在生成完sql语句之后,我们还需要在进入游戏时就进行对游戏相关参数的加载即初始化玩家的一些信息。这时候就需要用到java.lang.Process这个类,此类是通过ProcessBulder或者Runtime生成的一个子进程类。子进程,顾名思义,是附在父进程之上的,换句话说我们必须等待父进程执行完之后才可以执行这个子进程。相关内容可以查看java的API文档。此加载初始化信息的方法如下:
/**
* 调用 mysql 命令执行 配置表的初始化
*
* @param host
* @param userName
* @param password
* @param dbName
* @param filePath 脚本文件路径
* @param sqlFileNme 执行的sql脚本文件名字
* @param currSystem 项目所在系统环境 0:windows 1:linux
*/
private void backSql(String host, String userName, String password,
String dbName, String filePath, String sqlFileNme, int currSystem) {
SimpleDateFormat df = new SimpleDateFormat("yyyy_MM_dd");// 设置日期格式
String prefix = PoiAllExcelConfigData.class.getResource("/").getPath();
String insertSqlFile = (new java.io.File(prefix)).getParent()
+ filePath + sqlFileNme;
String backupSqlFile = (new java.io.File(prefix)).getParent()
+ filePath + df.format(new Date()) + "_" + sqlFileNme;
String mysql = "mysql -h" + host + " -u" + userName.trim() + " -p"
+ password.trim() + " --default-character-set=utf8 -D "
+ dbName.trim() + " < " + insertSqlFile;
String mysqldump = "mysqldump -h" + host + " -u" + userName.trim()
+ " -p" + password.trim() + " --default-character-set=utf8 "
+ dbName.trim() + " "
+ sqlFileNme.substring(0, sqlFileNme.length() - 4) + " > "
+ backupSqlFile;
Process pw = null;
InputStream inerr;
BufferedReader brerr;
String lineerr = null;
try {
// 备份数据库脚本
if (true) {
switch (currSystem) {//需要对操作系统进行判断,因为windows系统和linux系统执行mysql语句是不一样的
case CURR_SYS_WINDOWS:
pw = java.lang.Runtime.getRuntime().exec(
"cmd /C " + mysqldump);
inerr = pw.getErrorStream();
brerr = new BufferedReader(new InputStreamReader(inerr));
while ((lineerr = brerr.readLine()) != null) {
logger.error(" ERROR 数据库备份脚本的命令错误输出tableName is : "+ sqlFileNme + " ================ "+ lineerr);
brerr.close();
inerr.close();
break;
}
brerr.close();
inerr.close();
logger.info(" 执行 backup 数据库脚本完成 :" + backupSqlFile);
break;
case CURR_SYS_LINUX:
pw = java.lang.Runtime.getRuntime().exec(
new String[] { "sh", "-c", mysqldump });
inerr = pw.getErrorStream();
brerr = new BufferedReader(new InputStreamReader(inerr));
while ((lineerr = brerr.readLine()) != null) {
logger.error(" ERROR 数据库备份脚本的命令错误输出tableName is : "+ sqlFileNme + " ================ "+ lineerr);
brerr.close();
inerr.close();
break;
}
brerr.close();
inerr.close();
logger.info(" 执行 backup 数据库脚本完成 :" + backupSqlFile);
break;
default:
logger.error(" ERROR 没有成功执行脚本命令 : " + backupSqlFile+ " SYSTEM IS : " + currSystem);
break;
}
}
// 导入新生成的sql脚本
switch (currSystem) {
case CURR_SYS_WINDOWS:
pw = java.lang.Runtime.getRuntime().exec("cmd /C " + mysql);
inerr = pw.getErrorStream();
brerr = new BufferedReader(new InputStreamReader(inerr));
while ((lineerr = brerr.readLine()) != null) {
logger.error(" ERROR 执行数据库脚本的命令错误输出tableName is : " + sqlFileNme+ " ================ " + lineerr);
brerr.close();
inerr.close();
break;
}
brerr.close();
inerr.close();
logger.info(" 执行 insert 数据库脚本完成 :" + filePath+ sqlFileNme);
break;
case CURR_SYS_LINUX:
pw = java.lang.Runtime.getRuntime().exec(
new String[] { "sh", "-c", mysql });
inerr = pw.getErrorStream();
brerr = new BufferedReader(new InputStreamReader(inerr));
while ((lineerr = brerr.readLine()) != null) {
logger.error(" ERROR 执行数据库脚本的命令错误输出tableName is : " + sqlFileNme+ " ================ " + lineerr);
brerr.close();
inerr.close();
break;
}
brerr.close();
inerr.close();
logger.info(" 执行 insert 数据库脚本完成 :" + filePath+ sqlFileNme);
break;
default:
logger.error(" ERROR SWITCH 没有成功执行脚本命令 : " + sqlFileNme+ " SYSTEM IS : " + currSystem);
break;
}
} catch (Throwable e) {
logger.error(" ERROR backSql() 异常 :" + filePath + sqlFileNme);
e.printStackTrace();
}
}
分享到:
2011-12-08 16:33
浏览 954
评论