首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

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

2012-12-14 
使用JAVA程序将EXCEL数据导入MySQL数据库问题从网上找了一段读取EXCEL的代码,稍作修改,发现能实现功能.程

使用JAVA程序将EXCEL数据导入MySQL数据库问题
从网上找了一段读取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;
 }
}
写的有些笨哈。。。见笑了见笑了。。。
求各位给些解答,提前谢过!

[解决办法]
你把错误的SQL语句贴全了比那一堆代码有用
[解决办法]
楼上没明白我的意思啊,同样的SQL语句,程序中不能执行,但是在命令行中能执行啊
[解决办法]
错误已解决

热点排行