Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

studentService.cfc 2.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  1. component {
  2. // index method to handle requests to the root URL or specified route
  3. function index(event, rc, prc) {
  4. // Your logic to process the request goes here
  5. // Example: Set a variable to pass data to the view
  6. prc.message = "Welcome to the index page!";
  7. // Example: Render a view
  8. event.setView("studentService/createStudent");
  9. }
  10. // Function to create a new student
  11. public void function createStudent(sname, major, level, byear) {
  12. // Initialize error variable
  13. var errorMessage = "";
  14. // Check if any form field is empty
  15. if (!len(arguments.sname) or !len(arguments.major) or !len(arguments.level) or !len(arguments.byear)) {
  16. errorMessage = "All fields are required.";
  17. } else {
  18. // Try inserting the student record into the database
  19. try {
  20. // Insert the student
  21. var insertQuery = new query();
  22. insertQuery.setDatasource("task");
  23. var sql = "INSERT INTO Student (sname, major, level, byear) VALUES (?, ?, ?, ?)";
  24. insertQuery.setSQL(sql);
  25. insertQuery.addParam(name="sname", value=arguments.sname, cfsqltype="CF_SQL_VARCHAR");
  26. insertQuery.addParam(name="major", value=arguments.major, cfsqltype="CF_SQL_VARCHAR");
  27. insertQuery.addParam(name="level", value=arguments.level, cfsqltype="CF_SQL_VARCHAR");
  28. insertQuery.addParam(name="byear", value=arguments.byear, cfsqltype="CF_SQL_VARCHAR");
  29. insertQuery.execute();
  30. } catch (any e) {
  31. // Catch any exceptions and set error message
  32. errorMessage = "Error creating student: " & e.message;
  33. } finally {
  34. // Close the query
  35. if (isQuery(insertQuery)) {
  36. insertQuery.close();
  37. }
  38. }
  39. }
  40. // Output error message, if any
  41. if (len(errorMessage)) {
  42. writeOutput(errorMessage);
  43. } else {
  44. writeOutput("Student created successfully!");
  45. }
  46. }
  47. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...