读取多级目录下客户资料到mysql数据库中

阅读更多
package write_read;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ReadFileMysql {

public ReadFileMysql() {
}

/**
* 读取某个文件夹下的所有文件目录名称,及文件存储到数据库中
*
* @曲敏杰 2012-07-09
* @throws ClassNotFoundException
* @throws SQLException
*/
public static boolean readfile(String filepath)
throws FileNotFoundException, IOException, ClassNotFoundException,
SQLException {
try {

File file = new File(filepath);
// 如果是文件
if (!file.isDirectory()) {
System.out.println("absolutepath=" + file.getAbsolutePath());
} else if (file.isDirectory()) {
// 如果是文件夹(目录)
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
// 获取文件绝对路径
String pathStr = readfile.getAbsolutePath();
System.out.println("文件绝对路径=" + pathStr);

// 获取'\'字符所在位置下标添加到ArrayList或List中
// ArrayList list = new ArrayList();
List list = new ArrayList();

for (int j = 0; j < pathStr.length(); j++) {
if (pathStr.charAt(j) == '\\') {
list.add(j + 1);
}
}
// 获取客户名称开始下标
String khmcstart = list.get(1).toString();
int mcks = Integer.parseInt(khmcstart);
// 获取客户名称结束下标后一位
// 获取客户账号开始下标
String khmcend = list.get(2).toString();
int mcjs = Integer.parseInt(khmcend);
// 获取客户账号结束下标后一位
String khzhstart = list.get(3).toString();
int zhjs = Integer.parseInt(khzhstart);
// 获取客户名称
String khmcStr = pathStr.substring(mcks, mcjs - 1);
System.out.println("客户名称=" + khmcStr);
// 获取客户账号
String khzhStr = pathStr.substring(mcjs, zhjs - 1);
System.out.println("客户账号=" + khzhStr);
putimg(khmcStr, khzhStr, pathStr);

} else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i]);
}
}
}

} catch (FileNotFoundException e) {
System.out.println("readfile()   Exception:" + e.getMessage());
}
return true;
}

/**
* 连接db2(或mysql)数据库 把读取的目录及文件存储到数据库中
*/
public static boolean putimg(String khmc, String khzh, String path) {
try {
// 连接mysql数据库
String url = "jdbc:mysql://localhost/img?useUnicode=true&characterEncoding=gb2312";
Class.forName("com.mysql.jdbc.Driver");
String username = "root";
String password = "root";

// //连接DB2数据库
// String url = "jdbc:db2://localhost:50000/SJPT";
// Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
// String username="DB_SJPT";
// String password="sjpt";
Connection conn = DriverManager.getConnection(url, username,
password);
int returnFlag = 0;
// String picID = null;
Statement stmt = conn.createStatement();
// 生成序号
// String xhSql =
// "SELECT NEXTVAL FOR SEQ_KHXX_XH XH FROM SYSIBM.SYSDUMMY1";
// ResultSet rs = stmt.executeQuery(xhSql);
//
// if(rs.next()){
// picID = rs.getString("XH");
// }
// String strSql =
// "insert   into   t_rp_khzl (id,khmc,khzh) values ('"+picID+"','"+khmc+"','"+khzh+"')";
// stmt.execute(strSql);

PreparedStatement pstmt = null;
// String updateSql = "";
File files = new File(path);
InputStream photoStream = new FileInputStream(files);
// updateSql = "  UPDATE t_rp_khzl SET img=? where id='"+picID+"'";
String strSql = "insert   into   t_rp_khzl (khmc,khzh,img) values (?,?,?)";
pstmt = conn.prepareStatement(strSql);
pstmt.setString(1, khmc);
pstmt.setString(2, khzh);
pstmt.setBinaryStream(3, photoStream, (int) files.length());
returnFlag = pstmt.executeUpdate();

if (returnFlag >= 1) {
System.out.println("图片写入成功!");
} else {
System.out.println("图片写入失败!");
}
stmt.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

/**
* main方法入口,把"E:/客户资料"目录下的文件以二进制的形式出入到数据库中
*/
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
try {
boolean flag = readfile("E:/客户资料");
if (flag == true) {
System.out.println("文件写入已完成");
}
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}

}

}

你可能感兴趣的:(java,io,db2,mysql,write)