0

I wonder if there is a way to select table with user input in the following statement:

myStmt = db.myConn.prepareStatement("select * from Rooms where "
                + "idRooms = ?");
        myStmt.setInt(1, roomNum);
        myRs = myStmt.executeQuery();

Is there a possibility to do something like select * from =? where idRooms = ? to select Rooms with prepared statement upon user input?

myStmt.setString(1, Room);
mysStmt.setInt(2, roomID);

Thanks

G.M
  • 653
  • 1
  • 15
  • 35

2 Answers2

1

It is not possible to add a table name as a parameter. You can simply create a string containing your constructed query as follows:

String query = "select * from " + userParamVariable + " where idRooms = ?"
Byron
  • 1,313
  • 10
  • 22
0

I guess you manage your tables with splitting, but unfortunately PreparedStatement does not support it, you can manually replace the table name with String.format() method.