Created
June 19, 2014 22:09
-
-
Save yuriwoof/610709ea8da4ec1b91e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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