如何将这个 postgresql 查询压缩到 java 中的一条语句中?

发布于 2024-10-21 21:14:07 字数 1403 浏览 2 评论 0原文

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();
            }
        }

    }

我可以将 squery 和 query 变量合并到一个查询中吗?如果是,那么如何?

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 ? If yes , then how ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回眸一遍 2024-10-28 21:14:07

也许这会对您有所帮助

参数不能用于参数化表或参数化任何数据库对象。它们主要用于参数化 WHERE/HAVING 子句。

要执行您想要的操作,您需要自己进行替换并根据需要创建常规语句。

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.

To do what you want, you'll need to do the substitution yourself and create a regular statement as needed.

仄言 2024-10-28 21:14:07

为什么不在创建表时将所有列添加到表中。这样您就可以避免第二个 ALTER TABLE 查询。

字符串查询 = "CREATE TABLE EtherMap "+table_name+" ("+ feature_name+" 字符(20), geom int , shape 字符(10)";

Why don't you add all the columns to the table while creating it. That way you avoid the second ALTER TABLE query.

String query = "CREATE TABLE EtherMap "+table_name+" ("+ feature_name+" character(20), geom int , shape character(10)";

帥小哥 2024-10-28 21:14:07

您可以简单地按照 CREATE TABLE 语法来编写单个查询。

String query = "create table "+table_name+" ( geom int, shape character(10) );";

You can simply follow the CREATE TABLE syntax to write a single query.

String query = "create table "+table_name+" ( geom int, shape character(10) );";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文