在JDBC类中运行两个方法以执行多个查询

时间:2019-11-18 15:49:57

标签: java jdbc

在Jdbc类中,我首先需要运行两个方法,一个方法需要成功执行并将所有数据插入数据库,然后第二个方法应开始执行并将数据插入数据库。

我尝试了此处显示的代码,但是在完成第一种方法之前,代码将执行第二种方法:

public class dataTransfer extends DataAccess {

    public static void main(String[] args) throws DataAccessException {
        // TODO Auto-generated method stub
        dailyOut(); // First Method to be done without going to next method
        dailyIn(); // After writing all files in first mehod this method should write 

    }

    public static  void  dailyOut()
    {
        Connection connection = null;
        ResultSet rs = null;
        String selectSQL = " SELECT * FROM all_reports WHERE report_id IN (SELECT report_id FROM daily_tmp_out )";
        DataAccess bao = new DataAccess();
        try {
            connection = bao.getConnection();
            PreparedStatement pstmt = connection.prepareStatement(selectSQL);
            rs = pstmt.executeQuery();
            ByteArrayOutputStream ous = null;
            InputStream ios = null;
            while (rs.next()) {
                String filename = rs.getString("file_name");
                String path = ("C:\\TEST\\" + filename );
                File file = new File(path);
                FileOutputStream output = new FileOutputStream(file);
                Blob blob = rs.getBlob("file_data");
                byte[] bytes1 = blob.getBytes(1l, (int) blob.length());
                for (int i = 0; i < bytes1.length; i++) {
                    output.write(bytes1);

                }
                rs = pstmt.executeQuery("UPDATE all_reports set job_run= 'Y' where file_name =filename");
            }
        } 
        catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (rs != null) {
                       rs.close();
                }
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public static  void  dailyIn()
    {
        Connection connection = null;
        ResultSet rs = null;

    String selectSQL = " SELECT * FROM all_reports WHERE report_id IN (SELECT report_id FROM daily_tmp_in)";
        DataAccess bao = new DataAccess();

        try {
            connection = bao.getConnection();
            PreparedStatement pstmt = connection.prepareStatement(selectSQL);
            rs = pstmt.executeQuery();
            ByteArrayOutputStream ous = null;
            InputStream ios = null;
            while (rs.next()) {
                String filename = rs.getString("file_name");
                String path = ("C:\\TEST\\In\\" + filename );
                File file = new File(path);
                FileOutputStream output = new FileOutputStream(file);
                Blob blob = rs.getBlob("file_data");
                byte[] bytes1 = blob.getBytes(1l, (int) blob.length());
                for (int i = 0; i < bytes1.length; i++) {
                    output.write(bytes1);

                }
                rs = pstmt.executeQuery("UPDATE all_reports set job_run= 'Y' where file_name =filename");

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }
    }

}

我尝试的代码将转到第二种方法,而没有在第一种方法中完成所有插入操作,我想我错过了一些阻止执行第二种方法的功能。由于我是jdbc的新手,请给我建议和帮助以完成我的项目

0 个答案:

没有答案