Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.FlowLayout;
- import java.awt.HeadlessException;
- import javax.swing.AbstractButton;
- import javax.swing.JButton;
- import javax.swing.JDialog;
- import javax.swing.JFileChooser;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import javax.swing.filechooser.FileSystemView;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import javax.swing.JPasswordField;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.Serializable;
- import java.net.URL;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Arrays;
- public class UserNamePage extends JDialog {
- //private final AbstractButton txtPath;
- private final JPanel contentPanel = new JPanel();
- private JTextField jtfUserName;
- private JTextField jtfUploadImage;
- private JTextField jtfFirstName;
- private JTextField jtfLastName;
- private JPasswordField jpwfSelectPassword;
- private JPasswordField jpwfReEnterPassword;
- private JTextField jtfEmail;
- private JFileChooser fileChooser = new JFileChooser();
- private ArrayList<String> allUserNames = new ArrayList<>();
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- try {
- UserNamePage dialog = new UserNamePage();
- dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
- dialog.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Create the dialog.
- */
- public UserNamePage() {
- setTitle("Individual Lawyer Sign Up Screen");
- setBounds(100, 100, 819, 574);
- getContentPane().setLayout(new BorderLayout());
- contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
- getContentPane().add(contentPanel, BorderLayout.CENTER);
- contentPanel.setLayout(null);
- {
- JLabel jlblUserName = new JLabel("User Name");
- jlblUserName.setBounds(159, 205, 84, 16);
- contentPanel.add(jlblUserName);
- }
- {
- JLabel jlblPassword = new JLabel("Select your password");
- jlblPassword.setBounds(159, 234, 133, 16);
- contentPanel.add(jlblPassword);
- }
- {
- JLabel jlblReenterPassword = new JLabel("Re-enter password");
- jlblReenterPassword.setBounds(159, 263, 164, 16);
- contentPanel.add(jlblReenterPassword);
- }
- {
- JLabel jlblUploadImage = new JLabel("Upload Image");
- jlblUploadImage.setBounds(160, 292, 104, 16);
- contentPanel.add(jlblUploadImage);
- }
- jtfUserName = new JTextField();
- jtfUserName.setBounds(335, 199, 171, 22);
- contentPanel.add(jtfUserName);
- jtfUserName.setColumns(10);
- jtfUploadImage = new JTextField(15);
- jtfUploadImage.setText("No File Uploaded");
- jtfUploadImage.setColumns(10);
- jtfUploadImage.setBounds(335, 289, 171, 22);
- contentPanel.add(jtfUploadImage);
- //browse button will fire an action event to allow user to scan their file system for image upload
- JButton jbtnBrowse = new JButton("Browse");
- jbtnBrowse.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- //the file chooser will start at the root drive (C:drive) and display the user's entire file system for file uploaded
- fileChooser = new JFileChooser("C:\", FileSystemView.getFileSystemView());
- fileChooser.setFileFilter(new FileNameExtensionFilter("Image Files", "jpg", "png", "gif"));
- int returnValue = fileChooser.showOpenDialog(jtfUploadImage);
- if(returnValue == JFileChooser.APPROVE_OPTION) {
- String fileName = fileChooser.getSelectedFile().getName();
- String extension = fileName.substring(fileName.lastIndexOf("."));
- //this will only allow the user to upload a jpg, png and gif files
- if(extension.equalsIgnoreCase(".jpg")||extension.equalsIgnoreCase(".png")||extension.equalsIgnoreCase(".gif")) {
- //txtPath = new JTextField(15);
- jtfUploadImage.setText(fileChooser.getSelectedFile().getPath());
- }
- else {
- JOptionPane.showMessageDialog(null, "Please select and image file that is supported(.jpg, .png, .gif)");
- }
- }
- }
- });
- jbtnBrowse.setBounds(539, 288, 97, 25);
- contentPanel.add(jbtnBrowse);
- JLabel jlblFirstName = new JLabel("First Name");
- jlblFirstName.setBounds(159, 115, 84, 16);
- contentPanel.add(jlblFirstName);
- JLabel jlblLastName = new JLabel("Last Name");
- jlblLastName.setBounds(159, 146, 84, 16);
- contentPanel.add(jlblLastName);
- jtfFirstName = new JTextField();
- jtfFirstName.setColumns(10);
- jtfFirstName.setBounds(335, 112, 171, 22);
- contentPanel.add(jtfFirstName);
- jtfLastName = new JTextField();
- jtfLastName.setColumns(10);
- jtfLastName.setBounds(335, 143, 171, 22);
- contentPanel.add(jtfLastName);
- jpwfSelectPassword = new JPasswordField();
- jpwfSelectPassword.setBounds(335, 231, 171, 22);
- contentPanel.add(jpwfSelectPassword);
- jpwfReEnterPassword = new JPasswordField();
- jpwfReEnterPassword.setBounds(335, 260, 171, 22);
- contentPanel.add(jpwfReEnterPassword);
- JButton jbtnCancel = new JButton("Cancel");
- jbtnCancel.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- dispose();
- }
- });
- jbtnCancel.setBounds(539, 479, 97, 25);
- contentPanel.add(jbtnCancel);
- JButton jbtnRegister = new JButton("Register");
- jbtnRegister.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- try {
- SaveUser();//NULL POINTER EXCEPTION HERE!!
- } catch (NumberFormatException | HeadlessException
- | ClassNotFoundException | SQLException | IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- jbtnRegister.setBounds(648, 479, 97, 25);
- contentPanel.add(jbtnRegister);
- JLabel jlblEmail = new JLabel("Email");
- jlblEmail.setBounds(159, 175, 56, 16);
- contentPanel.add(jlblEmail);
- jtfEmail = new JTextField();
- jtfEmail.setBounds(335, 172, 171, 22);
- contentPanel.add(jtfEmail);
- jtfEmail.setColumns(10);
- {
- JPanel buttonPane = new JPanel();
- buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
- getContentPane().add(buttonPane, BorderLayout.SOUTH);
- }
- }
- //public void initalizeDB() {
- //try {
- //Class.forName("com.mysql.jdbc.Driver");
- //establish connection to the database
- //Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/abaapplication" , "root", "root");
- //}
- //catch(Exception e) {
- //e.printStackTrace();
- //}
- //}
- @SuppressWarnings({ "null", "resource" })
- public void SaveUser() throws SQLException, IOException, ClassNotFoundException, NumberFormatException, HeadlessException {
- String imagePath = null;
- String firstName = this.jtfFirstName.getText();
- String lastName = this.jtfLastName.getText();
- String email = this.jtfEmail.getText();
- String userName = this.jtfUserName.getText();
- //passwords need to be stored as char[]
- char[]password = this.jpwfSelectPassword.getPassword();
- char[]reenterpassword = this.jpwfReEnterPassword.getPassword();
- if(firstName.isEmpty()) {
- JOptionPane.showMessageDialog(null, "First Name is a required field");
- }
- else if(lastName.isEmpty()) {
- JOptionPane.showMessageDialog(null, "Last Name is a required field");
- }
- else if(email.isEmpty()) {
- JOptionPane.showMessageDialog(null, "Email is a required field");
- }//this will check if the username box is left empty
- else if(userName.isEmpty()) {
- JOptionPane.showMessageDialog(null, "Please select a user name");
- }//this will check the required length of the user name and decided if it is valid
- else if(userName.length() <= 0){
- JOptionPane.showMessageDialog(null, "This is not a valid user name. Please enter a valid user name");
- }//this will check if the password and re enter password fields are left blank
- else if(password.length <= 0 && reenterpassword.length <= 0) {
- JOptionPane.showMessageDialog(null, "Password fields are required");
- }
- else if((!(Arrays.equals(password, reenterpassword)))) {
- JOptionPane.showMessageDialog(null, "The passwords do not match. Please re-enter password.");
- }//this will compare the 2 arrays of password and reenter password and see if they match
- try{
- byte[] rawBytes = null;
- FileInputStream fileInputStream = null;
- if(imagePath.equals("No File Uploaded")) {//null pointer exeception here!!
- ClassLoader classLoader = this.getClass().getClassLoader();
- URL resources = classLoader.getResource("C:\Users\Justyn Bell\Desktop\abaApplicationImages\blankProfile.jpg");
- imagePath = resources.getFile();
- }
- File fileObject = new File(imagePath);
- fileInputStream = new FileInputStream(fileObject);
- //this will load the jdbc driver for SQL operations
- Class.forName("com.mysql.jdbc.Driver");
- //establish connection to the database
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/abaapplication" , "root", "root");
- PreparedStatement pst = conn.prepareStatement("UserCreate");
- pst.setString(1, firstName);
- pst.setString(2, lastName);
- pst.setString(3, email);
- pst.setString(4, userName);
- //pst.setString(5, password);
- //pst.setArray(6, reenterpassword);
- int imageLength = Integer.parseInt(String.valueOf(fileObject.length()));
- rawBytes = new byte[imageLength];
- fileInputStream.read(rawBytes, 0, imageLength);
- //this will set the image to an integer in the database using prepared statement
- pst.setBytes(7, rawBytes);
- int count = pst.executeUpdate();
- if(count > 0) {
- JOptionPane.showMessageDialog(this, "Image file sucessfully saved");
- }
- else {
- JOptionPane.showMessageDialog(this, "We're sorry, there was an error saving this file");
- }
- }
- catch (HeadlessException| IOException | ClassNotFoundException | NumberFormatException| SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement