简体   繁体   English

使用Java将多个数据插入Oracle数据库

[英]Insert Multiple data into Oracle db using Java

Following is my code which retrieves Rally data. 以下是我的代码,用于检索Rally数据。

public static void main(String[] args) throws URISyntaxException, IOException {

String host = "examplehost";
String username = "exampleuser";
String password = "password";
String projectRef   = "project";
String workspaceRef = "workspace"; 
String applicationName = "";
int queryLimit = 4000;
Connection conn        = null;
String propertiesFile  = "";
String projectValue = "";
String columnValue = "";
String returnValue = "";

    RallyRestApi restApi = new RallyRestApi(
            new URI(host),
            username,
            password);
    restApi.setApplicationName(applicationName); 
    System.out.println(restApi.getWsapiVersion()); 



    try{
         QueryRequest projectRequest = new QueryRequest("Project");
         projectRequest.setFetch(new Fetch("Name", "TeamMembers"));
         projectRequest.setWorkspace(workspaceRef);
             projectRequest.setProject(projectRef);
             projectRequest.setScopedDown(true);
         projectRequest.setQueryFilter(new QueryFilter("Name", "contains", "DT-"));  
         projectRequest.setLimit(queryLimit);
         QueryResponse projectQueryResponse = restApi.query(projectRequest);
         int count = projectQueryResponse.getResults().size();
         System.out.println(count);

         if(count > 0){
             for (int i=0;i<count;i++){
                 JsonObject projectObject = projectQueryResponse.getResults().get(i).getAsJsonObject();
                 JsonObject obj = projectObject.getAsJsonObject();                   
                 projectValue = JsonUtil.getJsonValue(obj, "_refObjectName");

                 System.out.println("Project: " + projectValue);
                 int numberOfTeamMembers = projectObject.getAsJsonObject("TeamMembers").get("Count").getAsInt();
                 if(numberOfTeamMembers > 0) {
                        QueryRequest teamRequest = new QueryRequest(projectObject.getAsJsonObject("TeamMembers"));
                        JsonArray teammates = restApi.query(teamRequest).getResults();
                        //JsonObject teammates = restApi.query(teamRequest).getResults();
                            if (teammates instanceof JsonArray) {
                                    JsonArray jsonarr = teammates.getAsJsonArray();
                                    //returnValue += squote;
                                    for (int j=0; j<jsonarr.size(); j++) {
                                        if (j>0) 
                                            returnValue += "\n";
                                        JsonObject tobj = jsonarr.get(j).getAsJsonObject();
                                        if (obj.has("Name"))
                                            columnValue = getJsonValue(tobj, "Name");
                                        if (obj.has("_refObjectName"))
                                            columnValue = JsonUtil.getJsonValue(tobj, "_refObjectName");
                                        returnValue += columnValue;
                                      System.out.println(columnValue);   
                                    }
                                    //returnValue += squote;
                                } 
                //columnValue = JsonUtil.getJsonString(teammates);      
                        //for (int j=0;j<numberOfTeamMembers;j++){
                            //JsonObject teamObject = teamQueryResponse.getResults().get(j).getAsJsonObject();


                            //System.out.println(teammates.get(j).getAsJsonObject().get("_refObjectName").getAsString());
                        //}
                    }
             }
         }

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

    } finally{
        restApi.close();
    }
}

I'm trying to insert Project ID and the associated team members into db. 我正在尝试将Project ID和相关的团队成员插入db。 For one Project ID there may be 1- 10 members, The count may differ for each project. 一个项目ID可能有1到10个成员,每个项目的数量可能有所不同。 In DB, there would be only two Column holding Project ID and User name. 在数据库中,将只有两个列,分别包含项目ID和用户名。 Could anyone help me with this request? 有人可以帮我这个要求吗?

Hope it helps. 希望能帮助到你。

If you need more details, please do reply 如果您需要更多详细信息,请回复

Thanks 谢谢

Sreer Sreer

From your comment above, the actual line that is failing is: 从上面的评论中,失败的实际行是:

st.executeUpdate("INSERT INTO CUST_RALLY_TEAM_MEMBER " + "VALUES projectValue,columnValue)");

Here, you are literally passing this string to Oracle: 在这里,您实际上是在将这个字符串传递给Oracle:

INSERT INTO CUST_RALLY_TEAM_MEMBER VALUES projectValue,columnValue)

You need to pass the values of those variables (you're also missing an open parenthesis: 您需要传递这些变量的 (您还缺少一个右括号:

st.executeUpdate("INSERT INTO CUST_RALLY_TEAM_MEMBER VALUES ('" + projectValue + "','" + columnValue + "')");

This assumes that the cust_rally_team_member contains only these two columns, if not (and as a best practice, even if it does), you should be specifying the columns to be inserted into. 这假定cust_rally_team_member仅包含这两列,如果没有(并且最好的话,即使这样做),也应指定要插入的列。 Assuming the column names are also "projectvalue" and "columnvalue", then the statement would be: 假设列名称也为“ projectvalue”和“ columnvalue”,则该语句为:

st.executeUpdate("INSERT INTO CUST_RALLY_TEAM_MEMBER (projectvalue,columnvalue) VALUES ('" + projectValue + "','" + columnValue + "')");

Note, too, that prepared statements are a good approach for a process such as this. 还要注意,对于这样的过程, 准备好的语句是一种很好的方法。 It eliminates the risk of SQL errors if the values contain a single quote, reduces the risk of SQL Injection attacks, and improves performance by compiling the statement only once. 如果值包含单引号,则消除了SQL错误的风险,降低了SQL注入攻击的风险,并通过仅编译一次语句来提高了性能。

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

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