mysql 테이블 열 이름 num1, num2, num3, num4, num5, num6 테이블 이름 Excel
업로드 방법
패키지 com.web.connection; import java.io.fileInputStream; import java.io.filenotfoundException; import java.io.ioexception; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import org.apache.poi.hssfic org.apache.poi.hssf.usermodel.hssfrow; import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apchache.poi.xsssf.usermodel org.apache.poi.xssf.usermodel.xssfrow; import org.apache.poi.xssf.usermodel.xssfsheet; import org.apache.poi.xssf.usermodel.xssfworkbook; public class testexcel {// classation log의 출력 정보 기록 = logfactory.getLog (testexcel.class); // xsfworkbook .xlx를 사용하여 Excel 문서의 경로를 가져옵니다. xssfworkbook (new FileInputStream (filepath)); // Excel 문서에서, 첫 번째 워크 시트의 기본 인덱스는 0 // ITS 문입니다. hssfsheet sheet = workbook.getsheetat (0); xssfsheet sheet = wookbook.getsheet ( "sheet1"); // excel int rows in Excel Pile int Rows int Rows int Rows Get in Excel 시트. getPhysicalNumberOfrows (); // 행을 트랜스 웨이프 (int i = 0; i <rows; i ++) {// 왼쪽 상단 셀을 읽습니다 xssfrow row = sheet.getrow (i); // 행이 비어 있지 않으면 (row! = null) {// excel 셀에서 모든 열을 가져옵니다. ""; int j = 0; j <셀; += cell.getnumericcellValue () +","; break; case hssfcell.cell_type_string : value += cell.getStringCellValue () +","; break; default : value += "0"; break;}}} // mysql database string에 데이터를 삽입한다. testentity (); entity.setnum1 (val [0]); entity.setnum2 (val [1]); entity.setnum3 (val [2]); entity.setnum4 (val [3]); entity.setnum5 (val [4]); entity.setnum6 (val [5]); testmethod method = new testmethod (); a = method.add (entity); if (a> 0) {system.out.println ( "삽입 성공");} else {system.out.println ( "삽입 실패");}}}}} catch (filenotfoundExcept e) {e.printstacktrace ();} {e.princection e)}};여기서 Testentity는 Excel 테이블에서 쿼리 된 데이터를 저장하는 엔티티 클래스입니다.
package com.web.connection; public class testentity {private string num1; private string num2; private String num3; private String num3; private String num4; private String num5; public testentity () {} public string getNum1 () {return num1;} public void setnum1 (String Num1) {this.num1 = num1;) setnum2 (string num2) {this.num2 = num2;} public String getNum3 () {return num3;} public void setnum3 (string num3) {this.num3 = num3;} public string getNum4 () {return num4;} public string setnum4 (string4) {num4 =} num5;} public void setnum5 (String num5) {this.num5 = num5;} public String getNum6 () {return num6;} public void setnum6 (String Num6) {this.num6 = num6;}}TestMethod는 MySQL 테이블에 데이터를 삽입하는 SQL 문입니다.
패키지 com.web.connection; import java.sql.connection; import java.sql.preparedStatement; import java.sql.sqlexception; public class testmethod {public int add (tesentity te) {Connection con = dbConnection.getConnection (); int count = 0; int count = 0; int count; Excel (num1, num2, num3, num4, num5, num6) 값 (?,?,?,?,?); te.getnum3 ()); pstmt.setString (4, te.getnum4 ()); pstmt.setString (5, te.getnum5 ()); pstmt.setString (6, te.getnum6 ()); count = pstmt.executeUpdate (); count = intogixection (new) {new whow retaMecutection (); }*/} catch (sqlexception e) {// todo auto-auto-auto-recenated catch blocke.printstacktrace ();} 마침내 {try {pstmt.close ();} catch (sqlexception e) {// todo auto-auto-auto-genated catch.printstacktrace ();} dbconnection.요약
위의 내용은 Excel 컨텐츠를 Java의 MySQL 인스턴스 코드에 업로드하는 것에 대한이 기사의 전체 내용입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!