2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Javaのシステムでcsvからインポートする機能を実装する方法

Posted at

まずはJSPから。

formは以下のように書く。

<form id="" action="" method="POST" enctype="multipart/form-data">

inputのタイプはfileに

<div class="tr">
	<label for="csv" class="th">csv:</label>
	<div class="td">
		<input type="file" id="csv" name="csv" required>
	</div>
</div>

JSPは以上です。
サーブレットはアノテーションのすぐ下に以下を追記

@MultipartConfig(
	maxFileSize=10000000,
	maxRequestSize=10000000,
	fileSizeThreshold=10000000
)

doPostを以下のように記述

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// TODO Auto-generated method stub

	// 文字化け対策
	request.setCharacterEncoding("UTF-8");
	response.setContentType("text/html;charset=UTF-8");

	// 接続情報
	db = DB接続情報を代入;
	Connection conn = null;

	// SQL
	PreparedStatement pstmt1 = null;

	// 送信情報の取得
	Part csv = request.getPart("csv");
	BufferedReader br = null;

	try {
		// データベース接続情報取得
		conn = db.getConnection();

		// csv読み込み
		InputStream is = csv.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		br = new BufferedReader(isr);
		String line;
					
		while ((line = br.readLine()) != null) {
			String[] data = line.split(",");

			// SQL実行
			String sql1 = "INSERT INTO table(aaa,bbb,ccc) VALUES(?,?,?)";
			pstmt1 = conn.prepareStatement(sql1);
			pstmt1.setString(1, data[0].trim());
			pstmt1.setString(2, data[1].trim());
			pstmt1.setString(3, data[2].trim());
			pstmt1.executeUpdate();
		}

	} catch (Exception e) {
		System.out.println(e.getMessage());
	} finally {
		try {
			pstmt1.close();
		} catch (SQLException e) { }

		try {
			br.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	response.sendRedirect("/");
}

csvファイル情報取得部分

Part csv = request.getPart("csv");
BufferedReader br = null;

csv読み込み部分

InputStream is = csv.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line;

while文でデータベースに保存

while ((line = br.readLine()) != null) {
	String[] data = line.split(",");

	// SQL実行
	String sql1 = "INSERT INTO table(aaa,bbb,ccc) VALUES(?,?,?)";
	pstmt1 = conn.prepareStatement(sql1);
	pstmt1.setString(1, data[0].trim());
	pstmt1.setString(2, data[1].trim());
	pstmt1.setString(3, data[2].trim());
	pstmt1.executeUpdate();
}

catchで例外処理

catch (Exception e) {
	System.out.println(e.getMessage());
}

最後にbrを解放して終わり

try {
	br.close();
} catch (Exception e) {
	System.out.println(e.getMessage());
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?