Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. package quizapp.model;
  2.  
  3. import java.sql.*;
  4. import quizpp.util.DatabaseManager;
  5.  
  6. public class AddAccount {
  7.    
  8.     private DatabaseManager db = new DatabaseManager();
  9.  
  10.     public boolean insertStaffAccount(String staffID, String staffFN, String staffLN, String staffE) {
  11.        
  12.         String insertStaffTableSQL = "INSERT INTO 16agileteam1db.staff "
  13.                 + "VALUES(" + staffID + ");";
  14.         String insertProfileTableSQL = "INSERT INTO 16agileteam1db.profile_details(staff_id_number, first_name, last_name, email, soul) "
  15.                 + "VALUES(" + staffID + ",'" + staffFN + "','" + staffLN + "','" + staffE + "','staff');";
  16.  
  17.         try(Connection connection = db.getConnection(); Statement statement = connection.createStatement()) {
  18.            
  19.             statement.addBatch(insertStaffTableSQL);
  20.             statement.addBatch(insertProfileTableSQL);
  21.             statement.executeBatch();
  22.            
  23.         } catch (ClassNotFoundException | SQLException e) {
  24.             e.printStackTrace();
  25.         }
  26.        
  27.         return false;
  28.     }
  29.  
  30.     public boolean insertStudentAccount(String matricN, String studentFN, String studentLN, String studentE) {
  31.         String driverName = "com.mysql.jdbc.Driver";
  32.         String connectionUrl = "jdbc:mysql://silva.computing.dundee.ac.uk:3306/";
  33.         String dbName = "16agileteam1db";
  34.         String userID = "16agileteam1";
  35.         String password = "8320.at1.0238";
  36.        
  37.         try {
  38.             Class.forName(driverName);
  39.         } catch (ClassNotFoundException e) {
  40.         }
  41.  
  42.         Connection connection = null;
  43.         Statement statement = null;
  44.         Statement statement2 = null;
  45.        
  46.         String insertStudentTableSQL = "INSERT INTO 16agileteam1db.student (matriculation_number) "
  47.                 + "VALUES(" + matricN + ");";
  48.         String insertProfileTableSQL = "INSERT INTO 16agileteam1db.profile_details(matriculation_number, first_name, last_name, email, soul) "
  49.                 + "VALUES(" + matricN + ",'" + studentFN + "','" + studentLN + "','" + studentE + "','student');";
  50.         try {
  51.             connection = DriverManager.getConnection(connectionUrl + dbName, userID, password);
  52.             statement = connection.createStatement();
  53.             statement2 = connection.createStatement();
  54.             statement.execute(insertStudentTableSQL);
  55.             statement2.execute(insertProfileTableSQL);
  56.             connection.close();
  57.             return true;
  58.         } catch (Exception e) {
  59.             e.getMessage();
  60.         }
  61.         return false;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement