title: How to use prepared statement for select query in Java?
body: <p>I had tried several times using prepared statements but it returns SQL exception. here is my code:</p>
  
  <pre><code>public ArrayList&lt;String&gt; name(String mobile, String password) {
      ArrayList&lt;String&gt; getdata = new ArrayList&lt;String&gt;();
      PreparedStatement stmt = null;
      try {
          String login = "select mobile, password from tbl_1 join tbl_2 on tbl_1.fk_id=2.Pk_ID where mobile=? and password=?";
  
          String data = "select * from tbl_2  where password='" + password + "'";
  
          PreparedStatement preparedStatement = conn.prepareStatement(login);
  
          preparedStatement.setString(1, mobile);
          preparedStatement.setString(1, password);
  
          ResultSet rs = preparedStatement.executeQuery(login);
  
          Statement stmts = (Statement) conn.createStatement();
  
          if (rs.next()) {
              System.out.println("Db inside RS");
              ResultSet data = stmts.executeQuery(data);
  
              while (data.next()) { /* looping through the resultset */
  
                  getdata.add(data.getString("name"));
                  getdata.add(data.getString("place"));
                  getdata.add(data.getString("age"));
                  getdata.add(data.getString("job"));
              }
  
          }
  
      } catch (Exception e) {
          System.out.println(e);
      }
  
      return getdata;
  }
  </code></pre>
  
  <p>While running this, I got the following SQL exception:</p>
  
  <pre class="lang-none prettyprint-override"><code>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 '? and password=?' at line 1.
  </code></pre>
  
  <p>Any suggestion to make this work? 
  any piece of code is appreciated. </p>