Android is stored in sqlite3 database via JXL read Excel _android

Source: Internet
Author: User
Tags sqlite first row

Copy Code code as follows:

Package com.demo.day20140228;

Import Java.io.File;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import java.sql.SQLException;
Import java.sql.Statement;

Import JXL. Cell;
Import JXL. Sheet;
Import JXL. workbook;

public class Dictexceldemo {
public static void Main (string[] args) {
Dictexceldemo ded = new Dictexceldemo ();
Connection conn = Ded.getconnection ();
DED.READEXCEL_ (conn);
}

Private Connection getconnection () {
Connection conn = null;
try {
Class.forName ("Org.sqlite.JDBC");
conn = Drivermanager.getconnection ("jdbc:sqlite:database.db");
Statement stat = conn.createstatement ();
Stat.executeupdate ("CREATE table if not exists dictionary (enword varchar), Cnword varchar (200));" /create a table, two columns

catch (ClassNotFoundException e) {
E.printstacktrace ();
catch (SQLException e) {
E.printstacktrace ();
}
Return conn;
}

private void Readexcel_ (Connection conn) {
try {
Workbook book = Workbook.getworkbook (New File ("English.xls"));
PreparedStatement prep = conn.preparestatement ("INSERT into dictionary (Enword,cnword) values (?,?);");

for (int a = 0; a < a++) {
Get first Worksheet Object
Sheet Sheet = Book.getsheet (a);
Get the cell in the first row of the first column
Get the cell in the first row of the first column
int columnum = Sheet.getcolumns ()//Get the number of columns
int rownum = Sheet.getrows ()//Get number of rows
for (int i = 1; i < rownum i++)//loop for reading and writing
{//Line
String key = "";
String value = "";
for (int j = 0; J < Columnum; J + +) {//Column
Cell cell1 = Sheet.getcell (j, I);
String result = Cell1.getcontents ();
if (j = = 0) {
key + = result;
} else {
Value + = result;
}
}
System.out.println (key+ "= =" +value);
Prep.setstring (1, key);
Prep.setstring (2, value);
Prep.addbatch ();
}
}
Conn.setautocommit (FALSE);
Prep.executebatch ();
Conn.setautocommit (TRUE);
Conn.close ();
Book.close ();
catch (Exception e) {
System.out.println (e);
}
}
}

Related Article

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.