gpt4 book ai didi

java - 用于从 Hibernate 将动态表创建到数据库中的 REST 服务

转载 作者:行者123 更新时间:2023-11-30 08:11:00 24 4
gpt4 key购买 nike

我正在编写 RESTful 网络服务以将动态表插入数据库。我在某处出错了,它没有像我想做的那样工作。有人可以找出并纠正我做错的地方吗?

持久化类:

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)

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

最佳答案

问题可能是由于您使用新的空 ArrayList 重新分配了 columns 变量造成的。这意味着不会将任何列添加到查询中,这样的 lastIndexOf() 将返回 -1。

关于java - 用于从 Hibernate 将动态表创建到数据库中的 REST 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31335611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com