简体   繁体   English

如何从Excel读取SQL查询并传递给JDBC代码

[英]How to read the SQL query from Excel and pass into JDBC code

Can someone suggest/help on the below query. 有人可以建议/帮助以下查询。

I have the below code which retrieve the data from database, the SQL query i am hard coding want to pass in excel and read the SQL query from excel and store the SQL query output in the result column. 我有下面的代码,它们从数据库中检索数据,我是硬编码的SQL查询,希望传入excel并从excel中读取SQL查询,并将SQL查询输出存储在结果列中。

package com.DBUtility;

import java.io.*;  
import java.sql.*;  


public class DataRetrieveMainClass {  
    public static void main(String args[]) throws Exception {

    PrintColumnAndData PrintCl=new PrintColumnAndData();

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@123.32.23.123:8080/orcl", "Test1", "******");

            PreparedStatement ps = con.prepareStatement("select * from MSG where MID='1234'");
            ResultSet rs = ps.executeQuery();

            try {

                PrintCl.printResultColumns(rs);
            } catch (SQLException e) {
                System.err.println(e.getMessage());
            }

            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


** - Excel File:**

SQL QUERY                                    RESULT
______________________________________________________
select * from MSG where MID='1234'

电子表格

public static void main(String args[]){
    writeExcel(getSQLObjects());
}

private static ArrayList<MyObject> getSQLObjects(){
    PrintColumnAndData PrintCl=new PrintColumnAndData();
    ArrayList<MyObject> objList = new ArrayList<MyObject>();
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        try (Connection con = DriverManager.getConnection("jdbc:oracle:thin:@123.32.23.123:8080/orcl", "Test1", "******")){
            PreparedStatement ps = con.prepareStatement("select * from MSG where MID='1234'");
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                MyObject object = new MyObject();
                //set object values for each column using rs.getString() etc.
            objList.add(object);
            }
        }
    }catch(SQLException e) {
        e.printStackTrace();
    }catch(ClassNotFoundException e) {
        e.printStackTrace();
    }catch (Exception e) {
        e.printStackTrace();
    }
    return objList;
}

private static void writeExcel(ArrayList<MyObject> objList) {
    Workbook book = null;
    try {
        try {
            book = new WorkbookFactory.create(new File("myFile"));
            Sheet sheet = book.getSheet("mySheet");
            int startRow = 1;
            for(MyObject obj : objList) {
                Row row = sheet.createRow(startRow);
                row.getCell(0).setCellValue(obj.value);
                row.getCell(1).setCellValue(obj.otherValue);
                //etc.
                startRow++;
            }
            book.write(new FileOutputStream(new File("myFile")));
        }finally {
            book.close();
        }
    }catch(IOException e) {
        e.printStackTrace();
    }catch(Exception e) {
        e.printStackTrace();
    }
}

That should be a huge help for you (might need to be modified slightly to fit your needs, but should give you a 'map' to your solution nonetheless). 这对您应该是一个巨大的帮助(可能需要稍作修改以适应您的需求,但是仍然应该为您提供解决方案的“映射”)。 Your code seems a bit clumsy at the moment and very disorganized. 您的代码目前似乎有些笨拙,并且非常混乱。 You should separate your SQL database call and the excel writing into separate methods (if not classes) to handle these; 您应该将SQL数据库调用和excel写入单独的方法(如果不是类)中来处理这些方法; I personally even create a class for the connection itself. 我个人甚至为连接本身创建一个类。 You should also consider creating a class to represent an object to pass between the two (data transfer object). 您还应该考虑创建一个类来表示要在两者之间传递的对象(数据传输对象)。

Some things that I assumed: you are using Apache POI for excel, your excel file and the sheet for it already exist (you are just populating it), and you have headers in the first row of the sheet (hence startRow = 1). 我假设了一些事情:您正在使用Apache POI for excel,您的excel文件和用于它的工作表已经存在(您正在填充它),并且工作表的第一行中有标题(因此startRow = 1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM