Xuanwomingren01 2017-01-16 06:11 采纳率: 61.1%
浏览 867
已采纳

谁能看下那里错了,sql问题

public List SerachLog(String module, String target, String method, String targetID, String eventType,
String eventLevel) {
// TODO Auto-generated method stub
List logs = new ArrayList();
try {
/**
* 拼接sql
*/

        String sqlStr3 = "select * from logs where 1 = 1";

        if (!"".equals(module) && module != null) {
            sqlStr3 += "and module='" + module + "' ";
        }
        if (!"".equals(target) && target != null) {
            sqlStr3 += "and target='" + target + "' ";
        }
        if (!"".equals(method) && method != null) {
            sqlStr3 += "and method='" + method + "' ";
        }
        if (!"".equals(targetID) && targetID != null) {
            sqlStr3 += "and targetID='" + targetID + "' ";
        }
        if (!"".equals(eventType) && eventType != null) {
            sqlStr3 += "and eventType='" + eventType + "' ";
        }
        if (!"".equals(eventLevel) && eventLevel != null) {
            sqlStr3 += "and eventLevel='" + eventLevel + "' ";
        }

        Connection con = JDBC.open();
        PreparedStatement ps = con.prepareStatement(sqlStr3);
        ResultSet rs = ps.executeQuery();//一会儿提示这里错误
        while (rs.next()) {
            Log log = new Log(rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9),
                    rs.getString(10));
            logs.add(log);//一会儿又说这里错了
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return logs;
}
----------------------------------------------------------
错误提示
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'module='n'' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2293)
at cn.kerui.adapter.DBAdapter.SerachLog(DBAdapter.java:125)
at cn.kerui.test.Test1.main(Test1.java:49)
----------------------------------------------
List<Log> Q = as.SerachLog("n", null, null, null, null, null);
    System.out.println(Q.size());
    测试语句
  • 写回答

7条回答 默认 最新

  • haikuokuangtu 2017-01-16 06:18
    关注

    首先不知道你的1=1是干什么的,其次每一个and前面要有空格

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名