Advertisement
Guest User

Untitled

a guest
May 28th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.01 KB | None | 0 0
  1. Contact Form.java
  2. Design
  3. https://i.imgur.com/rgdnRSN.png
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. ContactForm.java
  12. Source
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. /*
  20. * To change this license header, choose License Headers in Project Properties.
  21. * To change this template file, choose Tools | Templates
  22. * and open the template in the editor.
  23. */
  24. package mdjavas12018;
  25.  
  26. import java.util.ArrayList;
  27. import javax.swing.JOptionPane;
  28.  
  29. /**
  30. *
  31. * @author sgg321-05
  32. */
  33. public class ContactForm extends javax.swing.JFrame {
  34.  
  35. /**
  36. * Creates new form ContactForm
  37. */
  38. ArrayList<Contact> contacts;
  39. int maxRows;
  40. int curRow;
  41. public ContactForm() {
  42. initComponents();
  43. Contact contact=new Contact();
  44. contacts=contact.getAllContacts();
  45. maxRows=contacts.size()-1;
  46. getFirst();
  47. }
  48.  
  49. /**
  50. * This method is called from within the constructor to initialize the form.
  51. * WARNING: Do NOT modify this code. The content of this method is always
  52. * regenerated by the Form Editor.
  53. */
  54. @SuppressWarnings("unchecked")
  55. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  56. private void initComponents() {
  57.  
  58. jLabel1 = new javax.swing.JLabel();
  59. jLabel2 = new javax.swing.JLabel();
  60. jLabel3 = new javax.swing.JLabel();
  61. jLabel4 = new javax.swing.JLabel();
  62. jLabel5 = new javax.swing.JLabel();
  63. txtID = new javax.swing.JTextField();
  64. txtName = new javax.swing.JTextField();
  65. txtDOB = new javax.swing.JTextField();
  66. txtCity = new javax.swing.JTextField();
  67. btnFirst = new javax.swing.JButton();
  68. btnPrevious = new javax.swing.JButton();
  69. btnNext = new javax.swing.JButton();
  70. btnLast = new javax.swing.JButton();
  71. btnAdd = new javax.swing.JButton();
  72. btnConnect = new javax.swing.JButton();
  73. btnCancel = new javax.swing.JButton();
  74. btnUpdate = new javax.swing.JButton();
  75. btnDelete = new javax.swing.JButton();
  76.  
  77. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  78.  
  79. jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  80. jLabel1.setText("Contact Form");
  81.  
  82. jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
  83. jLabel2.setText("ContactID");
  84.  
  85. jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
  86. jLabel3.setText("Name");
  87.  
  88. jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
  89. jLabel4.setText("DOB");
  90.  
  91. jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
  92. jLabel5.setText("City");
  93.  
  94. txtID.addActionListener(new java.awt.event.ActionListener() {
  95. public void actionPerformed(java.awt.event.ActionEvent evt) {
  96. txtIDActionPerformed(evt);
  97. }
  98. });
  99.  
  100. btnFirst.setText("|<");
  101. btnFirst.addActionListener(new java.awt.event.ActionListener() {
  102. public void actionPerformed(java.awt.event.ActionEvent evt) {
  103. btnFirstActionPerformed(evt);
  104. }
  105. });
  106.  
  107. btnPrevious.setText("<");
  108. btnPrevious.addActionListener(new java.awt.event.ActionListener() {
  109. public void actionPerformed(java.awt.event.ActionEvent evt) {
  110. btnPreviousActionPerformed(evt);
  111. }
  112. });
  113.  
  114. btnNext.setText(">");
  115. btnNext.addActionListener(new java.awt.event.ActionListener() {
  116. public void actionPerformed(java.awt.event.ActionEvent evt) {
  117. btnNextActionPerformed(evt);
  118. }
  119. });
  120.  
  121. btnLast.setText(">|");
  122. btnLast.addActionListener(new java.awt.event.ActionListener() {
  123. public void actionPerformed(java.awt.event.ActionEvent evt) {
  124. btnLastActionPerformed(evt);
  125. }
  126. });
  127.  
  128. btnAdd.setText("Start Add");
  129. btnAdd.addActionListener(new java.awt.event.ActionListener() {
  130. public void actionPerformed(java.awt.event.ActionEvent evt) {
  131. btnAddActionPerformed(evt);
  132. }
  133. });
  134.  
  135. btnConnect.setText("Connect Add");
  136. btnConnect.addActionListener(new java.awt.event.ActionListener() {
  137. public void actionPerformed(java.awt.event.ActionEvent evt) {
  138. btnConnectActionPerformed(evt);
  139. }
  140. });
  141.  
  142. btnCancel.setText("Cancel");
  143. btnCancel.addActionListener(new java.awt.event.ActionListener() {
  144. public void actionPerformed(java.awt.event.ActionEvent evt) {
  145. btnCancelActionPerformed(evt);
  146. }
  147. });
  148.  
  149. btnUpdate.setText("Update");
  150. btnUpdate.setToolTipText("");
  151. btnUpdate.addActionListener(new java.awt.event.ActionListener() {
  152. public void actionPerformed(java.awt.event.ActionEvent evt) {
  153. btnUpdateActionPerformed(evt);
  154. }
  155. });
  156.  
  157. btnDelete.setText("Delete");
  158. btnDelete.addActionListener(new java.awt.event.ActionListener() {
  159. public void actionPerformed(java.awt.event.ActionEvent evt) {
  160. btnDeleteActionPerformed(evt);
  161. }
  162. });
  163.  
  164. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  165. getContentPane().setLayout(layout);
  166. layout.setHorizontalGroup(
  167. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  168. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  169. .addContainerGap(62, Short.MAX_VALUE)
  170. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  171. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  172. .addComponent(jLabel1)
  173. .addGap(33, 33, 33))
  174. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  175. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  176. .addComponent(jLabel5)
  177. .addComponent(jLabel4)
  178. .addComponent(jLabel3)
  179. .addComponent(jLabel2))
  180. .addGap(45, 45, 45)
  181. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  182. .addComponent(txtDOB, javax.swing.GroupLayout.Alignment.LEADING)
  183. .addComponent(txtID, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  184. .addComponent(txtName, javax.swing.GroupLayout.Alignment.LEADING)
  185. .addComponent(txtCity, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
  186. .addGap(55, 55, 55))
  187. .addGroup(layout.createSequentialGroup()
  188. .addGap(39, 39, 39)
  189. .addComponent(btnFirst)
  190. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  191. .addComponent(btnPrevious)
  192. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  193. .addComponent(btnNext)
  194. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  195. .addComponent(btnLast)
  196. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
  197. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  198. .addComponent(btnDelete)
  199. .addComponent(btnAdd)
  200. .addComponent(btnConnect)
  201. .addComponent(btnCancel)
  202. .addComponent(btnUpdate))
  203. .addGap(62, 62, 62))
  204. );
  205. layout.setVerticalGroup(
  206. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  207. .addGroup(layout.createSequentialGroup()
  208. .addContainerGap()
  209. .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
  210. .addGap(32, 32, 32)
  211. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  212. .addComponent(jLabel2)
  213. .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  214. .addComponent(btnAdd))
  215. .addGap(18, 18, 18)
  216. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  217. .addComponent(jLabel3)
  218. .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  219. .addComponent(btnConnect))
  220. .addGap(18, 18, 18)
  221. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  222. .addComponent(jLabel4)
  223. .addComponent(txtDOB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  224. .addComponent(btnCancel))
  225. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  226. .addGroup(layout.createSequentialGroup()
  227. .addGap(18, 18, 18)
  228. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  229. .addComponent(jLabel5)
  230. .addComponent(txtCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  231. .addGroup(layout.createSequentialGroup()
  232. .addGap(9, 9, 9)
  233. .addComponent(btnUpdate)))
  234. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  235. .addComponent(btnDelete)
  236. .addGap(2, 2, 2)
  237. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  238. .addComponent(btnFirst)
  239. .addComponent(btnPrevious)
  240. .addComponent(btnNext)
  241. .addComponent(btnLast))
  242. .addContainerGap(60, Short.MAX_VALUE))
  243. );
  244.  
  245. pack();
  246. }// </editor-fold>
  247.  
  248. private void txtIDActionPerformed(java.awt.event.ActionEvent evt) {
  249. // TODO add your handling code here:
  250. }
  251.  
  252. private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {
  253. // TODO add your handling code here:
  254. getFirst();
  255. }
  256.  
  257. private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
  258. // TODO add your handling code here:
  259. clearTextBoxes();
  260. btnFirst.setEnabled(false);
  261. btnNext.setEnabled(false);
  262. btnPrevious.setEnabled(false);
  263. btnLast.setEnabled(false);
  264. btnAdd.setEnabled(false);
  265. btnConnect.setEnabled(true);
  266. btnCancel.setEnabled(true);
  267. btnUpdate.setEnabled(false);
  268. btnDelete.setEnabled(false);
  269. }
  270.  
  271. private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
  272. // TODO add your handling code here:
  273. try{
  274. Contact contact = contacts.get(curRow);
  275. contact.deleteContact(contact);
  276. JOptionPane.showMessageDialog(null, "Current Contact has been deleted");
  277. contacts.clear();
  278. contacts=contact.getAllContacts();
  279. getFirst();
  280. }catch(Exception e){
  281. JOptionPane.showMessageDialog(null, e.getMessage());
  282. }
  283. }
  284.  
  285. private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {
  286. // TODO add your handling code here:
  287. curRow=maxRows;
  288. fillDetails();
  289. }
  290.  
  291. private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
  292. // TODO add your handling code here:
  293. if(curRow==maxRows){
  294. JOptionPane.showMessageDialog
  295. (null, "This is the Last Record");
  296. }else{
  297. curRow++;
  298. fillDetails();
  299. }
  300. }
  301.  
  302. private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {
  303. // TODO add your handling code here:
  304. if(curRow==0){
  305. JOptionPane.showMessageDialog
  306. (null, "This is the First Record");
  307. }else{
  308. curRow--;
  309. fillDetails();
  310. }
  311. }
  312.  
  313. private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
  314. // TODO add your handling code here:
  315. try{
  316. Contact contact = contacts.get(curRow);
  317. contact.setCname(txtName.getText());
  318. contact.setCcity(txtCity.getText());
  319. contact.setCdob(txtDOB.getText());
  320. contact.updateContact(contact);
  321. JOptionPane.showMessageDialog(null, "Current Contact has been updated");
  322. contacts.clear();
  323. contacts=contact.getAllContacts();
  324. }catch(Exception e){
  325. JOptionPane.showMessageDialog(null, e.getMessage());
  326. }
  327. }
  328.  
  329. private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {
  330. // TODO add your handling code here:
  331. btnFirst.setEnabled(true);
  332. btnNext.setEnabled(true);
  333. btnPrevious.setEnabled(true);
  334. btnLast.setEnabled(true);
  335. btnAdd.setEnabled(true);
  336. btnConnect.setEnabled(false);
  337. btnCancel.setEnabled(true);
  338. btnUpdate.setEnabled(true);
  339. btnDelete.setEnabled(true);
  340. try{
  341. Contact contact = new Contact();
  342. int lastID=contacts.get(maxRows).getCid();
  343. int newID=lastID+1;
  344. contact.setCid(newID);
  345. contact.setCname(txtName.getText());
  346. contact.setCcity(txtCity.getText());
  347. contact.setCdob(txtDOB.getText());
  348. contact.addContact(contact);
  349. JOptionPane.showMessageDialog(null, "New Contact saved");
  350. contacts.clear();
  351. contacts=contact.getAllContacts();
  352. maxRows=contacts.size()-1;
  353. curRow=maxRows;
  354. fillDetails();
  355. }catch(Exception e){
  356. JOptionPane.showMessageDialog(null, e.getMessage());
  357. }
  358. }
  359.  
  360. private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
  361. // TODO add your handling code here:
  362. clearTextBoxes();
  363. btnFirst.setEnabled(true);
  364. btnNext.setEnabled(true);
  365. btnPrevious.setEnabled(true);
  366. btnLast.setEnabled(true);
  367. btnAdd.setEnabled(true);
  368. btnConnect.setEnabled(false);
  369. btnCancel.setEnabled(true);
  370. btnUpdate.setEnabled(true);
  371. btnDelete.setEnabled(true);
  372. getFirst();
  373. }
  374.  
  375. /**
  376. * @param args the command line arguments
  377. */
  378. public static void main(String args[]) {
  379. /* Set the Nimbus look and feel */
  380. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  381. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  382. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  383. */
  384. try {
  385. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  386. if ("Nimbus".equals(info.getName())) {
  387. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  388. break;
  389. }
  390. }
  391. } catch (ClassNotFoundException ex) {
  392. java.util.logging.Logger.getLogger(ContactForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  393. } catch (InstantiationException ex) {
  394. java.util.logging.Logger.getLogger(ContactForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  395. } catch (IllegalAccessException ex) {
  396. java.util.logging.Logger.getLogger(ContactForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  397. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  398. java.util.logging.Logger.getLogger(ContactForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  399. }
  400. //</editor-fold>
  401.  
  402. /* Create and display the form */
  403. java.awt.EventQueue.invokeLater(new Runnable() {
  404. public void run() {
  405. new ContactForm().setVisible(true);
  406. }
  407. });
  408. }
  409.  
  410. // Variables declaration - do not modify
  411. private javax.swing.JButton btnAdd;
  412. private javax.swing.JButton btnCancel;
  413. private javax.swing.JButton btnConnect;
  414. private javax.swing.JButton btnDelete;
  415. private javax.swing.JButton btnFirst;
  416. private javax.swing.JButton btnLast;
  417. private javax.swing.JButton btnNext;
  418. private javax.swing.JButton btnPrevious;
  419. private javax.swing.JButton btnUpdate;
  420. private javax.swing.JLabel jLabel1;
  421. private javax.swing.JLabel jLabel2;
  422. private javax.swing.JLabel jLabel3;
  423. private javax.swing.JLabel jLabel4;
  424. private javax.swing.JLabel jLabel5;
  425. private javax.swing.JTextField txtCity;
  426. private javax.swing.JTextField txtDOB;
  427. private javax.swing.JTextField txtID;
  428. private javax.swing.JTextField txtName;
  429. // End of variables declaration
  430.  
  431. private void getFirst() {
  432. //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  433. curRow=0;
  434. fillDetails();
  435. }
  436.  
  437. private void fillDetails() {
  438. //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  439. Contact contact = contacts.get(curRow);
  440. txtID.setText(String.valueOf(contact.getCid()));
  441. txtName.setText(contact.getCname());
  442. txtCity.setText(contact.getCcity());
  443. txtDOB.setText(contact.getCdob());
  444. }
  445.  
  446. private void clearTextBoxes() {
  447. //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  448. txtName.setText("");
  449. txtDOB.setText("");
  450. txtCity.setText("");
  451. txtID.setText("");
  452. }
  453.  
  454. }
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464. DBConnect.java
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475. /*
  476. * To change this license header, choose License Headers in Project Properties.
  477. * To change this template file, choose Tools | Templates
  478. * and open the template in the editor.
  479. */
  480. package mdjavas12018;
  481.  
  482. import java.sql.Statement;
  483. import java.sql.Connection;
  484. import java.sql.DriverManager;
  485. import java.sql.ResultSet;
  486. import java.sql.SQLException;
  487.  
  488. /**
  489. *
  490. * @author sgg321-05
  491. */
  492. public class DBConnect {
  493.  
  494. /**
  495. * @param args the command line arguments
  496. */
  497. public static void main(String[] args) {
  498. // TODO code application logic here
  499. Connection con=null;
  500. ResultSet rst=null;
  501. Statement stmt=null;
  502. String DBUrl=
  503. "jdbc:derby://localhost:1527/";
  504. String DBName="sample";
  505. String userName="app";
  506. String password="app";
  507. try{
  508. con=DriverManager
  509. .getConnection(DBUrl+DBName,userName,password);
  510. System.out.println("Connected with Sample");
  511. stmt=con.createStatement();
  512. String sql="SELECT * FROM APP.CUSTOMER";
  513. rst=stmt.executeQuery(sql);
  514. while(rst.next()==true){
  515. System.out.println
  516. ("ID: "+rst.getInt("CUSTOMER_ID"));
  517. System.out.println
  518. ("Name: "+rst.getString("Name"));
  519. System.out.println("-----------");
  520. }
  521.  
  522. }
  523. catch(SQLException e){
  524. System.err.println("Error: "+e.getMessage());
  525.  
  526. }
  527. }
  528.  
  529. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement