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);
}
}
}