在Excel中将Excel数据写入数据库 [英] Writing Excel data to database in Java

查看:138
本文介绍了在Excel中将Excel数据写入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来将excel文件数据写入数据库的方法。

  public static void executeSQLUpdate ,List< Object> arguments){
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection(); //一个返回java.sql.Connection到数据库的方法
System.out.println(\\\
01)conection:+ con);
pstmt = con.prepareStatement(sql);
System.out.println(\\\
02)pstn:+ pstmt);
System.out.println(\\\
03)arguments size:+ arguments.size());
if(arguments!= null){
int i = 1;
System.out.println(\\\
04)if:+ arguments);
for(Object o:arguments){
System.out.println(\\\
05)executeSQLUpdate);
System.out.println(\\\
06)object。+ o);
System.out.println(\\\
07)...................:+ i + o);
pstmt.setObject(i,o);
System.out.println(\\\
08)____________________+ i + o);

}
}
System.out.print(\\\
09)errorchk ...........:);
//执行insert,update,delete语句的方法...
pstmt.executeUpdate();
System.out.print(\\\
10)+++++++++++++++++:);
} catch(SQLException e){
System.out.println(\\\
11)*************:+ e);
//处理错误...
} finally {
//关闭资源(总是在finally块,不是在尝试!)
try {
if(pstmt!= null){
pstmt.close();
}
if(con!= null){
con.close();
}
} catch(SQLException e){
}
}
}

最多没有07所有的系统都工作。但是之后任何系统都不工作。这是什么原因?这个是否有错误?



这是我的输出:

  run:
AAA BBB CCC
DDD EEE FFF
GGG HHH III
JJJ KKK LLL
MMM NNN OOO
PPP QQQ RRR

01)conection:com.mysql.jdbc.JDBC4Connection@6e70c7



02)pstn:com.mysql.jdbc.JDBC4PreparedStatement@29428e:INSERT INTO files_1 VALUES(** NOT SPECIFIED 未指定未指定**)



03)arguments size:6



04)if:[[AAA,BBB,CCC] DDD,EEE,FFF],[GGG,HHH,III],[JJJ,KKK,LLL],[MMM,NNN,OOO],[PPP,QQQ,RRR]]



05)executeSQLUpdate:



06)object:[AAA,BBB,CCC] )...................:1 [AAA,BBB,CCC]



08) _ __ _ __ _ ___ :1 [AAA,BBB,CCC]



05)executeSQLUpdate:



06)object:[DDD,EEE,FFF] )...................:1 [DDD,EEE,FFF]



08) _ __ _ __ _ ___ :1 [DDD,EEE,FFF]



05)executeSQLUpdate:



06)object:[GGG,HHH,III] )...................:1 [GGG,HHH,III]



08) _ __ _ __ _ ___ :1 [GGG,HHH,III]



05)executeSQLUpdate:



06)object:[JJJ,KKK,LLL]



07 )...................:1 [JJJ,KKK,LLL]



08) _ __ _ __ _ ___ :1 [JJJ,KKK,LLL]



05)executeSQLUpdate:



06)object:[MMM,NNN,OOO] )...................:1 [MMM,NNN,OOO]



08) _ __ _ __ _ ___ :1 [MMM,NNN,OOO]



05)executeSQLUpdate:



06)object:[PPP,QQQ,RRR]



)...................:1 [PPP,QQQ,RRR]



08) _ __ _ __ _ ___ :1 [PPP,QQQ,RRR]



09)errorchk ...........:
11) * ** * * :没有为参数2指定值

  java.sql.SQLException:没有为参数2指定值
at com .mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException (SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2560)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2536)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2383)
at com.mysql。 jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2327)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2312)
at com.project.bulk.ReadExcelFile.executeSQLUpdate(ReadExcelFile。 java:112)
at com.project.bulk.ReadExcelFile.MethodToData(ReadExcelFile.java:138)
at com.project.bulk.ReadExcelFile.main(ReadExcelFile.java:39)



BUILD SUCCESSFUL(总时间:3秒)

解决方案

一个错误肯定是你增加 i 两次!!!

  System.out.println(\\\
07)...................+ i ++ + o); // one
pstmt.setObject(i ++,o); // two

这意味着你不设置偶数索引,只是奇数:1 ,3,5 ...



这应该可以更正此错误:

  System.out.println(\\\
07)...................+ i + o);
pstmt.setObject(i ++,o); //只有一次,后评价!

EDIT
* 错误 *

 } catch(SQLException e){
System.out.println(\ n11)*************:+ e); // WTF?
//处理错误...
}

借口



请为我们和你的(未来的)同事精神健康的缘故,不要再担心重新开始!



打印异常必须通过以下两种方式之一进行:




  • logger.error(message,e);

  • e.printStackTrace();


b $ b

由于这些保留了堆栈跟踪,因此可以正确调试代码



,但应




  • System.out.print(e)
  • >
  • System.out.print(e.getMessage)

  • System.out.print(message+ e.getMessage)

  • logger.error(e.getMessage)



所以正确的应该是:

 } catch(SQLException e){
System.out.println(\\\
11)************* :+ e.getMessage());
e.printStackTrace();
// TODO:处理错误...
}

顺便说一下:使用正确的日志,如log4j是很值得的时间!它会消耗更多的时间来清理所有的System.out。*,而不是设置一个正确的loglevel ...



EDIT2 p>

对于SQL错误:

  String sql =INSERT INTO files_1 VALUES(?); 

这个SQL行告诉DBMS它将有一个参数来处理。该表有3列,因此您需要指定3个值。常数或参数(使用)。所以你应该有:

  String sql =INSERT INTO files_1 VALUES(?,?,? 


This is my method that I use to write excel file data to a database.

public static void executeSQLUpdate(String sql, List<Object> arguments) {
    Connection con = null;
    PreparedStatement pstmt = null;
    try {
        con = getConnection(); //a method that returns a java.sql.Connection to your database
        System.out.println("\n01)conection :"+con);
        pstmt =  con.prepareStatement(sql);
        System.out.println("\n02)pstn :"+pstmt);
        System.out.println( "\n03)arguments size :"+arguments.size());
        if (arguments != null) {
            int i = 1;
            System.out.println( "\n04)if :"+arguments);
            for(Object o : arguments) {
                 System.out.println( "\n05)executeSQLUpdate");
                 System.out.println( "\n06)object."+o);                 
                 System.out.println("\n07)................... :"+i + o);
                 pstmt.setObject(i, o);
                 System.out.println("\n08)____________________"+i+o);

            }
        }
        System.out.print("\n09)errorchk........... :");
        //method to execute insert, update, delete statements...
        pstmt.executeUpdate();
        System.out.print("\n10)+++++++++++++++++ :");
    } catch(SQLException e) {
        System.out.println("\n11)************* :"+e);
        //handle the error...
    } finally {
        //closing the resources (always in finally block, not in the try!)
        try {
            if (pstmt != null) {
                pstmt.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException e) {
        }
    }
}

Up to no 07 all the system out are working. But after that any system out are not working. What is the reason for that? Is there any error in this one?

This is my out put:

run:
AAA     BBB     CCC     
DDD     EEE     FFF     
GGG     HHH     III     
JJJ     KKK     LLL     
MMM     NNN     OOO     
PPP     QQQ     RRR 

01)conection :com.mysql.jdbc.JDBC4Connection@6e70c7

02)pstn :com.mysql.jdbc.JDBC4PreparedStatement@29428e: INSERT INTO files_1 VALUES(** NOT SPECIFIED , NOT SPECIFIED , NOT SPECIFIED **)

03)arguments size :6

04)if :[[AAA, BBB, CCC], [DDD, EEE, FFF], [GGG, HHH, III], [JJJ, KKK, LLL], [MMM, NNN, OOO], [PPP, QQQ, RRR]]

05)executeSQLUpdate :

06)object :[AAA, BBB, CCC]

07)................... :1[AAA, BBB, CCC]

08)__________ :1[AAA, BBB, CCC]

05)executeSQLUpdate :

06)object :[DDD, EEE, FFF]

07)................... :1[DDD, EEE, FFF]

08)__________ :1[DDD, EEE, FFF]

05)executeSQLUpdate :

06)object :[GGG, HHH, III]

07)................... :1[GGG, HHH, III]

08)__________ :1[GGG, HHH, III]

05)executeSQLUpdate :

06)object :[JJJ, KKK, LLL]

07)................... :1[JJJ, KKK, LLL]

08)__________ :1[JJJ, KKK, LLL]

05)executeSQLUpdate :

06)object :[MMM, NNN, OOO]

07)................... :1[MMM, NNN, OOO]

08)__________ :1[MMM, NNN, OOO]

05)executeSQLUpdate :

06)object :[PPP, QQQ, RRR]

07)................... :1[PPP, QQQ, RRR]

08)__________ :1[PPP, QQQ, RRR]

09)errorchk........... : 11)***** :No value specified for parameter 2

java.sql.SQLException: No value specified for parameter 2
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
    at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2560)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2536)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2383)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2327)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2312)
    at com.project.bulk.ReadExcelFile.executeSQLUpdate(ReadExcelFile.java:112)
    at com.project.bulk.ReadExcelFile.MethodToData(ReadExcelFile.java:138)
    at com.project.bulk.ReadExcelFile.main(ReadExcelFile.java:39)

BUILD SUCCESSFUL (total time: 3 seconds)

解决方案

One error for sure is that you increment i two times!!!

System.out.println("\n07)..................."+i++ + o); // one
pstmt.setObject(i++, o); // two

This means that you don't set the even indices, just the odd ones: 1, 3, 5...

This should correct this error:

System.out.println("\n07)..................."+i + o); 
pstmt.setObject(i++, o); // only once, and after the evaluation!

EDIT *Second, but also big mistake*

} catch(SQLException e) {
    System.out.println("\n11)************* :"+e); //WTF?
    //handle the error...    
}

excuse me for shouting, this has to happen now!

Please, for our and your (future) colleagues' mental health's sake, DO NOT EVER DO THIS AGAIN!

Printing exceptions must happen in one of two ways:

  • logger.error("message", e);
  • e.printStackTrace();

As these reserve the stack trace, and thus enable proper debugging of the code

but should never, ever, ever, never! happen in any of these ways:

  • System.out.print(e)
  • System.out.print(e.getMessage)
  • System.out.print("message " + e.getMessage)
  • logger.error(e.getMessage)

So correctly this should be:

} catch(SQLException e) {
    System.out.println("\n11)************* :"+e.getMessage()); 
    e.printStackTrace();
    //TODO: handle the error...    
}

By the way: using proper logging like log4j is well worth the time! It consumes much more time to clean up all the System.out.*, than to set a proper loglevel...

EDIT2

As for the SQL error:

String sql = "INSERT INTO files_1 VALUES(?)"; 

This SQL line tells the DBMS that it will have one parameter to deal with. The table has 3 columns, so you need to specify 3 values. Either constants, or parameters (by using ?). So you should have:

String sql = "INSERT INTO files_1 VALUES(?,?,?)"; 

这篇关于在Excel中将Excel数据写入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆