package excel;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class Exceltest {
public static void main(String arg[]) throws BiffException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
String url="jdbc:mysql://localhost:3306/excel";
String user="root";
String pwd="root";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url,user, pwd);
if(!conn.isClosed()){
System.out.println("Succeeded connecting to db!");
}
Statement statement = conn.createStatement();
File file = new File("src"+File.separator+"excel"+File.separator+"test.xls");
Workbook wb = Workbook.getWorkbook(file);
Sheet sheet1 = wb.getSheet(0);
int numCols = sheet1.getColumns();
int numRows = sheet1.getRows();
for(int i=0;i<numRows;i++){
Cell c1 = sheet1.getCell(0,i);
String c11= c1.getContents();
Cell c2 = sheet1.getCell(1,i);
String c12= c2.getContents();
Cell c3 = sheet1.getCell(2,i);
String c13= c3.getContents();
Cell c4 = sheet1.getCell(3,i);
String c14= c4.getContents();
String sql="insert into excel(date,hlb,qhjhj_q,qhspj_q) values('"+c11+"','"+c12+"','"+c13+"','"+c14+"')";
statement.executeUpdate(sql);
}
statement.close();
conn.close();
}
}
在此期间,遇到一个问题 就是删除表中的所有记录,老是敲错sql命令。正确如下
delete from tablename --写入日志
Truncate tablename --不写入日志