Advertisement
Guest User

Untitled

a guest
Apr 11th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.38 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.FlowLayout;
  3. import java.awt.HeadlessException;
  4.  
  5. import javax.swing.AbstractButton;
  6. import javax.swing.JButton;
  7. import javax.swing.JDialog;
  8. import javax.swing.JFileChooser;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JPanel;
  11. import javax.swing.border.EmptyBorder;
  12. import javax.swing.filechooser.FileNameExtensionFilter;
  13. import javax.swing.filechooser.FileSystemView;
  14. import javax.swing.JLabel;
  15. import javax.swing.JTextField;
  16. import javax.swing.JPasswordField;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.ActionEvent;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.IOException;
  22. import java.io.Serializable;
  23. import java.net.URL;
  24. import java.sql.Connection;
  25. import java.sql.DriverManager;
  26. import java.sql.PreparedStatement;
  27. import java.sql.SQLException;
  28. import java.util.ArrayList;
  29. import java.util.Arrays;
  30.  
  31. public class UserNamePage extends JDialog {
  32.  
  33.  
  34. //private final AbstractButton txtPath;
  35. private final JPanel contentPanel = new JPanel();
  36. private JTextField jtfUserName;
  37. private JTextField jtfUploadImage;
  38. private JTextField jtfFirstName;
  39. private JTextField jtfLastName;
  40. private JPasswordField jpwfSelectPassword;
  41. private JPasswordField jpwfReEnterPassword;
  42. private JTextField jtfEmail;
  43. private JFileChooser fileChooser = new JFileChooser();
  44. private ArrayList<String> allUserNames = new ArrayList<>();
  45.  
  46.  
  47. /**
  48. * Launch the application.
  49. */
  50. public static void main(String[] args) {
  51. try {
  52. UserNamePage dialog = new UserNamePage();
  53. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  54. dialog.setVisible(true);
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }
  59.  
  60. /**
  61. * Create the dialog.
  62. */
  63. public UserNamePage() {
  64. setTitle("Individual Lawyer Sign Up Screen");
  65. setBounds(100, 100, 819, 574);
  66. getContentPane().setLayout(new BorderLayout());
  67. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  68. getContentPane().add(contentPanel, BorderLayout.CENTER);
  69. contentPanel.setLayout(null);
  70. {
  71. JLabel jlblUserName = new JLabel("User Name");
  72. jlblUserName.setBounds(159, 205, 84, 16);
  73. contentPanel.add(jlblUserName);
  74. }
  75. {
  76. JLabel jlblPassword = new JLabel("Select your password");
  77. jlblPassword.setBounds(159, 234, 133, 16);
  78. contentPanel.add(jlblPassword);
  79. }
  80. {
  81. JLabel jlblReenterPassword = new JLabel("Re-enter password");
  82. jlblReenterPassword.setBounds(159, 263, 164, 16);
  83. contentPanel.add(jlblReenterPassword);
  84. }
  85. {
  86. JLabel jlblUploadImage = new JLabel("Upload Image");
  87. jlblUploadImage.setBounds(160, 292, 104, 16);
  88. contentPanel.add(jlblUploadImage);
  89. }
  90.  
  91. jtfUserName = new JTextField();
  92. jtfUserName.setBounds(335, 199, 171, 22);
  93. contentPanel.add(jtfUserName);
  94. jtfUserName.setColumns(10);
  95.  
  96. jtfUploadImage = new JTextField(15);
  97. jtfUploadImage.setText("No File Uploaded");
  98. jtfUploadImage.setColumns(10);
  99. jtfUploadImage.setBounds(335, 289, 171, 22);
  100. contentPanel.add(jtfUploadImage);
  101. //browse button will fire an action event to allow user to scan their file system for image upload
  102. JButton jbtnBrowse = new JButton("Browse");
  103. jbtnBrowse.addActionListener(new ActionListener() {
  104. public void actionPerformed(ActionEvent arg0) {
  105.  
  106. //the file chooser will start at the root drive (C:drive) and display the user's entire file system for file uploaded
  107. fileChooser = new JFileChooser("C:\", FileSystemView.getFileSystemView());
  108. fileChooser.setFileFilter(new FileNameExtensionFilter("Image Files", "jpg", "png", "gif"));
  109.  
  110. int returnValue = fileChooser.showOpenDialog(jtfUploadImage);
  111.  
  112. if(returnValue == JFileChooser.APPROVE_OPTION) {
  113. String fileName = fileChooser.getSelectedFile().getName();
  114. String extension = fileName.substring(fileName.lastIndexOf("."));
  115. //this will only allow the user to upload a jpg, png and gif files
  116. if(extension.equalsIgnoreCase(".jpg")||extension.equalsIgnoreCase(".png")||extension.equalsIgnoreCase(".gif")) {
  117. //txtPath = new JTextField(15);
  118. jtfUploadImage.setText(fileChooser.getSelectedFile().getPath());
  119.  
  120. }
  121. else {
  122. JOptionPane.showMessageDialog(null, "Please select and image file that is supported(.jpg, .png, .gif)");
  123. }
  124.  
  125.  
  126. }
  127. }
  128.  
  129. });
  130. jbtnBrowse.setBounds(539, 288, 97, 25);
  131. contentPanel.add(jbtnBrowse);
  132.  
  133. JLabel jlblFirstName = new JLabel("First Name");
  134. jlblFirstName.setBounds(159, 115, 84, 16);
  135. contentPanel.add(jlblFirstName);
  136.  
  137. JLabel jlblLastName = new JLabel("Last Name");
  138. jlblLastName.setBounds(159, 146, 84, 16);
  139. contentPanel.add(jlblLastName);
  140.  
  141. jtfFirstName = new JTextField();
  142. jtfFirstName.setColumns(10);
  143. jtfFirstName.setBounds(335, 112, 171, 22);
  144. contentPanel.add(jtfFirstName);
  145.  
  146. jtfLastName = new JTextField();
  147. jtfLastName.setColumns(10);
  148. jtfLastName.setBounds(335, 143, 171, 22);
  149. contentPanel.add(jtfLastName);
  150.  
  151. jpwfSelectPassword = new JPasswordField();
  152. jpwfSelectPassword.setBounds(335, 231, 171, 22);
  153. contentPanel.add(jpwfSelectPassword);
  154.  
  155. jpwfReEnterPassword = new JPasswordField();
  156. jpwfReEnterPassword.setBounds(335, 260, 171, 22);
  157. contentPanel.add(jpwfReEnterPassword);
  158.  
  159. JButton jbtnCancel = new JButton("Cancel");
  160. jbtnCancel.addActionListener(new ActionListener() {
  161. public void actionPerformed(ActionEvent arg0) {
  162. dispose();
  163.  
  164. }
  165. });
  166. jbtnCancel.setBounds(539, 479, 97, 25);
  167. contentPanel.add(jbtnCancel);
  168.  
  169. JButton jbtnRegister = new JButton("Register");
  170. jbtnRegister.addActionListener(new ActionListener() {
  171. public void actionPerformed(ActionEvent arg0) {
  172. try {
  173. SaveUser();//NULL POINTER EXCEPTION HERE!!
  174. } catch (NumberFormatException | HeadlessException
  175. | ClassNotFoundException | SQLException | IOException e) {
  176. // TODO Auto-generated catch block
  177. e.printStackTrace();
  178. }
  179. }
  180. });
  181. jbtnRegister.setBounds(648, 479, 97, 25);
  182. contentPanel.add(jbtnRegister);
  183.  
  184. JLabel jlblEmail = new JLabel("Email");
  185. jlblEmail.setBounds(159, 175, 56, 16);
  186. contentPanel.add(jlblEmail);
  187.  
  188. jtfEmail = new JTextField();
  189. jtfEmail.setBounds(335, 172, 171, 22);
  190. contentPanel.add(jtfEmail);
  191. jtfEmail.setColumns(10);
  192. {
  193. JPanel buttonPane = new JPanel();
  194. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  195. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  196. }
  197. }
  198.  
  199. //public void initalizeDB() {
  200. //try {
  201. //Class.forName("com.mysql.jdbc.Driver");
  202. //establish connection to the database
  203. //Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/abaapplication" , "root", "root");
  204.  
  205. //}
  206. //catch(Exception e) {
  207. //e.printStackTrace();
  208. //}
  209. //}
  210.  
  211. @SuppressWarnings({ "null", "resource" })
  212. public void SaveUser() throws SQLException, IOException, ClassNotFoundException, NumberFormatException, HeadlessException {
  213. String imagePath = null;
  214. String firstName = this.jtfFirstName.getText();
  215. String lastName = this.jtfLastName.getText();
  216. String email = this.jtfEmail.getText();
  217. String userName = this.jtfUserName.getText();
  218. //passwords need to be stored as char[]
  219. char[]password = this.jpwfSelectPassword.getPassword();
  220. char[]reenterpassword = this.jpwfReEnterPassword.getPassword();
  221.  
  222. if(firstName.isEmpty()) {
  223. JOptionPane.showMessageDialog(null, "First Name is a required field");
  224. }
  225. else if(lastName.isEmpty()) {
  226. JOptionPane.showMessageDialog(null, "Last Name is a required field");
  227. }
  228. else if(email.isEmpty()) {
  229. JOptionPane.showMessageDialog(null, "Email is a required field");
  230. }//this will check if the username box is left empty
  231. else if(userName.isEmpty()) {
  232. JOptionPane.showMessageDialog(null, "Please select a user name");
  233. }//this will check the required length of the user name and decided if it is valid
  234. else if(userName.length() <= 0){
  235. JOptionPane.showMessageDialog(null, "This is not a valid user name. Please enter a valid user name");
  236. }//this will check if the password and re enter password fields are left blank
  237. else if(password.length <= 0 && reenterpassword.length <= 0) {
  238. JOptionPane.showMessageDialog(null, "Password fields are required");
  239. }
  240. else if((!(Arrays.equals(password, reenterpassword)))) {
  241. JOptionPane.showMessageDialog(null, "The passwords do not match. Please re-enter password.");
  242. }//this will compare the 2 arrays of password and reenter password and see if they match
  243.  
  244.  
  245. try{
  246. byte[] rawBytes = null;
  247. FileInputStream fileInputStream = null;
  248.  
  249. if(imagePath.equals("No File Uploaded")) {//null pointer exeception here!!
  250. ClassLoader classLoader = this.getClass().getClassLoader();
  251. URL resources = classLoader.getResource("C:\Users\Justyn Bell\Desktop\abaApplicationImages\blankProfile.jpg");
  252. imagePath = resources.getFile();
  253. }
  254.  
  255. File fileObject = new File(imagePath);
  256. fileInputStream = new FileInputStream(fileObject);
  257. //this will load the jdbc driver for SQL operations
  258. Class.forName("com.mysql.jdbc.Driver");
  259. //establish connection to the database
  260. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/abaapplication" , "root", "root");
  261.  
  262.  
  263. PreparedStatement pst = conn.prepareStatement("UserCreate");
  264.  
  265.  
  266. pst.setString(1, firstName);
  267. pst.setString(2, lastName);
  268. pst.setString(3, email);
  269. pst.setString(4, userName);
  270. //pst.setString(5, password);
  271. //pst.setArray(6, reenterpassword);
  272.  
  273.  
  274. int imageLength = Integer.parseInt(String.valueOf(fileObject.length()));
  275. rawBytes = new byte[imageLength];
  276. fileInputStream.read(rawBytes, 0, imageLength);
  277. //this will set the image to an integer in the database using prepared statement
  278. pst.setBytes(7, rawBytes);
  279.  
  280. int count = pst.executeUpdate();
  281.  
  282. if(count > 0) {
  283. JOptionPane.showMessageDialog(this, "Image file sucessfully saved");
  284. }
  285. else {
  286. JOptionPane.showMessageDialog(this, "We're sorry, there was an error saving this file");
  287. }
  288. }
  289.  
  290.  
  291. catch (HeadlessException| IOException | ClassNotFoundException | NumberFormatException| SQLException e) {
  292. e.printStackTrace();
  293. }
  294.  
  295.  
  296. }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement