REST服务从Hibernate创建动态表到数据库

时间:2015-07-10 08:07:49

标签: java stringbuilder

我正在编写RESTful Web服务以将动态表插入数据库。我在那里的某个地方出错了,而且我的工作没有用。有人可以弄清楚并纠正我在哪里做错了吗?

坚持等级:

public void createTable(String tableName, List<String> columns, List<String> datatypes) {

         StringBuilder createTableQuery = new StringBuilder("CREATE TABLE IF NOT EXISTS `" + tableName + "` (");
          columns = new ArrayList<>();


         for (int i=0;i<columns.size();i++) {

                 createTableQuery.append("`" + columns + "` ");
                 createTableQuery.append(datatypes + ", ");
            }

             //To replace last ',' character and place the bracket.
            createTableQuery.replace(createTableQuery.lastIndexOf(","),createTableQuery.length(), ")");
        eTableQuery);

            Session session = null;
            Transaction transaction = null;
            try {

                session = HibernateUtil.getSessionFactory().getCurrentSession();
                transaction = session.beginTransaction();
                int count = session.createSQLQuery(createTableQuery.toString()).executeUpdate();   

                transaction.commit();
            } catch (Exception ex) {
                transaction.rollback();         
            }
    }    

服务类:

@POST
    @Path("/selfbi/addtable")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response createTable(JSONObject jsonObject, @QueryParam("uniqueid") String uniqueid, @QueryParam("sheetname") String sheetname) throws JSONException  {
        Response res = null;
        try{
        String tableName = "";
        if(jsonObject.has("tableName")){
            tableName = jsonObject.getString("tableName");
        }

        JSONArray jsonArray = jsonObject.getJSONArray("columns");

        List<String> col = new ArrayList<String>(); 
            if (jsonArray != null) { 
            for (int i=0;i<jsonArray.length();i++) { 
                col.add(jsonArray.getString(i));    
            } 
        }


        JSONArray json = jsonObject.getJSONArray("datatypes");
        List<String> dtype = new ArrayList<String>(); 
        if (json != null) { 
            for (int i=0;i<json.length();i++) { 
                dtype.add(json.getString(i));   
            } 
        }


        restService.createTable(tableName, col, dtype);



        res = Response.status(Status.CREATED).build();

        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("service------>>>"+res);
        return res;
    }  

控制台中的错误:

java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:789)
    at java.lang.StringBuilder.replace(StringBuilder.java:266)
    at com.acinfotech.timebound.jpa.service.ReportJobsPersistenceServiceImpl.createTable(ReportJobsPersistenceServiceImpl.java:7843)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy81.createTable(Unknown Source)
    at com.acinfotech.timebound.restservice.service.RestServiceImpl.createTable(RestServiceImpl.java:2751)
    at com.acinfotech.timebound.restservice.service.RestrsService.createTable(RestrsService.java:428)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

如果您不想回答,请忽略它而不是投票/阻止它。

2 个答案:

答案 0 :(得分:2)

问题可能是由于您已使用新的空columns重新分配ArrayList变量。这意味着不会向查询添加任何列,并且此类lastIndexOf()将返回-1。

答案 1 :(得分:1)

如果columns.size()为零,

用括号替换最后一个逗号将无效。做类似的事情:

if (columns.size() > 0){
createTableQuery.replace(createTableQuery.lastIndexOf(","),createTableQuery.length(), ")");
}