Advertisement
Guest User

Untitled

a guest
Feb 28th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4. public class DbConfig {
  5.  
  6. public static Connection getMySqlConnection() {
  7.  
  8. Connection mysqlConnection = null;
  9. try {
  10. //returns the Class object associated with the class or interface with the given string name
  11. Class.forName("com.mysql.jdbc.Driver");
  12. String connectionUrl = "jdbc:mysql://br-cdbr-azure-south-a.cloudapp.net/StudentDB";
  13. mysqlConnection = DriverManager.getConnection(connectionUrl, "***", "***");
  14. } catch (Exception ex) {
  15. ex.printStackTrace();
  16. }
  17. return mysqlConnection;
  18.  
  19. }
  20. public static void main(String[] args) {
  21. DbConfig d = new DbConfig();
  22. d.menu(getMySqlConnection());
  23. }
  24. public static void menu(Connection mysqlConnection) {
  25. Scanner keyboard = new Scanner(System.in);
  26. System.out.println("Welcome to Student Database! ");
  27. System.out.println("Please select an option to continue!");
  28. System.out.println("1.Display all students ");
  29. System.out.println("2.Add student");
  30. System.out.println("3.Update student");
  31. System.out.println("4.Delete student");
  32. System.out.println("5.Search by major");
  33. System.out.println("6.Search by GPA");
  34. System.out.println("7.Search by Adviser");
  35. int selection = keyboard.nextInt();
  36. switch(selection) {
  37. case 1:
  38. System.out.println("Display all students");
  39. break;
  40. case 2:
  41. System.out.println("Id: ");
  42. int id = keyboard.nextInt();
  43. System.out.println("First name: ");
  44. String firstName = keyboard.next();
  45. System.out.println("Last name: ");
  46. String lastName = keyboard.next();
  47. System.out.println("GPA: ");
  48. Double gpa = keyboard.nextDouble();
  49. System.out.println("Major: ");
  50. String major = keyboard.next();
  51. System.out.println("Adviser: ");
  52. String adviser = keyboard.next();
  53.  
  54. String query = "INSERT INTO Student (StudentId, FirstName, LastName, GPA, Major, FacultyAdviser) VALUES ('id','firstName','lastName','gpa','major','adviser')";
  55. PreparedStatement stmt = null;
  56. try {
  57. stmt = mysqlConnection.prepareStatement(query);
  58. stmt.executeUpdate(query);
  59. }
  60. catch (SQLException e) {
  61. e.printStackTrace();
  62. }
  63. break;
  64. case 3:
  65. System.out.println("Update Student");
  66. break;
  67. case 4:
  68. System.out.println("Delete Student");
  69. break;
  70. case 5:
  71. System.out.println("Search by Major");
  72. break;
  73. case 6:
  74. System.out.println("Search by GPA");
  75. break;
  76. case 7:
  77. System.out.println("Search by Adviser");
  78. break;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement