0

Somebody please help me.I am doing things right but i am getting an error.It is a JAVA application linked to MYSQL wamp server.

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 'Chege' at line 1

MY CODE:

public class MyQuery {

    public Connection getConnection() {
        Connection con = null;
        try {
            con = DriverManager.getConnection("jdbc:mysql://"
                    + "localhost:3306/employee_certificate", "root", "");
        } catch (SQLException ex) {
            Logger.getLogger(Query.class.getName())
                    .log(Level.SEVERE, null, ex);
        }
        return con;
    }

    public ArrayList<Item> getData(String EmpName) {
        ArrayList<Item> list = new ArrayList<Item>();
        Connection con = getConnection();
        Statement st;
        ResultSet rs;
        try {
            st = con.createStatement();
            rs = st.executeQuery("SELECT Emp_Id, Emp_Name, Department "
                    + "FROM staff WHERE Emp_Name = " + EmpName + " ");
            Item I;
            while (rs.next()) {
                I = new Item(
                        rs.getString("Emp_Id"),
                        rs.getString("Emp_Name"),
                        rs.getString("Department"));
                list.add(I);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }
}
4
  • 2
    Put the value of EmpName in quotes or even better use Prepared Statements Commented Jul 17, 2016 at 12:12
  • quotes are not working Commented Jul 17, 2016 at 12:17
  • WHERE Emp_Name = '"+EmpName+"' " Mind the quotes. Commented Jul 17, 2016 at 12:19
  • Thank you so much @juergen d. It works! Commented Jul 17, 2016 at 12:40

2 Answers 2

3

Your query string is not correct. Should be something like the following:

rs=st.executeQuery("SELECT Emp_Id, Emp_Name, Department "
          + "FROM staff WHERE Emp_Name = '"+EmpName+"'");

But I'd recommend to use a PreparedStatement object for sending SQL statements to the database.

String query = "SELECT Emp_Id, Emp_Name, Department FROM staff WHERE Emp_Name = ?";
PreparedStatement preStatement = conn.prepareStatement(query);
preStatement.setString(1, EmpName);
ResultSet result = preStatement.executeQuery();

This approach is safer and more convenient.

Sign up to request clarification or add additional context in comments.

Comments

1

You have a little problem in your query:

try {
    st = con.createStatement();
    //Add quotes 'YourString'
    rs = st.executeQuery("SELECT Emp_Id, Emp_Name, Department "
            + "FROM staff WHERE Emp_Name = '" + EmpName + "' ");
    Item I;
    while (rs.next()) {
        I = new Item(rs.getString("Emp_Id"), rs.getString("Emp_Name"), rs.getString("Department"));
        list.add(I);
    }
} catch (SQLException ex) {
    Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
}

or for a safety Query use prepared statement:

try {
    PreparedStatement ps = connection.prepareStatement("SELECT Emp_Id, Emp_Name, Department FROM staff WHERE Emp_Name = ?");
    ps.setString(1, EmpName);
    rs = ps.executeUpdate();
    Item I;
    while (rs.next()) {
        I = new Item(rs.getString("Emp_Id"), rs.getString("Emp_Name"), rs.getString("Department"));
        list.add(I);
    }
} catch (SQLException ex) {
    Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.