开发者

How do I compress both this postgresql query into one statement in java?

开发者 https://www.devze.com 2023-02-17 04:59 出处:网络
package database; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement;
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 ?


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.


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)";


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

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

精彩评论

暂无评论...
验证码 换一张
取 消