Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Scanner;
- public class DbConfig {
- public static Connection getMySqlConnection() {
- Connection mysqlConnection = null;
- try {
- //returns the Class object associated with the class or interface with the given string name
- Class.forName("com.mysql.jdbc.Driver");
- String connectionUrl = "jdbc:mysql://br-cdbr-azure-south-a.cloudapp.net/StudentDB";
- mysqlConnection = DriverManager.getConnection(connectionUrl, "***", "***");
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return mysqlConnection;
- }
- public static void main(String[] args) {
- DbConfig d = new DbConfig();
- d.menu(getMySqlConnection());
- }
- public static void menu(Connection mysqlConnection) {
- Scanner keyboard = new Scanner(System.in);
- System.out.println("Welcome to Student Database! ");
- System.out.println("Please select an option to continue!");
- System.out.println("1.Display all students ");
- System.out.println("2.Add student");
- System.out.println("3.Update student");
- System.out.println("4.Delete student");
- System.out.println("5.Search by major");
- System.out.println("6.Search by GPA");
- System.out.println("7.Search by Adviser");
- int selection = keyboard.nextInt();
- switch(selection) {
- case 1:
- System.out.println("Display all students");
- break;
- case 2:
- System.out.println("Id: ");
- int id = keyboard.nextInt();
- System.out.println("First name: ");
- String firstName = keyboard.next();
- System.out.println("Last name: ");
- String lastName = keyboard.next();
- System.out.println("GPA: ");
- Double gpa = keyboard.nextDouble();
- System.out.println("Major: ");
- String major = keyboard.next();
- System.out.println("Adviser: ");
- String adviser = keyboard.next();
- String query = "INSERT INTO Student (StudentId, FirstName, LastName, GPA, Major, FacultyAdviser) VALUES ('id','firstName','lastName','gpa','major','adviser')";
- PreparedStatement stmt = null;
- try {
- stmt = mysqlConnection.prepareStatement(query);
- stmt.executeUpdate(query);
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- break;
- case 3:
- System.out.println("Update Student");
- break;
- case 4:
- System.out.println("Delete Student");
- break;
- case 5:
- System.out.println("Search by Major");
- break;
- case 6:
- System.out.println("Search by GPA");
- break;
- case 7:
- System.out.println("Search by Adviser");
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement