如何在java中串联两行?

时间:2019-01-16 07:41:59

标签: java sql jdbc string-concatenation

   public void ReserveTimeList(){ 
    String sql = "select to_char(to_char(start_time,'HH24:MI') ||' - '|| 
    to_char(end_time,'HH24:MI')) from register_table";
    try {
        DefaultListModel dlm = new DefaultListModel();
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        while(rs.next()){

             //SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
           //  dlm.addElement(format.parseObject(rs.getString("time")));

   dlm.addElement(rs.getString("to_char(to_char(start_time,'HH24:MI') ||' - 
   '|| to_char(end_time,'HH24:MI'))"));
            lstResTime.setModel(dlm);
        }
       } catch (SQLException ex) {
          Logger.getLogger(Time_Setting.class.getName()).log(Level.SEVERE, 
       null, ex);
      }

    }

该程序是正确的,但是我不知道出了什么问题, 当我运行程序时,异常arias:java.sql.SQLException:无效的列名

1 个答案:

答案 0 :(得分:2)

我要添加一个别名,例如my_time

String sql = "select to_char(to_char(start_time,'HH24:MI') ||' - '|| 
    to_char(end_time,'HH24:MI')) as my_time from register_table";
 //...

 dlm.addElement(rs.getString("my_time"))l