[英]How do I compress both this postgresql query into one statement in java?
package database;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import database.Dbconnect;
public class CreateQuery {
Connection conn;
public CreateQuery() throws ClassNotFoundException, SQLException, IOException {
conn=new Dbconnect().returnDatabaseConnection();
}
public int addNewLayertoDB(String feature_name,String shape,int Latitude , int Longitude , int feature_geom , String feature_details){
try {
PreparedStatement statement = null;
String table_name = feature_name + "_" + shape;
String query = "CREATE TABLE EtherMap "+table_name+" ("+ feature_name+" (20))";
statement = conn.prepareStatement(query);
statement.setString(1, feature_name);
statement.execute();
String squery = "ALTER TABLE EtherMap" +table_name+" ADD COLUMN geom int , ADD COLUMN shape character(10)";
return 1;
} catch (SQLException ex) {
return 0;
}
}
public void closeConn() throws SQLException {
if (conn != null) {
this.conn.close();
}
}
}
Can I club both squery and query variable into one query ? 我可以将squery和query变量合并为一个查询吗? If yes , then how ?
如果可以,那怎么办?
Maybe this will help you : 也许这会帮助您 :
Parameters cannot be used to parameterize the table, or parameterize any database objects.
参数不能用于参数化表或参数化任何数据库对象。 They're mostly used for parameterizing WHERE/HAVING clauses.
它们主要用于参数化WHERE / HAVING子句。
To do what you want, you'll need to do the substitution yourself and create a regular statement as needed.
要执行所需的操作,您需要自己进行替换并根据需要创建一个常规语句。
您只需遵循CREATE TABLE语法即可编写单个查询。
String query = "create table "+table_name+" ( geom int, shape character(10) );";
Why don't you add all the columns to the table while creating it. 为什么在创建表时不将所有列添加到表中。 That way you avoid the second ALTER TABLE query.
这样,您可以避免第二个ALTER TABLE查询。
String query = "CREATE TABLE EtherMap "+table_name+" ("+ feature_name+" character(20), geom int , shape character(10)";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.