Reputation: 107
I'm trying to catch a parameter from a MYSQL DB and print it on a JSP page through spring mvc.
I have the following function in my controller:
@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;
}
that get the parameter "Id" from the following form:
<form action="showentry">
<input type= "hidden" name="id" value = "${item.id}">
<button> Show Entry </button>
</form>
the function that calls the showentry method is in a DAO class:
@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;
}
I can print on the logs the string with the id related to the entry, for example: xxxxxxxxxxxxxxxxxxxxxxxx 32
But it doesn't print on the jsp, and it returns an error:
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
Is there something wrong with the sql string ?
Upvotes: 0
Views: 71
Reputation: 69440
You have to remove the patameter in resultSet = ps.executeQuery(sql);
it must be
resultSet = ps.executeQuery();
See the Javadoc from the method executeQuery(String sql)
:
Note:This method cannot be called on a PreparedStatement or CallableStatement
Also remove the ;
at the end of your statement:
String sql = "SELECT * FROM Person WHERE Id = ?";
Upvotes: 2
Reputation: 430
Did you try removing semicolon inside the sql
String sql = "SELECT * FROM Person WHERE Id = ? ;";
Upvotes: 0