我写了一个SQL解析器,代码工作正常,并且在解析结束时可以看到正确的输出。现在,我试图在JSP中显示已解析的输出。为此,我在IntelliJ上设置了项目,如下图所示。

java - 如何调用另一个.java文件中存在的类(不是嵌套类)?-LMLPHP

我先前编写的解析器的主要方法位于getTableColumn内,该方法在图像的红色框中标记。
看起来像这样。

public class getTableColumn {
    ..
    ..
    public static void main(String args[]) throws SQLException {
        ReadQueries rq = new ReadQueries("query");
        String output  = rq.getData();
        ....
        ....
        ....
        ....
    }
 }


ReadQueries是我在其中声明所有自定义方法以解析SQL的类。方法getData返回已解析的输出,该输出存在于字符串output中。我正在尝试在jsp中显示output的值。

我将getTableColumn中的main方法重命名为processQuery,以便可以从其他类中调用它。
基于JSP的下拉菜单,我试图将字符串值传递给getTableColumn的processQuery,如下所示。

import com.query.data.ReadQueries;
import com.table.modules.TableModules;
import com.where.WhereData;
import gudusoft.gsqlparser.EDbVendor;
import gudusoft.gsqlparser.IMetaDatabase;
import save.querydata.InsertColumns;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

class sampleMetaDB implements IMetaDatabase {
    String columns[][] = { {"server", "db", "schema", "TBL_BBGPOS_REP", "M_TP_PFOLIO"
            }, { "server", "db", "schema", "A_FXPOSITIONDEL_REP", "M_FXDELTA"},
               {"server", "db", "schema", "A_FXPOSITIONDEL_REP", "M_CLOSING_E" },
               {"server", "db", "schema", "A_FXPOSITIONDEL_REP", "M_FXDELTA_Z"}
    };

    public boolean checkColumn(String server, String database, String schema, String table, String column) {
        boolean bServer, bDatabase, bSchema, bTable, bColumn, bRet = false;
        for (int i = 0; i < columns.length; i++) {
            if ((server == null) || (server.length() == 0)) {
                bServer = true;
            } else {
                bServer = columns[i][0].equalsIgnoreCase(server);
            }
            if (!bServer) continue;
            if ((database == null) || (database.length() == 0)) {
                bDatabase = true;
            } else {
                bDatabase = columns[i][1].equalsIgnoreCase(database);
            }
            if (!bDatabase) continue;
            if ((schema == null) || (schema.length() == 0)) {
                bSchema = true;
            } else {
                bSchema = columns[i][2].equalsIgnoreCase(schema);
            }
            if (!bSchema) continue;
            bTable = columns[i][3].equalsIgnoreCase(table);
            if (!bTable) continue;
            bColumn = columns[i][4].equalsIgnoreCase(column);
            if (!bColumn) continue;
            bRet = true;
            break;
        }
        return bRet;
    }
}

public class getTableColumn {
    private static File[] listFiles(File sqlFiles) {
        List<File> children = new ArrayList<File>();
        if (sqlFiles != null)
            listFiles(sqlFiles, children);
        return children.toArray(new File[0]);
    }

    private static void listFiles(File rootFile, List<File> children) {
        if (rootFile.isFile())
            children.add(rootFile);
        else {
            File[] files = rootFile.listFiles();
            for (int i = 0; i < files.length; i++) {
                listFiles(files[i], children);
            }
        }
    }

    public String processQuery(String category) throws SQLException {

        EDbVendor vendor = EDbVendor.dbvmysql;
        System.out.println("Processing " + vendor.toString() + "...");
        TGetTableColumn getTableColumn = new TGetTableColumn(vendor);
        getTableColumn.showDetail = false;
        getTableColumn.showSummary = true;
        getTableColumn.showTreeStructure = false;
        getTableColumn.showBySQLClause = false;
        getTableColumn.showJoin = false;
        getTableColumn.showColumnLocation = true;
        getTableColumn.linkOrphanColumnToFirstTable = false;
        getTableColumn.showIndex = false;
        getTableColumn.showDatatype = true;
        getTableColumn.listStarColumn = true;
        getTableColumn.setMetaDatabase(new sampleMetaDB());
        String sqlQuery = queryDetails[1];
        getTableColumn.run(sqlQuery, false);

        String issue = category;
        ReadQueries rq = new ReadQueries(issue);
        String outputQuery = rq.getQueries();
        long t = System.currentTimeMillis();
        EDbVendor vendor = EDbVendor.dbvmysql;

        return outputQuery();
    }

    private static void displayInitInformation() {
        System.out.println("Usage: java getTableColumn [/f <path_to_sql_file>] [/d <path_to_directory_includes_sql_files>] [/t <database type>] [/<show option>]");
        System.out.println("/f: specify the sql file path to analyze.");
        System.out.println("/t: option, set the database type. Support oracle, mysql, mssql, db2, netezza, teradata, informix, sybase, postgresql, hive, greenplum and redshift, the default type is oracle");
        System.out.println("/showSummary: default show option, display the summary information.");
        System.out.println("/showDetail: show option, display the detail information.");
        System.out.println("/showTreeStructure: show option, display the information as a tree structure.");
        System.out.println("/showBySQLClause: show option, display the information group by sql clause type.");
        System.out.println("/showJoin: show option, display the join table and column.");
    }
}


我看到的问题是我无法在servlet类中创建getTableColumn的对象。

package com.servlets;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/getdata.do")
public class DataServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String issue        = request.getParameter("reportSelection");
        getTableColumn gt   = new getTableColumn();
        if ("Latency".equals(issue)) {
            getTableColumn gt   = new getTableColumn();
            System.out.println("Latency");
        } else if ("DataQuality".equals(issue)) {
            System.out.println("Data quality");
        } else if ("Outage".equals(issue)) {

        }
    }
}


这行:getTableColumn gt = new getTableColumn();Cannot resolve symbol 'getTableColumn'。我试图编写该类的导入语句,但它也不起作用。
我不知道如何解决此问题。谁能让我知道该如何解决?

最佳答案

我认为您遇到了问题,因为您的getTableColumn类不在软件包中(或者它在默认软件包中),并且不在you can't import a class from the default package中。所以:


将您的getTableColumn类移动到一个包中,例如tablecolumn
然后在DataServlet中添加:import tablecolumn.getTableColumn和其他导入


并请在课程中使用大写字母:getTableColumn zh tableRelationAnalyze不是有效的名称

09-27 18:30