String leadsql="BULK INSERT hnunicom..test1 FROM ";
        leadsql += "'"+fujianWenjian+"' ";
        leadsql += " with(DATAFILETYPE='char',FIELDTERMINATOR='|',ROWTERMINATOR='\n') ";在本机导入是没有问题的。但是在客户端导入的时候,就不可以了。
请教解决办法!
难道非要按行读取文本文件,一行一行往数据库里面写?请高手执教,谢谢

解决方案 »

  1.   

    客户端我没搞过``不过本地的 我用blob试过```可以写入的```
      

  2.   

    我写过往mysql里写excel,基本的思路是用一个list跟map,map遍历行,效验通过后写入list,然后将list对象写入数据库具体代码如下:
    package cn.com.hitb;import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    import java.util.regex.Pattern;import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;public class excel001 { /**
     * 关于配置文件读取的变量声明区
     */
    public final static String file = "e:\\test.properties"; static String driver; static String url; static String user; static String pwd; static String excel; static String sql; /**
     * 数据库连接时需用的变量定义
     * 
     */
    static Connection conn = null; static Statement stmt = null; /**
     * 程序入口
     */ public static void main(String[] args) {
    try { getms(); readXls(excel);
    } catch (Exception e) {
    e.printStackTrace();
    }
    } /**
     * 读取配置文件信息
     * 
     */
    static void getms() {
    try {
    Properties p = new Properties();
    FileInputStream fs = new FileInputStream(file);
    p.load(fs);
    driver = p.getProperty("db.driver");// 读取驱动信息
    url = p.getProperty("db.url");// 读取url信息
    user = p.getProperty("db.user");// 读取用户名
    pwd = p.getProperty("db.pwd");// 读取密码
    excel = p.getProperty("file.excel");// 读取文件路径
    sql = p.getProperty("db.sql");// 读取sql语句 } catch (Exception e) {
    System.out.println("读取配置文件出错~!" + e.getMessage()); }
    } /**
     * 读取excel
     * 
     * @param readFileName
     * @throws BiffException
     * @throws IOException
     */
    public static void readXls(String readFileName) throws BiffException,
    IOException { Workbook book = Workbook.getWorkbook(new File(readFileName));
    Sheet rs = book.getSheet(0);
    int rsRows = rs.getRows();
    System.out.println("excel表有" + rsRows + "行");// 获取一个表有几行
    int rsColumns = rs.getColumns();// 获取一个表的总列数(分析与数据库中列数是否一致,不一致则不予更新)
    System.out.println("此表有" + rsColumns + "列");
    List rows = new ArrayList();
    for (int i = 0; i < rsRows; i++) {
    Map map = new HashMap();
    map.clear();
    for (int j = 0; j < rsColumns; j++) { map.put(new Integer(j), rs.getCell(j, i).getContents());
    // System.out.println(map.toString()); }
    System.out.println(map);
    rows.add(map); System.out.println(rows.get(i));
    }
    String sqled = sqlms(rows);
    try {
    getConntion();// 初始化数据库
    System.out.println(sqled);
    stmt.execute(sqled);// 执行加工过的sql语句
    } catch (Exception e) {
    System.out.println("数据库操作出错,数据未更新成功" + e.getMessage());
    try {
    conn.rollback();
    } catch (Exception ex) {
    System.out.println("数据库操作出错" + ex.getMessage()); } } } /**
     * 对sql语句进行加工
     */
    static String sqlms(List list) { Pattern pattern = Pattern.compile("[^0-9]+");
    Pattern pattern1 = Pattern.compile("[^A-Z]+");
    String[] s = pattern.split(sql);
    String[] st = pattern1.split(sql);
    String sqled = null;// 处理后的sql语句
    if (s.length != 0 && st.length != 0) {// 对精确位置的操作 for (int i = 1; i < s.length; i++) {
    System.out.println(s[i]);
    String s1 = s[i];
    System.out.println(s1);
    String st1 = st[i];
    System.out.println(st1);
    int m = Integer.parseInt(s1);
    int n = wordtonum(st1); // 计算列号
    Map map = (Map) list.remove(m - 1);
    String sqled1 = map.get(n - 1).toString();// 取出替换后的值
    System.out.println(map.get(n - 1));
    sqled = sql.replaceAll(s[i] + st[i], sqled1);
    } // 对sql语句中对应值进行替换 }
    if (s.length == 0) {// 对整列进行操作 for (int i = 1; i < st.length; i++) {
    String st2 = st[i];
    int n1 = wordtonum(st2); for (int j = 0; j < list.size(); j++) {
    Map map1 = (Map) list.remove(j - 1);
    String sqled2 = map1.get(n1 - 1).toString();
    // 对sql语句中对应值进行替换
    sqled = sql.replaceAll(st[i], sqled2);
    } } }
    if (st.length == 0) {// 对整行进行操作
    String sqled4 = "";
    for (int i = 1; i < s.length; i++) {
    String s2 = s[i];
    int m1 = Integer.parseInt(s2);
    System.out.println(m1);
    Map m = (Map) list.get(0);
    System.out.println(list.size());
    System.out.println(m.size());
    Map map2 = (Map) list.remove(m1 - 1);
    for (int j = 1; j < m.size(); j++) {
    // System.out.println(m1); String sqled3 = map2.get(j).toString();
    if (j == m.size() - 1) {
    sqled4 += "'" + sqled3 + "'";
    } else {
    sqled4 += "'" + sqled3 + "'" + ",";
    } }
    System.out.println(sqled4);
    // 对sql语句中对应值进行替换
    sqled = sql.replaceAll(s[i], sqled4); }
    } return sqled;
    } /**
     * 字母转为数字的计算方法
     */
    static int cf = 26; static int cf1 = 26; static int wordtonum(String fColName) { int retValue = 0; int level = 0; for (int i = fColName.length() - 1; i >= 0; i--) { char temp = fColName.charAt(i); int tempValue = ((int) temp) - 64; tempValue = ((26 * level) == 0 ? 1 : cf(level)) * tempValue; retValue += tempValue; level++; }
    return retValue; } /**
     * 次方运算
     * 
     * @param lth
     * @return
     */ static int cf(int lth) {
    for (int i = 1; i <= lth; i++) {
    cf = cf * cf1;
    }
    cf = cf / cf1;
    // System.out.println(cf);
    return cf;
    } /**
     * 初始化数据库
     * 
     */
    static void getConntion() { try {
    // conn.setAutoCommit(false);
    Class.forName(driver);
    conn = DriverManager.getConnection(url, user, pwd);
    stmt = conn.createStatement();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } static boolean close() {// 关闭数据库连接,释放资源
    try {
    // conn.commit(); if (stmt != null)
    stmt.close();
    if (conn != null)
    conn.close(); return true;
    } catch (SQLException ex) {
    ex.printStackTrace();
    return false; }
    }
    }
    后来因为没用这个模块所以就没再做调整,自己看看吧,希望对你有帮助