用java中的try--catch捕捉sql相关错误(记录错误)

用java中的try--catch捕捉sql相关错误(记录错误),java,jdbc,error-handling,Java,Jdbc,Error Handling,我使用log4j在java桌面应用程序中记录消息(在Eclipse上创建这个应用程序) 工作的一部分是使用JDBC将csv(逗号分隔值)数据批量插入sql server数据库 我希望捕获批量插入期间的任何错误,并记录发生错误的特定记录的详细信息,然后继续批量插入工作 下面给出了执行批量插入的相关代码,我如何编写异常处理代码以实现我的愿望?还有一个问题-目前堆栈跟踪打印到控制台上,我想将其记录到日志文件中(使用Log4j)。。。我该怎么做 public void insertdata(String

我使用log4j在java桌面应用程序中记录消息(在Eclipse上创建这个应用程序)

工作的一部分是使用JDBC将csv(逗号分隔值)数据批量插入sql server数据库

我希望捕获批量插入期间的任何错误,并记录发生错误的特定记录的详细信息,然后继续批量插入工作

下面给出了执行批量插入的相关代码,我如何编写异常处理代码以实现我的愿望?还有一个问题-目前堆栈跟踪打印到控制台上,我想将其记录到日志文件中(使用Log4j)。。。我该怎么做

public void insertdata(String filename)
{
    String path = System.getProperty("user.dir");
    String createString = "BULK INSERT Assignors FROM  '" + path + "\\" +filename+ ".txt' WITH (FIELDTERMINATOR = ',')";   
    System.out.println(" Just before try block...");

    try  
       { 
            // Load the SQLServerDriver class, build the 
            // connection string, and get a connection 
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
            String connectionUrl = "jdbc:sqlserver://ARVIND-PC\\SQLEXPRESS;" + 
                                    "database=test01;" + 
                                    "integratedSecurity=true;"; 
            Connection con = DriverManager.getConnection(connectionUrl); 
            System.out.println("Connected."); 

            // Create and execute an SQL statement that returns some data.  
            String SQL = "BULK INSERT dbo.Assignor FROM  '" + path + "\\" +filename+ ".txt' WITH (FIELDTERMINATOR = ',')";

            Statement stmt = con.createStatement();  
            //ResultSet rs = stmt.executeQuery(SQL);
            Integer no_exec;
            no_exec=stmt.executeUpdate(SQL);
            // Iterate through the data in the result set and display it.  
            //while (rs.next())  
            //{  
            // //System.out.println(rs.getString(1) + " " + rs.getString(2));
            //  System.out.println(" Going through data");
            //}

            System.out.println("Number of rows updated by the query=" + no_exec);
       }  
       catch(Exception e)  
       { 
            System.out.println(e.getMessage()); 
            e.printStackTrace(); 
            System.exit(0);  
       } 
}
你读了吗?您需要一个日志实例,其余的就是蛋糕