jdbc -PSQLException:类型timestamp / date / time的值不正确:{1}

时间:2017-04-20 10:44:07

标签: java mysql postgresql jdbc

我在Web应用程序的日期字段上执行选择时遇到问题。 我已经用这种方式定义了数据:

Date data = new Date(System.currentTimeMillis());
            SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
            String dateTemp2 = dateFormat2.format(data.getTime());
    risultato_vendita = bean.inserimento_vendita(scontrino,data,prodotto1, quantita1);

在DB data_vendita中,Date字段不为空。插入后,我通过在pgadmin中进行选择来看到这样的日期:2017-04-20

这是进行选择的功能:

public int inserimento_vendita(int scontrino, Date data, String prodotto, int quantita) throws eccezione {

        int risultato = 0;
        String insert = "INSERT INTO farmacia.vendita (id, data_vendita, prodotto, quantita) "+
                        "VALUES (?, ?, ?, ?) ";
        PreparedStatement stmt = null;
        try 
        {
            connect();
            stmt = getStatement(insert);
            stmt.setInt(1, scontrino);
            stmt.setDate(2, data);
            stmt.setString(3, prodotto);
            stmt.setInt(4, quantita);
            risultato = executeUpdate(stmt);
        } 
        catch (SQLException ex) {
            System.out.println("\n--- SQLException catturata ---\n");
            Logger.getLogger(SessionBean.class.getName()).log(Level.SEVERE, null, ex);} catch (ClassNotFoundException ex) {
            Logger.getLogger(SessionBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            try
            {
                stmt.close();
                close();
            }
            catch (SQLException ex)  
            {
                Logger.getLogger(SessionBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        } 
        return risultato;

    }

我发现了这个错误:

org.postgresql.util.PSQLException: Bad value for type timestamp/date/time: {1}
        at org.postgresql.jdbc.TimestampUtils.parseBackendTimestamp(TimestampUtils.java:358)
        at org.postgresql.jdbc.TimestampUtils.toTimestamp(TimestampUtils.java:390)
        at org.postgresql.jdbc.TimestampUtils.toDate(TimestampUtils.java:468)
        at org.postgresql.jdbc.PgResultSet.getDate(PgResultSet.java:516)
        at org.postgresql.jdbc.PgResultSet.getDate(PgResultSet.java:2378)
        at session.SessionBean.lista_vendite(SessionBean.java:1030)
        at org.apache.jsp.report_jsp._jspService(report_jsp.java:83)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
        at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
        at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
        at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.NumberFormatException: Expected time to be colon-separated, got '

这是表格声明:

CREATE TABLE vendita (
    id integer NOT NULL,
    prodotto character varying(30) NOT NULL,
    data_vendita date NOT NULL,
    quantita integer NOT NULL
);

编辑:lista_vendite

<%
SessionBean b = new SessionBean();

List<Vendita> lista_vendite = b.lista_vendite();

%GT;

<table class="myTable">
         <thead>
    <tr>
      <th>Codice</th>
      <th>Prodotto</th>
    <th>Data</th>
   </tr>
  </thead>   
  <tbody>         
             <%


        for(Vendita e: lista_vendite){  

        out.print("<tr><td>"+e.getVenditaPK()+"</td><td>"+e.getQuantita()+"</td><td>"+e.getDataVendita()+"</td></tr>");


}

        %>
  </tbody>
     </table>   

编辑:lista_vendite方法

public List<Vendita> lista_vendite() throws eccezione{

        int risultato = 0;
        VenditaPK venditaPK;
        List<Vendita> lista = new ArrayList<Vendita>();

        String select = "SELECT * FROM farmacia.vendita";
        PreparedStatement stmt = null;
        ResultSet res = null;

        try 
        {
            connect();
            stmt = getStatement(select);
            res = executeQuery(stmt);
             while(res.next())
            {
                System.out.println("Iterazione");
                Vendita d1 = new Vendita();
                venditaPK = new VenditaPK();
                d1.setDataVendita(res.getDate(1));
                d1.setQuantita(res.getInt(2));

                venditaPK.setId(res.getInt("id"));
                venditaPK.setProdotto(res.getString("prodotto"));
                d1.setVenditaPK(venditaPK);
                lista.add(d1);
            }
        } 
        catch (SQLException | ClassNotFoundException ex) 
        {
            ex.printStackTrace();
            Logger.getLogger(SessionBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        finally
        {
            try
            {
                stmt.close();
                res.close();
                close();
            }
            catch (SQLException ex) 
            {
                Logger.getLogger(SessionBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return lista;


}

0 个答案:

没有答案