Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package jp.hermesian.excel2db;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.excel.XlsDataSet;
import org.dbunit.operation.DatabaseOperation;
/**
* Excelからデータベースにデータを登録するクラス.
*/
public class Excel2DB {
/**
* mainメソッド.
*/
public static void main(final String[] args) throws Exception {
loadDataFromExcel("src/main/resources/transport.xls");
}
private static void loadDataFromExcel(String xlsFileName) throws Exception {
// プロパティファイル(mysql.properties)からパラメータを読み取り
ResourceBundle rb = ResourceBundle.getBundle("mysql");
try {
Connection conn = DriverManager.getConnection(
rb.getString("DB_URL"),
rb.getString("DB_USER"),
rb.getString("DB_PASSWORD")
);
conn.setAutoCommit(true);
IDatabaseConnection connDb = new DatabaseConnection(conn);
IDataSet dataSet = new XlsDataSet(new File(xlsFileName));
DatabaseOperation.CLEAN_INSERT.execute(connDb, dataSet);
} catch (SQLException ex) {
Logger.getLogger(Excel2DB.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment