Gera Gera - 5 months ago 6
Java Question

MYSQL query using a session user in JAVA

I'm trying to run this this query from Eclipse:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
String name = session.getAttribute("user").toString();
String query = "SELECT DISTINCT t.text, t.user, t.date"
+ " FROM users u, tweets t, follows f"
+ " Where t.parent is null"
+ " AND u.id ="+name
+ " AND ( f.follower = u.id"
+ " AND f.followed = t.user"
+ " OR t.user = u.id)"
+ " ORDER BY t.date DESC;";


But I get the following error:

Sesion(user):Takeshi
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Takeshi' in 'where clause'


I can imagine the error is because I am doing wrong the comparison between u.id and name, but how should I do it? with some special character? Thanks in advance.

Answer

Could you please use following query string:

String query =   " SELECT DISTINCT t.text, t.user, t.date"
                + " FROM users u, tweets t, follows f" 
                + " Where t.parent is null"
                + " AND u.id = '"+name+"'"
                + " AND ( f.follower = u.id"
                + " AND f.followed = t.user"
                + " OR t.user = u.id)"
                + " ORDER BY t.date DESC";
Comments