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

时间:2014-04-04 11:41:41

标签: java json oracle rally

以下是检索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();
    }
}

我正在尝试将项目ID和关联的团队成员插入到db中。对于一个项目ID,可能有1-10个成员,每个项目的计数可能不同。在DB中,只有两个列保存项目ID和用户名。有人可以帮我解决这个问题吗?

希望它有所帮助。

如果您需要更多详情,请回复

由于

Sreer

1 个答案:

答案 0 :(得分:1)

从上面的评论中,失败的实际行是:

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

在这里,您实际上是将此字符串传递给Oracle:

INSERT INTO CUST_RALLY_TEAM_MEMBER VALUES projectValue,columnValue)

您需要传递这些变量的(您还缺少一个左括号:

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

这假设cust_rally_team_member只包含这两列,如果不是(并且最佳实践,即使它确实如此),您应该指定要插入的列。假设列名也是“projectvalue”和“columnvalue”,那么语句将是:

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

另请注意,预备语句对于此类流程来说是一种很好的方法。如果值包含单引号,则可以消除SQL错误的风险,降低SQL注入攻击的风险,并通过仅编译语句一次来提高性能。

相关问题