0

Very first question here so I apologize for any mistakes and imperfections.

Basically there are three files, my main method tech_supportv1, login_controller containing a class used to store a bunch of methods, and login.java, a javabean. The point is to check if a certain row exists on the tech_support database. To do so I'm trying to use the code below. (db_util and type are classes containing connection data, they are tested and they work).

ISSUE: the data from the main method seems not be pasted into the string in the appropriate placeholders, and an error is returned. (Of course if I manually enter the strings instead of using placeholders, everything works just fine.)

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''technicians' WHERE 'techID' LIKE 16' at line 1

I tired to look at the mariaDB docs but all the other syntax provided yields the same results.

So this the main method.

public class Tech_support_v1 {

public static void main(String[] args) {

    System.out.println("Start.");

    login bean = new login();

    bean.setTable("technicians");
    bean.setColumn("techID");
    bean.setID(16);

    login_controller.select(bean);

}

}

This is the select method (with bean as argument, login is the Javabean class).

public static boolean select(login bean) {

    String sql = "SELECT * FROM ? WHERE ? LIKE ?";

    ResultSet rs;

    try (
            Connection conn = db_util.getConn(db_type.MYSQL);
            PreparedStatement stmt = conn.prepareStatement(sql);
            ) {

        stmt.setString(1, bean.getTable());
        stmt.setString(2, bean.getColumn());
        stmt.setInt(3, bean.getID());

        rs = stmt.executeQuery();

        if (rs.next()) {
            System.out.println("Y");
            return true;
        } else {
            System.err.println("N");
            return false;
        }

    } catch (Exception e) {
        System.err.println(e);
        return false;
    }

}

I won't include the bean class because it's literally only three variables with the relative set/get methods. Also the database runs with MariaDB, and is MySQL.

Thanks to everyone in advance.

Barbaldo
  • 72
  • 3
  • 11

3 Answers3

0

You are comparing with LIKE but you are setting an Int. Change the LIKE to = and see if it works.

Daniel Pereira
  • 2,720
  • 2
  • 28
  • 40
0

You have multiple problems with your code :-) First, you can't set table or column names with "setString" in a Prepared Statement! See this Question: How to use a tablename variable for a java prepared statement insert Second, as Daniel Pereira pointed out: You are trying to use a "like" Statement with "setInt"! See: https://dev.mysql.com/doc/refman/8.0/en/pattern-matching.html

Chris Eibl
  • 581
  • 6
  • 6
0

Try doing something like this:

String sql="SELECT * FROM :table ";


try (
            Connection conn = db_util.getConn(db_type.MYSQL);
            PreparedStatement stmt = conn.prepareStatement(sql);
            )
{
   String query = StringUtils.replace(sql, ":table", bean.getTable());
   stmt.executeQuery(query);
}
phoenix
  • 38
  • 5