每次在Java SQL表中更改具有新列名的表

时间:2016-06-02 07:21:41

标签: java sql

我想基于数据添加列意味着如果在仓库中获得5个值,则在仓库中添加5列,如仓库1,仓库2,..........等。

//connection is defined here
Connection con = DbUtil.getConnection();
PreparedStatement stm = null;
ResultSet rs = null;    

int i = 1;
// in workaddress am getting unlimited value , i want to add column till condition got false
if(request.getParameter("workaddress") != null)        
{         
     // here i have define column name warehouse + i with increment but i got error duplicate column name                   
     stm = con.prepareStatement("ALTER TABLE tbl_buyer ADD warehouse" + i++ + " VARCHAR(100)");                        
     stm.executeUpdate();
}

1 个答案:

答案 0 :(得分:0)

尝试在之前添加i ++(或使用++ i),因为它影响了值然后它会增加,因此它会抛出重复的列名错误。

i++;
stm = con.prepareStatement("ALTER TABLE tbl_buyer ADD warehouse" + i + " VARCHAR(100)");