2015-07-10 93 views
2

我寫RESTful Web服務中插入動態表到數據庫中。我在某個地方出了問題,而且這不是我想要做的。有人可以請弄清楚,糾正我在哪裏做錯了嗎?REST服務來創建動態表到數據庫從休眠

持久化類:

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) 

如果你不想回答,請忽略它,而不是反對投票/阻塞它。

+0

我不確定是不是你的錯誤的原因(可能不是),但是當添加列定義時,確保從'columns'和'datatypes'追加當前元素列表到StringBuilder,而不是列表本身。 – mthmulders

回答

2

問題可能是由於您已將columns變量重新分配給一個新的空ArrayList而導致的。這意味着沒有列將被添加到查詢中,並且此類lastIndexOf()將返回-1。如果columns.size()爲零

+0

是的你是對的。任何代碼建議來解決它? – arch

+0

如何刪除語句'columns = new ArrayList <>();'? – mthmulders

+0

有問題的語句是在持久類的'createTable'方法;它重新爲其輸入參數之一分配一個新的(因此是空的)列表。順便說一下,標記方法參數'final'確保重新分配是不可能的。 – mthmulders

1

更換帶支架的最後一個逗號將無法正常工作。做類似於:

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