Java imports Excel data into the database in bulk

Source: Internet
Author: User
Tags int size

Java imports the Excel data into the database in bulk, Java imports the Excel data code as follows


1. Public list<choice> Getfromxls (String xlsname) {

2.

3. list<choice> choices = new arraylist<choice> ();

4. Choice Choice=null;

5. Try {

6. Java.io.File file=new java.io.File (xlsname);

7. Workbook book = workbook.getworkbook (file);

8.

9.//Get the first sheet, the default is three

Sheet Sheet = book.getsheet (0);

11.//How many rows and columns of data are there altogether?

. int rows = Sheet.getrows ();

int columns = Sheet.getcolumns ();

. for (int i = 1; I <rows; i++) {

. Boolean hasText = false;

16.//filter out lines with no text content

. for (int j = 0; j < columns; J + +)

if (Sheet.getcell (J, i). getcontents (). Length ()!=0) {

HasText = true;

break;

21.}

if (HasText) {

. try {

24.

Choice = new Choice ();

Choice.settypeid (Integer.parseint (Sheet.getcell (0, I). getcontents ()));

Choice.setcataid (Sheet.getcell (1, i). getcontents ());

Choice.settitle (Sheet.getcell (2, I). getcontents ());

Choice.seta (Sheet.getcell (3, I). getcontents ());

Choice.setb (Sheet.getcell (4, I). getcontents ());

Choice.setc (Sheet.getcell (5, I). getcontents ());

CHOICE.SETD (Sheet.getcell (6, I). getcontents ());

Choice.setanswer (Sheet.getcell (7, I). getcontents ());

Choices.add (choice);

A.} catch (Exception e) {

//Todo:handle exception

Panax Notoginseng e.printstacktrace ();

38.}

39.

40.}

41.}

Book.close ();

.} catch (Exception e) {

E.printstacktrace ();

45.}

Choices return;

47.}

Batchinsert public int (final list<choice> q,string xlsname) {

//TODO auto-generated method stub

Final list<choice> choices = Getfromxls (xlsname);

Wuyi Final int size = Choices.size ();

52.

. int result=0;

54.//Here should get the question question to insert the database

. String sql = "INSERT into choice (title,typeid,cataid,a,b,c,d,answer)"

+ "VALUES (?,?,?,?,?,?,?,?)";

57.

Conn=jdbconn.getconn ();

try{.

Ps=conn.preparestatement (SQL);

. for (int i=0;i<size;i++) {

Choice Choice = Choices.get (i);

Ps.setstring (1, Choice.gettitle ());

Ps.setint (2,choice.gettypeid ());

Ps.setstring (3, Choice.getcataid ());

Ps.setstring (4, Choice.geta ());

Ps.setstring (5, CHOICE.GETB ());

Ps.setstring (6, choice.getc ());

Ps.setstring (7, choice.getd ());

Ps.setstring (8, Choice.getanswer ());

Ps.executeupdate ();

result++;

73.}

A.} catch (SQLException e) {

E.printstacktrace ();

The.} finally {

JDBCONN.CLOSEDB (conn);

78.}

. return result;

80.}

Java imports Excel data into the database in bulk

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.