@Throws(Exception::class) fun listItemsSortedSecure() { println("Secure Systems Inc. - list items") var order: String = input("order (id, name)?") if (!order.matches("[a-zA-Z0-9_]*")) { order = "id" } try { val rs: ResultSet = stat.executeQuery("SELECT ID, NAME FROM ITEMS ORDER BY $order") while (rs.next()) { println(rs.getString(1).toString() + ": " + rs.getString(2)) } } catch (e: SQLException) { System.out.println(e) } }
List items using a specified sort order . The method is secure as the user input is validated before use . However the database has no chance to verify this .