4
回答
关于DbUtils和postgresql数据库使用的时候,有问题。
开发十年,就只剩下这套Java开发体系了   
 public void fillStatement(PreparedStatement stmt, Object... params)
        throws SQLException {

        if (params == null) {
            return;
        }
        
        ParameterMetaData pmd = null;
        if (!pmdKnownBroken) {
            pmd = stmt.getParameterMetaData();
            if (pmd.getParameterCount() < params.length) {
                throw new SQLException("Too many parameters: expected "
                        + pmd.getParameterCount() + ", was given " + params.length);
            }
        }
        for (int i = 0; i < params.length; i++) {
            if (params[i] != null) {
                stmt.setObject(i + 1, params[i]);
            } else {
                // VARCHAR works with many drivers regardless
                // of the actual column type.  Oddly, NULL and 
                // OTHER don't work with Oracle's drivers.
                int sqlType = Types.VARCHAR;
                if (!pmdKnownBroken) {
                    try {
                        sqlType = pmd.getParameterType(i + 1);
                    } catch (SQLException e) {
                        pmdKnownBroken = true;
                    }
                }
                stmt.setNull(i + 1, sqlType);
            }
        }
    }

发生问题的代码我贴出来,红色的代码无法执行通过。

发生异常:

 ava.sql.SQLException: Can't infer the SQL type to use for an instance of [Ljava.lang.String;. Use setObject() with an explicit Types value to specify the type to use. Query: INSERT INTO jrails_blogs (blog_styleid,blog_typeid,blog_title,blog_content) VALUES (?,?,?,?) Parameters: [[2], [1], [dsfsd], [asdfasdf<br />
]]
 at org.apache.commons.dbutils.QueryRunner.rethrow(QueryRunner.java:542)
 at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:599)
 at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:655)
 at com.lovver.jrailsblog.models.Blogs.save(Blogs.java:27)
 at com.lovver.jrailsblog.controllers.BlogsController.submit(BlogsController.java:47)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at com.lovver.jrails.JRailsRequestProcessor.processRequest(JRailsRequestProcessor.java:194)
 at com.lovver.jrails.JRailsRequestProcessor.process(JRailsRequestProcessor.java:59)
 at com.lovver.jrails.JRailsServlet.prosess(JRailsServlet.java:157)
 at com.lovver.jrails.JRailsServlet.service(JRailsServlet.java:55)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
 at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
 at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
 at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
 at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
 at java.lang.Thread.run(Unknown Source)

 

 

举报
joliny
发帖于8年前 4回/2K+阅
共有4个答案 最后回答: 8年前
数组的那个是dbutils代码里面如果出了异常它做了这样的处理。
/**
     * Throws a new exception with a more informative error message.
     * 
     * @param cause The original exception that will be chained to the new 
     * exception when it's rethrown. 
     * 
     * @param sql The query that was executing when the exception happened.
     * 
     * @param params The query replacement parameters; <code>null</code> is a 
     * valid value to pass in.
     * 
     * @throws SQLException if a database access error occurs
     */
    protected void rethrow(SQLException cause, String sql, Object... params)
        throws SQLException {

        String causeMessage = cause.getMessage();
        if (causeMessage == null) {
            causeMessage = "";
        }
        StringBuffer msg = new StringBuffer(causeMessage);

        msg.append(" Query: ");
        msg.append(sql);
        msg.append(" Parameters: ");

        if (params == null) {
            msg.append("[]");
        } else {
            msg.append(Arrays.deepToString(params));
        }

        SQLException e = new SQLException(msg.toString(), cause.getSQLState(),
                cause.getErrorCode());
        e.setNextException(cause);

        throw e;
    }

所以看起来像是数组,其实我给的参数全部都是字符串!

     Map param 通过函数参数传递过来的。

  inserts = run.update( "INSERT INTO jrails_blogs (blog_styleid,blog_typeid,blog_title,blog_content) VALUES (?,?,?,?)",
        param.get("blogstyle"),
         param.get("blogtype"),
         param.get("blogtitle"),
        param.get("blog_content") );

顶部