【问题标题】:adding new column to table with column name as present days date将新列添加到表中,列名称为当前日期
【发布时间】:2013-03-02 05:50:35
【问题描述】:

我是 java 新手。我想为考勤管理系统构建简单的 java 应用程序。为此,我想在现有表中创建一个新列,列名应该是当前日期。我使用日期选择器获取现在的日期。 我为此编写了以下代码,但表中的列名设置为“+s1+”。 这是我的代码:

    String s="";  
    s=date.getText();//selected data from date picker
    s.replace(".","_");
    try{
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       con = DriverManager.getConnection("jdbc:odbc:vasu");
       st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
       //System.out.println("ALTER TABLE attendance  ADD "+s+" varchar(50);");
       rs = st.executeQuery("ALTER TABLE attendance  ADD '"+s+"' varchar(50);");
    }
    catch(Exception ex){
    }

谁能帮帮我 好提前谢谢。

【问题讨论】:

  • catch(Exception ex){ } 更改为catch(Exception ex){ ex.printStackTrace(); }

标签: java database


【解决方案1】:

试试这个

executeUpdate如果查询正常返回1,否则返回0

String s="";  
s=date.getText();//selected data from date picker
s.replace(".","_");
try
{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    ResultSet acrs;
    String op = "jdbc:odbc:vasu";
    Connection cnn = DriverManager.getConnection(op,"username", "password");
    Statement mystmt = cnn.createStatement();

    int successOrFailure = mystmt.executeUpdate("ALTER TABLE attendance ADD '"+s+"' VARCHAR(50)");
    //executeUpdate return 1 if the query work properly otherwise it will return 0

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

【讨论】:

  • 你能告诉我 i 的用途吗
  • executeUpdate 如果查询正常则返回 1,否则返回 0...这样我们就可以检查我们的查询是否正常运行,没有任何异常
猜你喜欢
  • 1970-01-01
  • 2019-09-16
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
相关资源
最近更新 更多