Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.41 KB | None | 0 0
  1. package src.ui;
  2.  
  3. import src.ui.ImageHandler;
  4.  
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9.  
  10. import javax.swing.event.*;
  11. import java.sql.*;
  12. import javax.swing.*;
  13.  
  14. import java.awt.*;
  15. import java.util.Calendar;
  16. import java.text.SimpleDateFormat;
  17. import java.util.Date;
  18. import java.text.DateFormat;
  19. import java.sql.DriverManager;
  20. import java.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.SQLException;
  23. import java.sql.Statement;
  24. import javax.swing.JOptionPane;
  25.  
  26. public class AddAccount extends JPanel implements ActionListener, DocumentListener, KeyListener {
  27.  
  28. String imagefolder = ImageHandler.returnimagepath();
  29. Image bgImage = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/src/ui/images/bgc.jpg"));
  30. private static final long serialVersionUID = 1L;
  31. public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
  32. String iden[] = {"Student","Others"};
  33. String type[] = {"Account User", "Walkthrough User"};
  34. JLabel userid = new JLabel("USER ID");
  35. JLabel name = new JLabel("FULL NAME");
  36. JLabel pswrd = new JLabel("PASSWORD");
  37. JLabel ident = new JLabel("IDENTIFICATION");
  38. JLabel idno = new JLabel("ID NUMBER");
  39. JLabel Add = new JLabel("ADDRESS");
  40. JLabel acctype = new JLabel("ACCOUNT TYPE");
  41. JTextField userFld = new JTextField(10);
  42. JTextField nameFld = new JTextField(10);
  43. JPasswordField pswrdFld = new JPasswordField(10);
  44. JComboBox identC = new JComboBox(iden);
  45. JComboBox actype = new JComboBox(type);
  46. JTextField idFld = new JTextField(10);
  47. JTextArea addArea = new JTextArea(10, 10);
  48. JButton okButton = new JButton("OK");
  49. JButton exitButton = new JButton("RESET");
  50. JButton b1 = new JButton("SEARCH");
  51. JScrollPane scroll = new JScrollPane(addArea);
  52.  
  53. public static Connection getConnection() {
  54. Connection conn = null;
  55. try {
  56. Class.forName("com.mysql.jdbc.Driver");
  57. conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/cybman", "root", "");
  58. } catch (Exception e) {
  59. //System.out.println(e);
  60. }
  61. return conn;
  62. }
  63. public static Connection getConnection1() {
  64. Connection con = null;
  65. try {
  66. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  67. con = DriverManager.getConnection("jdbc:sqlserver://localhost\BCA1:1433;databaseName=Old_Bhandarkars;user=sa;password=1234");
  68. } catch (Exception e) {
  69. JOptionPane.showMessageDialog(null,"SERVER ERROR");
  70. //System.out.println(e);
  71. }
  72. return con;
  73. }
  74.  
  75. public void changedUpdate(DocumentEvent ev) {
  76. }
  77.  
  78. public void removeUpdate(DocumentEvent ev) {
  79. }
  80.  
  81. public void insertUpdate(DocumentEvent ev) {
  82. }
  83.  
  84. public void paintComponent(Graphics g) {
  85. g.drawImage(bgImage, 0, 0, this);
  86. }
  87.  
  88. public void init() {
  89. setLayout(null);
  90. okButton.addActionListener(this);
  91. b1.addActionListener(this);
  92. exitButton.addActionListener(this);
  93. pswrdFld.addActionListener(this);
  94. pswrdFld.setEchoChar('*');
  95. addArea.getDocument().addDocumentListener(this);
  96. identC.addActionListener(this);
  97. actype.addActionListener(this);
  98. userid.setBounds(270, 20, 120, 20);
  99. userFld.setBounds(470, 20, 140, 20);
  100. b1.setBounds(670, 20, 100, 20);
  101. name.setBounds(270, 70, 120, 20);
  102. nameFld.setBounds(470, 70, 140, 20);
  103. pswrd.setBounds(270, 120, 120, 20);
  104. pswrdFld.setBounds(470, 120, 140, 20);
  105. ident.setBounds(270, 170, 120, 20);
  106. identC.setBounds(470, 170, 140, 20);
  107. idno.setBounds(270, 220, 120, 20);
  108. idFld.setBounds(470, 220, 140, 20);
  109. acctype.setBounds(270, 270, 120, 20);
  110. actype.setBounds(470, 270, 140, 20);
  111. Add.setBounds(270, 320, 320, 20);
  112. scroll.setBounds(470, 320, 140, 60);
  113. okButton.setBounds(270, 410, 100, 20);
  114. okButton.addKeyListener(this);
  115. exitButton.setBounds(510, 410, 100, 20);
  116. }
  117.  
  118. public AddAccount() {
  119. init();
  120. add(userid);
  121. add(userFld);
  122. add(name);
  123. add(nameFld);
  124. add(pswrd);
  125. add(pswrdFld);
  126. add(ident);
  127. add(identC);
  128. add(idno);
  129. add(idFld);
  130. add(acctype);
  131. add(actype);
  132. add(Add);
  133. add(b1);
  134. add(scroll);
  135. add(okButton);
  136. add(exitButton);
  137. }
  138.  
  139. public static String now() {
  140. Calendar cal = Calendar.getInstance();
  141. SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
  142. return sdf.format(cal.getTime());
  143.  
  144. }
  145.  
  146. private String getDateTime() {
  147. DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  148. Date date = new Date();
  149. return dateFormat.format(date);
  150. }
  151. public class Function
  152. {
  153. Connection con=getConnection1();
  154. ResultSet rs;
  155.  
  156. public ResultSet find(String s)
  157. {
  158. int a=Integer.parseInt(s);
  159. try{
  160. Statement sta = con.createStatement();
  161. String s1 = "select * from Members where Fld_Member_Id="+a+"";
  162. rs = sta.executeQuery(s1);
  163.  
  164.  
  165. }
  166. catch(Exception ex){
  167. JOptionPane.showMessageDialog(null,"No connection");
  168. //JOptionPane.showMessageDialog(null,ex.getMessage());
  169. }
  170. //JOptionPane.showMessageDialog(null,rs);
  171.  
  172. return rs;
  173.  
  174. }
  175.  
  176. }
  177.  
  178. public void actionPerformed(ActionEvent e) {
  179. String str = e.getActionCommand();
  180. char[] pass;
  181. String uid;
  182. String nam;
  183. String add;
  184. String id;
  185. String idnum;
  186. String actyp;
  187. int amt = 0;
  188. String datE;
  189. if (str.equals("SEARCH")) {
  190. Function f=new Function();
  191. ResultSet rs=null;
  192. String n1="Fld_First_Name";
  193. String i1="Fld_Member_Id";
  194. String address="Fld_PAddr";
  195. uid = userFld.getText();
  196.  
  197.  
  198. if(uid.equals(""))
  199. {
  200. JFrame msg=new JFrame();
  201. JOptionPane.showMessageDialog(msg, "Please enter the user id", "Error Message", JOptionPane.ERROR_MESSAGE);
  202.  
  203. }
  204. // simple.main();
  205. else
  206. {
  207. //idFld.setText(userFld.getText());
  208. rs=f.find(userFld.getText());
  209. // System.out.println("Fld_First_Name");
  210. try{
  211. if(rs.next()){
  212. // int i = Integer.parseInt(rs.getString("Fld_First_Name"));
  213. // int j = Integer.parseInt(rs.getString("Fld_Member_Id"));
  214. // int k = Integer.parseInt(rs.getString("Fld_PAddr"));
  215. nameFld.setText(rs.getString("Fld_First_Name"));
  216. idFld.setText(rs.getString("Fld_Member_Id"));
  217. addArea.setText(rs.getString("Fld_PAddr"));
  218.  
  219. }
  220. else
  221. {
  222. JOptionPane.showMessageDialog(null,"No data");
  223. }
  224. }
  225. catch(Exception ex)
  226. {
  227. JOptionPane.showMessageDialog(null,ex.getMessage());
  228. }
  229. }
  230. }
  231.  
  232. if (str.equals("OK")) {
  233.  
  234.  
  235. try {
  236.  
  237. uid = userFld.getText();
  238. nam = nameFld.getText();
  239. pass = pswrdFld.getPassword();
  240. add = addArea.getText();
  241. idnum = idFld.getText();
  242. id = (String) identC.getSelectedItem();
  243. actyp = (String) actype.getSelectedItem();
  244. datE = getDateTime();
  245. String pwrd = new String(pass);
  246.  
  247. Connection conn = getConnection();
  248.  
  249. /*ERROR CHECKING*/
  250. boolean success = true;
  251.  
  252. if (uid.equals("")) {
  253. JFrame msg = new JFrame();
  254. JOptionPane.showMessageDialog(msg, "Please enter the user id", "Error Message", JOptionPane.ERROR_MESSAGE);
  255. success = false;
  256. } else if (nam.equals("")) {
  257. JFrame msg = new JFrame();
  258. JOptionPane.showMessageDialog(msg, "Please enter the name", "Error Message", JOptionPane.ERROR_MESSAGE);
  259. success = false;
  260. } else if (pwrd.equals("")) {
  261. JFrame msg = new JFrame();
  262. JOptionPane.showMessageDialog(msg, "Please enter the password", "Error Message", JOptionPane.ERROR_MESSAGE);
  263. success = false;
  264. } else if ((!id.equals("Student")) && (idnum.equals(""))) {
  265. JFrame msg = new JFrame();
  266. JOptionPane.showMessageDialog(msg, "Please enter the identification number", "Error Message", JOptionPane.ERROR_MESSAGE);
  267. success = false;
  268. } else if (add.equals("")) {
  269. JFrame msg = new JFrame();
  270. JOptionPane.showMessageDialog(msg, "Please enter the address", "Error Message", JOptionPane.ERROR_MESSAGE);
  271. success = false;
  272. }
  273.  
  274. /*end of ERROR CHECKING*/
  275.  
  276. if (success == true) {
  277. ///////////ENSURE/////////
  278. JFrame msg = new JFrame();
  279. int choice;
  280. choice = JOptionPane.showConfirmDialog(msg, "Are you sure?", "Select your choice", JOptionPane.OK_CANCEL_OPTION);
  281. /////////////////////////
  282.  
  283. if (choice == JOptionPane.OK_OPTION) {
  284. str = "insert into account(userid,name,password,address,identification,identificationnum,amount,dates,acctype" + ") values (?,?,?,?,?,?,?,?,?)";
  285. PreparedStatement ps = conn.prepareStatement(str);
  286.  
  287. ps.setString(1, uid);
  288. ps.setString(2, nam);
  289. ps.setString(3, pwrd);
  290. ps.setString(4, add);
  291. ps.setString(5, id);
  292. ps.setString(6, idnum);
  293. ps.setInt(7, amt);
  294. ps.setString(8, datE);
  295. ps.setString(9, actyp);
  296.  
  297. int count;
  298. count = ps.executeUpdate();
  299. count++;
  300. count--;
  301.  
  302. //////////////////////////////
  303. ReportPanel.userC.addItem(uid);
  304. ViewAccount.userC.addItem(uid);
  305.  
  306. if (actyp.equals("Account User")) {
  307. RechargePanel.userC.addItem(uid);
  308. }
  309. //////////////////////////////
  310.  
  311. JFrame msg1 = new JFrame();
  312. JOptionPane.showMessageDialog(msg1, "An account has been successfully created", "Information", JOptionPane.INFORMATION_MESSAGE);
  313. } else if (choice == JOptionPane.CANCEL_OPTION) {
  314. JOptionPane.showMessageDialog(msg, "Account Creation Aborted", "Information", JOptionPane.INFORMATION_MESSAGE);
  315. }
  316. }
  317. //conn.close();
  318.  
  319.  
  320. } catch (SQLException e1) {
  321. String error;
  322. JFrame msg = new JFrame();
  323. //error=e1.toString();
  324. error = "Your entry for user id already exists";
  325. JOptionPane.showMessageDialog(msg, error, "Error Message", JOptionPane.ERROR_MESSAGE);
  326. } catch (Exception e1) {
  327. // TODO Auto-generated catch block
  328. String error;
  329. JFrame msg = new JFrame();
  330. error = e1.toString();
  331. JOptionPane.showMessageDialog(msg, error, "Error Message", JOptionPane.ERROR_MESSAGE);
  332.  
  333. }
  334.  
  335. }
  336. ///////////////////////////////////////////////////////////////////////////
  337. if (str.equals("RESET")) {
  338. try {
  339. userFld.setText("");
  340. nameFld.setText("");
  341. pswrdFld.setText("");
  342. addArea.setText("");
  343. idFld.setText("");
  344. } catch (Exception e2) {
  345. String error;
  346. JFrame msg = new JFrame();
  347. error = e2.toString();
  348. JOptionPane.showMessageDialog(msg, error, "Error Message", JOptionPane.ERROR_MESSAGE);
  349. }
  350. }
  351. }
  352. ///////////////////////////////////////////////////////////
  353.  
  354. public void keyPressed(KeyEvent key) {
  355. //
  356. }
  357. /////////////////////////////////////////////////////
  358.  
  359. public void keyReleased(KeyEvent key) {
  360. if (key.getSource() == pswrdFld) {
  361. if (key.getKeyCode() == KeyEvent.VK_ENTER) {
  362. }
  363. }
  364. }
  365.  
  366. /////////////////////////////////////////////////
  367. public void keyTyped(KeyEvent key) {
  368. //
  369. }
  370. //////////////////////////////////////////////
  371.  
  372. }
  373.  
  374. field name data type
  375. ---------------------------------------------------------------------
  376. Fld_First_Name varchar
  377. Fld_Member_Id varchar
  378. Fld_PAddr nvarchar
  379.  
  380.  
  381. SELECT * FROM Members where Fld_Member_Id='14932';
  382.  
  383.  
  384. _____________________________________________________________________________
  385. Fld_Member_Id Fld_First_Name Fld_PAddr
  386. ______________________________________________________________________________
  387. 14932 abc abc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement