我想在按钮点击时在oracle表中添加一列

时间:2016-02-12 11:26:45

标签: java oracle swing

ad.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String name = txtnm.getText();
            String cnm = Integer.toString(id)+name;
            try {
                System.out.print(cnm);

                st = con.createStatement();  // st is an object of Statement class
                int result = st.executeUpdate("alter table emp_att add ("+cnm+" varchar(30))"); //how to pass coloumn name in sql query

                if(result !=0)
                {
                    JOptionPane.showMessageDialog(null, "Table Altered");
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Something Went Wrong..");
                }

            }
            catch (Exception e) 
            {
                System.out.println("Error : " + e);
            }
        }
    });

在上面的代码中我尝试在按钮单击时添加列,但发生SQLException并给出eroor作为ORA-00904 ::无效标识符如何解决此问题

1 个答案:

答案 0 :(得分:1)

ORA-00904告诉您列的名称无效。这是规则:

  • 它不能保留oracle word;
  • 必须以字母开头;
  • 小于或等于30个字符;
  • 仅包含字母数字和特殊字符;

由于您的列名称以数字而非字母开头,您会收到此错误