2011-03-14 32 views
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和查询变量分成一个查询吗?如果是,那么如何?如何将这个postgresql查询压缩到java中的一个语句中?

+0

你是说'CREATE TABLE'和'ALTER TABLE'? – bluefoot 2011-03-14 15:26:56

+0

只能显示**有问题的SQL语句吗?这会让试图帮助你的人更容易。 – 2011-03-14 15:28:51

+1

对'PreparedStatement.setString'的调用在文本本身没有查询参数的情况下无法工作。你应该传递类似于''CREATE TABLE?(?VARCHAR(20),geom int,shape char(10))'',用?为您想要动态设置的每个值。 – 2011-03-14 15:29:57

回答

1

Maybe this will help you

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

要做你想做的事,你需要自己做替换,并根据需要创建一个正则表达式。

0

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

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

不起作用。您无法将表名称传递给PreparedStatement – 2011-03-14 15:31:28

+0

@a_horse_with_no_name:这很不幸。 – Jeremy 2011-03-14 15:32:07

0

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

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

相关问题