有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring mvc jsp MySQLSyntaxErrorException

我试图从MYSQL数据库中捕获一个参数,并通过spring mvc将其打印到JSP页面上

我的控制器中有以下功能:

@RequestMapping(value = "/showentry")
public ModelAndView showentry(@RequestParam("id") String id){
    System.out.println("xxxxxxxxxxxxxxxxxxxxxxxx" + id);
    entries = dao.search(id);
    ModelAndView mav = new ModelAndView();
    mav.addObject("list", entries);
    return mav;
}

从以下表格中获取参数“Id”:

  <form action="showentry">
    <input type=  "hidden" name="id" value = "${item.id}">
    <button> Show Entry </button>
  </form>

调用showentry方法的函数位于DAO类中:

@Override
public List<Entry> search(String id) {
    List<Entry> res = new ArrayList<Entry>();
    String sql = "SELECT * FROM Person WHERE Id = ? ;";
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet resultSet = null;
    try {
        //open connection
        conn = dataSource.getConnection();

        //prepare the statement
        ps = conn.prepareStatement(sql);

        //bind parameters to preparedstatement
        ps.setString(1, id);

        //execute the statement
        resultSet = ps.executeQuery(sql);

        while (resultSet.next()) {
            Entry entry = new Entry();
            entry.setId(resultSet.getInt("id"));
            entry.setCn(resultSet.getString("cn"));
            entry.setSn(resultSet.getString("sn"));
            entry.setPn(resultSet.getString("pn"));
            res.add(entry);
        }
    } catch (SQLException ex) {
        //[...]
    }
    return res;
}

我可以在日志上打印与条目相关的id字符串,例如: XXXXXXXXXXXXXXXXXXXX 32

但它不会在jsp上打印,并返回一个错误:

net.tirasa.springaddressbook.SpringEntryDAO search
GRAVE: null
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 '?' at line 1

sql字符串有问题吗


共 (1) 个答案

  1. # 1 楼答案

    您是否尝试删除sql语句中的分号

    String sql=“SELECT*FROM Person,其中Id=?