Java把excel中数据读入到数据库中

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 --不写入日志

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值