2008年7月17日星期四

写代码一定要小心

今天写程序时遇到一个问题,调试了半天没头绪,结果最后发现是一个非常脑残的问题:
问题代码如下:
        PreparedStatement ps = conn.prepareStatement(
                "INSERT INTO certs"/* +
                "   [CertData] ," +
                "   [PK_DUMP] ," +
                "   [PK_N],"+
                "   [PK_E],"+
                "   [Signature] ," +
                "   [Version] ," +
                "   [SigAlgName] ," +
                "   [SigAlgOID] ," +
                "   [SigAlgParams] ," +
                "   [SubjectDN] ," +
                "   [IssuerDN] ," +
                "   [CertDump] ," +
                "   [ServerName]) "*/ +
                "VALUES (null , ? , ? , ? ,? , ? , ? , ? , ? , ? , ? , ? , ? , ?)");
        ps.setBytes(1, cert.getTBSCertificate());
        System.out.println(cert.getTBSCertificate().length);
        if (goodPk) {
            ps.setBytes(2, pk.getEncoded());
            System.out.println(pk.getEncoded().length);
        } else {
            ps.setBytes(2, null);
        }
        if (rsapk != null) {
            ps.setBytes(3, rsapk.getModulus().toByteArray());
            ps.setBytes(4, rsapk.getPublicExponent().toByteArray());
        } else {
            ps.setBytes(3, null);
            ps.setBytes(4, null);
        }
        ps.setBytes(5, cert.getSignature());
        System.out.println(cert.getSignature().length);
        ps.setInt(6, cert.getVersion());
        ps.setString(7, cert.getSigAlgName());
        System.out.println(cert.getSigAlgName().length());
        ps.setString(8, cert.getSigAlgOID());
        System.out.println(cert.getSigAlgOID().length());
        if (cert.getSigAlgParams() != null) {
            ps.setBytes(9, cert.getSigAlgParams());
            System.out.println(cert.getSigAlgParams().length);
        } else {
            ps.setBytes(9, null);
        }
        ps.setString(10, cert.getSubjectDN().toString());
        System.out.println(cert.getSubjectDN().toString().length());
        ps.setString(11, cert.getIssuerDN().toString());
        System.out.println(cert.getIssuerDN().toString().length());
        ps.setString(12, cert.toString());
        ps.setString(13, serverName);
        return ps.executeUpdate();
你能看出这段代码有什么问题么?无论怎么改,总是提示我SQL语法出错=,=
看出来了么?问题在这里:
        PreparedStatement ps = conn.prepareStatement(
                "INSERT INTO certs"/* +
                "   [CertData] ," +
                "   [PK_DUMP] ," +
                ...
                "   [ServerName]) "*/ +
                "VALUES (null , ? , ? , ? ,? , ? , ? , ? , ? , ? , ? , ? , ? , ?)");
吧列名注释掉以后,表名和VALUES关键字后面少了一个空格!,所以无论如何都会出错……
所以遇到什么奇怪的问题时不妨仔细看看代码……不要像我今天这么脑残~~

0 人次吐槽:

发表评论