使用JAVA程序将EXCEL数据导入MySQL数据库问题

mhl2012 2012-11-15 05:27:03
从网上找了一段读取EXCEL的代码,稍作修改,发现能实现功能.程序可以解析EXCEL文档,并插入数据库。
主要问题:
在SQL语句相同的情况下,有些语句插入数据库失败。但是在命令行下插入这些语句却能成功。
截图:


我的程序代码:
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import javax.swing.JOptionPane;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
/**
* 导入Excel表
* @author Administrator
* 输入数据:表路径
*/
public class TestExcel2 {

static String UserName = "job_peopleinfo"; // 生成的数据库名字

private static Connection conn = null;

private static String drive = "com.mysql.jdbc.Driver";

private static String DBurl ="jdbc:mysql://localhost:3306/xms";

private static String name = "root"; //数据库账号

private static String pwd = "root"; //数据库,密码

private static Statement st=null;

public static void main(String[] args) {
readExcel("E:/T/3.xls");
UserName = "UserName";
}

public static void readExcel(String url) {
File filename = new File(url);
Workbook wb = null;
String sql = "insert into " + UserName + "(";
String parameter = "";
String value = "";
String insert = "";
try {
wb = Workbook.getWorkbook(filename);
Sheet s = wb.getSheet(0);// 第1个sheet
Cell c = null;
int row = s.getRows();// 总行数
int col = s.getColumns();// 总列数
for (int i = 0; i < col; i++) {
if (i == col-1) {
parameter += s.getCell(i, 0).getContents();
} else {
parameter += s.getCell(i, 0).getContents() + ",";
}
}
System.out.println("--------------------------------------");
for (int i = 1; i < row; i++) {
value="";
for (int j = 0; j < col-4; j++) {
c = s.getCell(j, i);
if (j < col - 1) {
value += "'" + c.getContents() + "',";

} else {
value += "'" + c.getContents() + "'";
}
}
value += (Long)Long.parseLong(s.getCell(col-4,i).getContents()) + "," + (Long)Long.parseLong(s.getCell(col-3,i).getContents()) + "," + (Long)Long.parseLong(s.getCell(col-2, i).getContents()) + "," + (Long)Long.parseLong(s.getCell(col-1,i).getContents());
insert = sql + parameter + ") values(" + value + ");";
System.out.println("添加语句-------" + insert);
int a = insert(insert);
if (a > 0) {
System.out.println("成功添加" + i + "次");
} else {
System.out.println("第" + i + "次失败了");
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
}

public static Connection getConn() {
try {
Class.forName(drive);
conn = DriverManager.getConnection(DBurl, name, pwd);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "数据库连接错误");
}
return conn;
}

public static void Close() {
if (conn != null)
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static int insert(String sql){
int result=0;
try {
st=getConn().createStatement();
result=st.executeUpdate(sql);
} catch (Exception e) {
System.out.println("添加失败");
}finally{
Close();
}
return result;
}

public static int Create(String sql) throws Exception{
int result=0;
try {
st=getConn().createStatement();
result=st.executeUpdate(sql);
}finally{
Close();
}
return result;
}
}
写的有些笨哈。。。见笑了见笑了。。。
求各位给些解答,提前谢过!
...全文
470 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mhl2012 2012-11-16
  • 打赏
  • 举报
回复
错误已解决
mhl2012 2012-11-16
  • 打赏
  • 举报
回复
楼上没明白我的意思啊,同样的SQL语句,程序中不能执行,但是在命令行中能执行啊
forgetsam 2012-11-15
  • 打赏
  • 举报
回复
你把错误的SQL语句贴全了比那一堆代码有用

50,533

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧