public User getUserDetails(String userId) { PreparedStatement ps = null; ResultSet rs = null; User user = null; RegisterService registerService = new RegisterService(); try { ps = connection.prepareStatement("SELECT * FROM user WHERE username=? LIMIT 1"); ps.setString(1, username); rs = ps.executeQuery(); if (rs != null && rs.next()) { user = new User(userId); user.setFirstName(rs.getString("firstname")); user.setLastName(rs.getString("lastname")); user.setEmail(rs.getString("email")); user.setPhoneNumber(rs.getString("primaryPhone")); user.setCellphone(rs.getString("secondaryPhone")); user.setAddress(registerService.createAddressFromID(rs.getInt("addressID"))); user.setCard(registerService.createCardFromID(rs.getInt("creditCardID"))); } } catch (SQLException e) { e.printStackTrace(); } return user; }
// Get All Books public User getUser() { List<User> users = new LinkedList<User>(); // 1. build the query String query = "SELECT * FROM " + TABLE_USER; // 2. get reference to writable DB SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(query, null); // 3. go over each row, build book and add it to list User user = null; if (cursor.moveToFirst()) { do { user = new User(); user.setId(Integer.parseInt(cursor.getString(0))); user.setFirstName(cursor.getString(1)); user.setLastName(cursor.getString(2)); user.setPhoneNumber(cursor.getString(3)); user.setEmergencyNumber(cursor.getString(4)); // Add book to books users.add(user); } while (cursor.moveToNext()); } Log.d("getAllBooks()", users.toString()); // Take only the first user - Ideally only once the profile can be created in the db if (users.isEmpty()) return null; else return users.get(0); // return user }
private void addManager() throws UserNameExistsException { User manager = new User(); manager.setUserName("root"); manager.setPassword(Hash.hashString("root")); manager.setPhoneNumber("0123456789"); manager.setEmail("*****@*****.**"); manager.setAddress("Root street 42."); userDAO.insertUser(manager); }
@RequestMapping(value = "/admin/updateUser.html", method = RequestMethod.POST) public ModelAndView updateUser( String guid, String realName, String phoneNumber, String email, String roleText) { User user = userService.findUserByGuid(guid); user.setRealName(realName); user.setPhoneNumber(phoneNumber); user.setEmail(email); user.setRole(roleText); userService.updateUser(user); return index(); }
/** GetUser */ @WebMethod(operationName = "getUser", action = "getUser") public User getUser( @WebParam(name = "sessionId") String sessionId, @WebParam(name = "login") String login) { Connection con = null; Statement st = null; ResultSet rs = null; User foundUser = null; try { con = DriverManager.getConnection( PostgresConfig.url, PostgresConfig.user, PostgresConfig.password); // if(!checkSessionId(login, sessionId, con)) // return null; st = con.createStatement(); //////////////////////////////////////////////////////////////////////////////// rs = st.executeQuery("SELECT * FROM users WHERE login=\'" + login + "\'"); while (rs.next()) { User user = new User(); user.setFirstName(rs.getString("imie")); user.setLastName(rs.getString("nazwisko")); user.setLogin(rs.getString("login")); user.setMail(rs.getString("mail")); user.setPhoneNumber(rs.getString("telefon")); foundUser = user; break; } System.out.println("Polaczono"); //////////////////////////////////////////////////////////////////////////////// } catch (SQLException e) { System.out.println("Blad polaczenia"); System.out.println(e.getMessage()); System.out.println(e.getErrorCode()); } finally { try { if (rs != null) rs.close(); if (st != null) st.close(); if (con != null) con.close(); } catch (SQLException ex) { System.out.println("Blad zamykania polaczenia"); System.out.println(ex.getMessage()); System.out.println(ex.getErrorCode()); } } return foundUser; }
/** ListUsers */ @WebMethod(operationName = "listUsers", action = "listUsers") public List<User> listUsers() { List<User> users = new LinkedList<User>(); Connection con = null; Statement st = null; ResultSet rs = null; try { con = DriverManager.getConnection( PostgresConfig.url, PostgresConfig.user, PostgresConfig.password); st = con.createStatement(); //////////////////////////////////////////////////////////////////////////////// rs = st.executeQuery("SELECT * FROM users;"); while (rs.next()) { User user = new User(); user.setFirstName(rs.getString("imie")); user.setLastName(rs.getString("nazwisko")); user.setLogin(rs.getString("login")); user.setMail(rs.getString("mail")); user.setPhoneNumber(rs.getString("telefon")); users.add(user); } System.out.println("Polaczono"); //////////////////////////////////////////////////////////////////////////////// } catch (SQLException e) { System.out.println("Blad polaczenia"); System.out.println(e.getMessage()); System.out.println(e.getErrorCode()); } finally { try { if (rs != null) rs.close(); if (st != null) st.close(); if (con != null) con.close(); } catch (SQLException ex) { System.out.println("Blad zamykania polaczenia"); System.out.println(ex.getMessage()); System.out.println(ex.getErrorCode()); } } return users; }
public User getUser(MySqlHelper helper, String userName, String password) { User user = new User(); String query = "select * " + "from login u inner join address a on u.UserId = a.UserId " + "inner join personalinfo p on a.AddressId = p.AddressId " + "where Email=? and Password= ?"; System.out.println(query); System.out.println("u=" + userName + " p=" + password); try { java.sql.PreparedStatement loginPreparedStatement = helper.conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); loginPreparedStatement.setString(1, userName); loginPreparedStatement.setString(2, password); ResultSet rs = loginPreparedStatement.executeQuery(); while (rs.next()) { user.setFirstName(rs.getString("FirstName")); user.setLastName(rs.getString("LastName")); user.setUserType(Integer.toString(rs.getInt("UserTypeId"))); user.setEmail(rs.getString("Email")); Address address = new Address(); address.setAddressLine1(rs.getString("AddressLine1")); address.setAddressLine2(rs.getString("AddressLine2")); address.setCity(rs.getString("City")); address.setState(rs.getString("State")); address.setZipCode(rs.getString("ZipCode")); address.setLattitude(rs.getString("Lattitude")); address.setLongitude(rs.getString("Longitude")); user.setAddress(address); user.setPhoneNumber(rs.getString("PhoneNumber")); } } catch (Exception ex) { ex.printStackTrace(); } return user; }
public static boolean createAdmin() { User myAdmin = new Admin("admin"); myAdmin.setEmail("*****@*****.**"); myAdmin.setFirstName("FirstAdmin"); myAdmin.setLastName("lastAdmin"); myAdmin.setgNumber("00800000"); myAdmin.setPassword("password"); myAdmin.setPhoneNumber("5555555555"); String username = JOptionPane.showInputDialog(null, "Enter admin username"); String password = JOptionPane.showInputDialog(null, "Enter admin Password"); if (username.equals(myAdmin.getUsername()) && password.equals(myAdmin.getPassword())) { return true; } else { JOptionPane.showMessageDialog( null, "Bad password: **Hint: username = '******' :: password = '******'"); return false; } }
public User getUser(String user) throws SQLException { start(); rs = stmt.executeQuery("SELECT * FROM userInfo WHERE username = '******'"); User rtrn = null; if (rs.next()) { rtrn = new User(); rtrn.setUserName(rs.getString(1)); rtrn.setPassWord(rs.getString(2)); rtrn.setFirstName(rs.getString(3)); rtrn.setLastName(rs.getString(4)); rtrn.setEmail(rs.getString(5)); long number = 0; String num = rs.getString(6); if (num != null) number = Long.parseLong(num.replace("-", "").replace("(", "").replace(")", "")); rtrn.setPhoneNumber(number); rtrn.setCity(rs.getString(7)); rtrn.setState(rs.getString(8)); rtrn.setRating(rs.getInt(9)); } close(); return rtrn; }
public String execute() throws Exception { UserCredentials currentUserCredentials = currentUserService.getCurrentUser() != null ? currentUserService.getCurrentUser().getUserCredentials() : null; // --------------------------------------------------------------------- // Prepare values // --------------------------------------------------------------------- if (email != null && email.trim().length() == 0) { email = null; } if (rawPassword != null && rawPassword.trim().length() == 0) { rawPassword = null; } // --------------------------------------------------------------------- // Update userCredentials and user // --------------------------------------------------------------------- Collection<OrganisationUnit> units = selectionTreeManager.getReloadedSelectedOrganisationUnits(); User user = userService.getUser(id); user.setSurname(surname); user.setFirstName(firstName); user.setEmail(email); user.setPhoneNumber(phoneNumber); user.updateOrganisationUnits(new HashSet<OrganisationUnit>(units)); UserCredentials userCredentials = userService.getUserCredentials(user); Set<UserAuthorityGroup> userAuthorityGroups = new HashSet<UserAuthorityGroup>(); for (String id : selectedList) { UserAuthorityGroup group = userService.getUserAuthorityGroup(Integer.parseInt(id)); if (currentUserCredentials != null && currentUserCredentials.canIssue(group)) { userAuthorityGroups.add(group); } } userCredentials.setUserAuthorityGroups(userAuthorityGroups); if (rawPassword != null) { userCredentials.setPassword( passwordManager.encodePassword(userCredentials.getUsername(), rawPassword)); } if (jsonAttributeValues != null) { AttributeUtils.updateAttributeValuesFromJson( user.getAttributeValues(), jsonAttributeValues, attributeService); } userService.updateUserCredentials(userCredentials); userService.updateUser(user); if (currentUserService.getCurrentUser() == user) { selectionManager.setRootOrganisationUnits(units); selectionManager.setSelectedOrganisationUnits(units); selectionTreeManager.setRootOrganisationUnits(units); selectionTreeManager.setSelectedOrganisationUnits(units); } if (units.size() > 0) { selectionManager.setSelectedOrganisationUnits(units); } return SUCCESS; }
private void addUsers() throws IOException { RandomAccessFile f = new RandomAccessFile("randomUser.csv", "r"); String dataString = null; ResourceType type = null; try { type = resourceTypeDAO.getResourceTypeByResourceTypeName("human"); } catch (Exception e) { System.err.println(e); } ArrayList<Group> groups = null; Random random = new Random(1l); try { groups = groupDAO.getAllGroups(); } catch (DAOException e1) { e1.printStackTrace(); } while ((dataString = f.readLine()) != null) { String[] data = dataString.split(";"); User insert = new User(); if (data[0].length() < 45) { insert.setUserName(data[0]); } else { insert.setUserName(data[0].substring(0, 44)); } insert.setPassword(Hash.hashString(data[1])); if (data[2].length() < 15) { insert.setPhoneNumber(data[2]); } else { insert.setPhoneNumber(data[2].substring(0, 14)); } if (data[3].length() < 45) { insert.setEmail(data[3]); } else { insert.setEmail(data[3].substring(0, 44)); } if (data[4].length() < 45) { insert.setAddress(data[4]); } else { insert.setAddress(data[4].substring(0, 44)); } Resource pair = new Resource(); pair.setActive(true); pair.setDescription(""); pair.setResourceTypeID(type.getResourceTypeID()); pair.setResourceName(data[5]); System.out.println(insert); try { userDAO.insertUser(insert); resourceDAO.insertResource(pair); resourceDAO.linkResourceToUser(pair, insert); } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i < 3; ++i) { try { resourceDAO.addResourceToGroup(pair, groups.get(random.nextInt(groups.size()))); } catch (DAOException e) { System.out.println( "Resource already added to group, but don't worry, there are plenty to choose from"); } } } f.close(); }
/** * @param userList * @return Student Register new student by prompting them for user info */ public static Student registerStudentAccount(LinkedList<User> userList) { String username = ""; String password = ""; String first = ""; String last = ""; String GNum = ""; String phoneNum = ""; String email = ""; String address = ""; User aStudent = null; // prompt for username until available username is entered do { username = JOptionPane.showInputDialog("Please enter desired username"); aStudent = validateUsername(username, userList); if (aStudent != null) { JOptionPane.showMessageDialog(null, "This username is already in use!\nPlease try again"); } } while (aStudent != null); // create student object aStudent = new Student(username); // prompt for password until valid entry is given do { password = JOptionPane.showInputDialog("Please enter desired password"); if (!aStudent.setPassword(password)) { JOptionPane.showMessageDialog( null, "Password does not meet requirements. Minimum 8 characters\nTry Again.", "Error", JOptionPane.ERROR_MESSAGE); } } while (!aStudent.setPassword(password)); // prompt for first name until valid entry is made do { first = JOptionPane.showInputDialog("Please enter your first name"); if (!aStudent.setFirstName(first)) { JOptionPane.showMessageDialog(null, "Invalid entry \nPlease try again."); } } while (!aStudent.setFirstName(first)); // prompt for last name until valid entry is made do { last = (JOptionPane.showInputDialog("Please enter your last name")); if (!aStudent.setLastName(last)) { JOptionPane.showMessageDialog(null, "Invalid entry \nPlease try again"); } } while (!aStudent.setLastName(last)); // prompt for G-Number until valid entry is made do { GNum = (JOptionPane.showInputDialog("Please enter your G-number")); if (!aStudent.setgNumber(GNum)) { JOptionPane.showMessageDialog( null, "Invalid entry! Please write your GNumber in this format 00XXXXXX \nPlease try again", "Error", JOptionPane.ERROR_MESSAGE); } } while (!aStudent.setgNumber(GNum)); // prompt for phone number until valid entry is made do { phoneNum = (JOptionPane.showInputDialog("Please enter your phone number")); if (!aStudent.setPhoneNumber(phoneNum)) { JOptionPane.showMessageDialog( null, "Invalid entry. Please write your phone number in XXXXXXXXXX format \nPlease try again", "Error", JOptionPane.ERROR_MESSAGE); } } while (!aStudent.setPhoneNumber(phoneNum)); // prompt for email until valid entry is made do { email = (JOptionPane.showInputDialog("Please enter your Email address")); if (!aStudent.setEmail(email)) { JOptionPane.showMessageDialog( null, "Invalid entry, correct format: [email protected] \nPlease try again", "Error", JOptionPane.ERROR_MESSAGE); } } while (!aStudent.setEmail(email)); // prompt for address until valid entry is made Student nStudent = (Student) aStudent; do { address = (JOptionPane.showInputDialog("Please enter your shipping address")); if (!nStudent.setShippingAddress(address)) { JOptionPane.showMessageDialog(null, "Invalid entry \nPlease try again"); } } while (!nStudent.setShippingAddress(address)); JOptionPane.showMessageDialog(null, "Your account has been created"); try { PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("accounts.txt", true))); pw.println( "\r\n-" + aStudent.getFirstName() + "," + aStudent.getLastName() + "," + aStudent.getgNumber() + "," + aStudent.getPassword() + "," + aStudent.getPhoneNumber() + "," + aStudent.getEmail() + "," + aStudent.getUsername() + "," + nStudent.getShippingAddress()); pw.close(); } catch (IOException e) { e.printStackTrace(); } userList.add(aStudent); return nStudent; }
/** @param userList Populate system with list of students accounts from text file */ public static void populateStudentAccounts(LinkedList<User> userList) { Scanner inputStream = null; // open text file of accounts try { inputStream = new Scanner(new FileInputStream("accounts.txt")); // System.out.println("accounts file read"); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "The file \"accounts.txt\" could not be found"); JOptionPane.showMessageDialog(null, "The system will now exit"); System.exit(0); } // Pull line of text to generate a student while (inputStream.hasNextLine()) { String s1 = inputStream.nextLine(); // locate first name int fNsameStart = (s1.indexOf("-") + 1); int fNameEnd = (s1.indexOf(",")); String fName = s1.substring(fNsameStart, fNameEnd); // locate Last name int lNameStart = (s1.indexOf(",", fNameEnd) + 1); int lNameEnd = (s1.indexOf(",", lNameStart)); String lName = s1.substring(lNameStart, lNameEnd); // locate gNumber int gNumberStart = (s1.indexOf(",", lNameEnd) + 1); int gNumberEnd = (s1.indexOf(",", gNumberStart)); String gNumber = s1.substring(gNumberStart, gNumberEnd); // locate password int passwordStart = (s1.indexOf(",", gNumberEnd) + 1); int passwordEnd = (s1.indexOf(",", passwordStart)); String password = s1.substring(passwordStart, passwordEnd); // locate phone number int phoneNumberStart = (s1.indexOf(",", passwordEnd + 1)); int phoneNumberEnd = (s1.indexOf(",", phoneNumberStart)); String phoneNumber = s1.substring(phoneNumberStart, phoneNumberEnd); // locate email int emailStart = (s1.indexOf(",", phoneNumberEnd) + 1); int emailEnd = (s1.indexOf(",", emailStart)); String email = s1.substring(emailStart, emailEnd); // locate username int usernameStart = (s1.indexOf(",", emailEnd) + 1); int usernameEnd = (s1.indexOf(",", usernameStart)); String username = s1.substring(usernameStart, usernameEnd); // locate address int addressStart = (s1.indexOf(",", usernameEnd) + 1); String address = s1.substring(addressStart); // create student object and populate info User aStudent = new Student(username); aStudent.setFirstName(fName); aStudent.setLastName(lName); aStudent.setgNumber(gNumber); aStudent.setPassword(password); aStudent.setPhoneNumber(phoneNumber); aStudent.setEmail(email); if (aStudent instanceof Student) { ((Student) aStudent).setShippingAddress(address); } // add Student to list userList.add(aStudent); System.out.println(userList.size()); } }