aspose file tools*
The moose likes JDBC and the fly likes MySQLSyntaxErrorException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "MySQLSyntaxErrorException" Watch "MySQLSyntaxErrorException" New topic
Author

MySQLSyntaxErrorException

Irwin Raja
Greenhorn

Joined: Apr 29, 2012
Posts: 11
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

Joined: Aug 05, 2005
Posts: 3742
    
  16
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.

Joanne
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
there is no space between clientdb and where so the query is Select Uname from clientdbwhere Uname 'whaever' and Pass='whatever'


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Irwin Raja
Greenhorn

Joined: Apr 29, 2012
Posts: 11
Thank you very much... It solved my problem...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MySQLSyntaxErrorException