• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MySQLSyntaxErrorException

 
Irwin Raja
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to connect my database which has user details, i.e, Uname, Pass..
and I'm trying to validate my jsp file where in user enters these details and upon validation gets redirected to next page(home page)..
but when I do so I'm getting this error ::: 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 '='Irwin' and Pass='1234'' at line 1

I have double checked my database connections and code.. I don't seem to find any error.. Need help

Login Servlet....

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author o
*/
@WebServlet(urlPatterns = {"/Login"})
public class Login extends HttpServlet {

/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Connection conn = null;

try {
String uid = request.getParameter("uid");
String cpass = request.getParameter("cpass");
String url = "jdbc:mysql://localhost/sikka";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url,"root","root");
PreparedStatement ps = conn.prepareStatement(url);
// Statement s = conn.createStatement();
String sql = "Select Uname from clientdb" + "where Uname ='" + uid + "'" + " and Pass='" + cpass + "'";
ps.executeQuery(sql);
ResultSet rs = ps.getResultSet();
while(rs.next()){
String uidVal = rs.getString("uidVal");
out.println("Uname = " + uidVal);
}
rs.close();
ps.close();

} catch(Exception e) {
out.println(e.toString());
} finally {
out.close();
if (conn != null) {
try {
conn.close();
out.println("Database Connection Terminated !");
} catch (Exception e) {
}
}
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Irwin Raja wrote:I have double checked my database connections and code.. I don't seem to find any error.. Need help

Print out the value of the sql variable after this line and you should see the problem.
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no space between clientdb and where so the query is Select Uname from clientdbwhere Uname 'whaever' and Pass='whatever'
 
Irwin Raja
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much... It solved my problem...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic