简体   繁体   English

如何将这两个postgresql查询压缩为Java中的一个语句?

[英]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.

相关问题 我如何同时使用&&和|| 在if语句中 - How do I use both && and || in an if statement 如果一个是一对而另一个不是很不幸,但如果两个都是一对,我该如何做if语句? - How do i make an if statement if one is a pair and other is not it is not lucky but if both is a pair it is lucky? 如何压缩由OR运算符组成的if语句? - How do you compress an if statement comprised of OR operators? 如何在Java中同时获取带有int选项或char选项(包括扫描仪)的switch语句 - How do i Get a switch statement with both a int option or char option (scanner included) in java Java:当两者都在同一个包中时,如何从当前应用程序启动一个独立的应用程序? - Java: How do I start a standalone application from the current one when both are in the same package? 如何使用clojure.java.jdbc从Postgresql查询JSON数据类型? - How do I query a JSON datatype from postgresql with clojure.java.jdbc? 对于 Java,如果我想做一个 if 语句以确保输入只包含一个字符,我应该如何进行? - For Java, if I want to do an if statement to make sure the input only takes in one character, how should I proceed? 如何在Java中压缩字符串? - How can I compress a string in java? 如何使用 java 压缩图像? - How can I compress images using java? 如何在 Docker 和 PostgreSQL 数据库中同时使用 Java? - How to use both Java in Docker and PostgreSQL database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM