Advertisement
Guest User

Untitled

a guest
May 7th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. package Dbweek3;
  2.  
  3. import javax.swing.SwingUtilities;
  4. import java.awt.BorderLayout;
  5. import javax.swing.JPanel;
  6. import javax.swing.JFrame;
  7. import java.awt.GridLayout;
  8. import javax.swing.JLabel;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JTextField;
  11. import javax.swing.SwingConstants;
  12. import java.sql.*;
  13.  
  14. import javax.swing.JButton;
  15. public class Data extends JFrame {
  16.  
  17. private static final long serialVersionUID = 1L;
  18. private JPanel jContentPane = null;
  19. private JPanel jPanel = null;
  20. private JLabel jLabel = null;
  21. private JLabel jLabel1 = null;
  22. private JLabel jLabel2 = null;
  23. private JLabel jLabel3 = null;
  24. private JTextField txtCustNo = null;
  25. private JTextField txtName = null;
  26. private JTextField txtPhone = null;
  27. private JTextField txtBDay = null;
  28.  
  29. //Programmer declared variables
  30. static Connection connection;
  31. Statement stmt; // @jve:decl-index=0:
  32. String sqlQuery; // @jve:decl-index=0:
  33. ResultSet rs; // @jve:decl-index=0:
  34. private JPanel jPanel1 = null;
  35. private JButton btnFirst = null;
  36. private JButton btnNext = null;
  37. private JButton btnPrevious = null;
  38. private JButton btnLast = null;
  39.  
  40.  
  41. // Some constants
  42. private static final int RESULT_FIRST = 0;
  43. private static final int RESULT_LAST = 1;
  44. private static final int RESULT_NEXT = 2;
  45. private static final int RESULT_PREVIOUS = 3;
  46.  
  47.  
  48. /**
  49. * This method initializes jPanel
  50. *
  51. * @return javax.swing.JPanel
  52. */
  53. private JPanel getJPanel() {
  54. if (jPanel == null) {
  55. jLabel3 = new JLabel();
  56. jLabel3.setText("Birthday");
  57. jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
  58. jLabel2 = new JLabel();
  59. jLabel2.setText("Phone Number");
  60. jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
  61. jLabel1 = new JLabel();
  62. jLabel1.setText("Name");
  63. jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
  64. jLabel = new JLabel();
  65. jLabel.setText("Customer No.");
  66. jLabel.setHorizontalAlignment(SwingConstants.RIGHT);
  67. jLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
  68. GridLayout gridLayout = new GridLayout();
  69. gridLayout.setRows(4);
  70. gridLayout.setColumns(2);
  71. jPanel = new JPanel();
  72. jPanel.setLayout(gridLayout);
  73. jPanel.add(jLabel, null);
  74. jPanel.add(getTxtCustNo(), null);
  75. jPanel.add(jLabel1, null);
  76. jPanel.add(getTxtName(), null);
  77. jPanel.add(jLabel2, null);
  78. jPanel.add(getTxtPhone(), null);
  79. jPanel.add(jLabel3, null);
  80. jPanel.add(getTxtBDay(), null);
  81. }
  82. return jPanel;
  83. }
  84.  
  85.  
  86. /**
  87. * This method initializes txtCustNo
  88. *
  89. * @return javax.swing.JTextField
  90. */
  91. private JTextField getTxtCustNo() {
  92. if (txtCustNo == null) {
  93. txtCustNo = new JTextField();
  94. }
  95. return txtCustNo;
  96. }
  97.  
  98. /**
  99. * This method initializes txtName
  100. *
  101. * @return javax.swing.JTextField
  102. */
  103. private JTextField getTxtName() {
  104. if (txtName == null) {
  105. txtName = new JTextField();
  106. }
  107. return txtName;
  108. }
  109.  
  110. /**
  111. * This method initializes txtPhone
  112. *
  113. * @return javax.swing.JTextField
  114. */
  115. private JTextField getTxtPhone() {
  116. if (txtPhone == null) {
  117. txtPhone = new JTextField();
  118. }
  119. return txtPhone;
  120. }
  121.  
  122. /**
  123. * This method initializes txtBDay
  124. *
  125. * @return javax.swing.JTextField
  126. */
  127. private JTextField getTxtBDay() {
  128. if (txtBDay == null) {
  129. txtBDay = new JTextField();
  130. }
  131. return txtBDay;
  132. }
  133.  
  134. /**
  135. * This method initializes jPanel1
  136. *
  137. * @return javax.swing.JPanel
  138. */
  139. private JPanel getJPanel1() {
  140. if (jPanel1 == null) {
  141. GridLayout gridLayout1 = new GridLayout();
  142. gridLayout1.setRows(1);
  143. gridLayout1.setColumns(4);
  144. jPanel1 = new JPanel();
  145. jPanel1.setLayout(gridLayout1);
  146. jPanel1.add(getBtnFirst(), null);
  147. jPanel1.add(getBtnNext(), null);
  148. jPanel1.add(getBtnPrevious(), null);
  149. jPanel1.add(getBtnLast(), null);
  150. }
  151. return jPanel1;
  152. }
  153.  
  154.  
  155. /**
  156. * This method initializes btnFirst
  157. *
  158. * @return javax.swing.JButton
  159. */
  160. private JButton getBtnFirst() {
  161. if (btnFirst == null) {
  162. btnFirst = new JButton();
  163. btnFirst.setText("First");
  164. btnFirst.addActionListener(new java.awt.event.ActionListener() {
  165. public void actionPerformed(java.awt.event.ActionEvent e) {
  166. showResult(Data.RESULT_FIRST);
  167. }
  168. });
  169. }
  170. return btnFirst;
  171. }
  172.  
  173.  
  174. /**
  175. * This method initializes btnNext
  176. *
  177. * @return javax.swing.JButton
  178. */
  179. private JButton getBtnNext() {
  180. if (btnNext == null) {
  181. btnNext = new JButton();
  182. btnNext.setText("Next");
  183. btnNext.addActionListener(new java.awt.event.ActionListener() {
  184. public void actionPerformed(java.awt.event.ActionEvent e) {
  185. showResult(Data.RESULT_NEXT);
  186. }
  187. });
  188.  
  189. }
  190. return btnNext;
  191. }
  192.  
  193.  
  194. /**
  195. * This method initializes btnPrevious
  196. *
  197. * @return javax.swing.JButton
  198. */
  199. private JButton getBtnPrevious() {
  200. if (btnPrevious == null) {
  201. btnPrevious = new JButton();
  202. btnPrevious.setText("Previous");
  203. btnPrevious.addActionListener(new java.awt.event.ActionListener() {
  204. public void actionPerformed(java.awt.event.ActionEvent e) {
  205. showResult(Data.RESULT_PREVIOUS);
  206. }
  207. });
  208. }
  209. return btnPrevious;
  210. }
  211.  
  212.  
  213. /**
  214. * This method initializes btnLast
  215. *
  216. * @return javax.swing.JButton
  217. */
  218. private JButton getBtnLast() {
  219. if (btnLast == null) {
  220. btnLast = new JButton();
  221. btnLast.setText("Last");
  222. btnLast.addActionListener(new java.awt.event.ActionListener() {
  223. public void actionPerformed(java.awt.event.ActionEvent e) {
  224. try{
  225. showResult(Data.RESULT_LAST);
  226. }
  227. catch(Exception ex){
  228. }
  229. }
  230. });
  231. }
  232. return btnLast;
  233. }
  234.  
  235.  
  236. /**
  237. * @param args
  238. */
  239. public static void main(String[] args) {
  240. SwingUtilities.invokeLater(new Runnable() {
  241. public void run() {
  242. Data thisClass = new Data();
  243. thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  244. thisClass.setVisible(true);
  245. }
  246. });
  247. }
  248.  
  249. /**
  250. * This is the default constructor
  251. */
  252. public Data() {
  253. super();
  254. initialize();
  255. getDBReady();
  256. }
  257.  
  258. /**
  259. * This method initializes this
  260. *
  261. * @return void
  262. */
  263. private void initialize() {
  264. this.setSize(385, 309);
  265. this.setContentPane(getJContentPane());
  266. this.setTitle("Customer Database");
  267. }
  268.  
  269. /**
  270. * This method initializes jContentPane
  271. *
  272. * @return javax.swing.JPanel
  273. */
  274. private JPanel getJContentPane() {
  275. if (jContentPane == null) {
  276. jContentPane = new JPanel();
  277. jContentPane.setLayout(new BorderLayout());
  278. jContentPane.add(getJPanel(), BorderLayout.NORTH);
  279. jContentPane.add(getJPanel1(), BorderLayout.SOUTH);
  280. }
  281. return jContentPane;
  282. }
  283.  
  284.  
  285. // @jve:decl-index=0:
  286.  
  287.  
  288. /**
  289. * Show the result for the current pos in the cursor
  290. *
  291. * @param pos - RESULT INFO
  292. */
  293. private void showResult(int pos) {
  294. try {
  295. // Check if its instantiated
  296. if(rs != null) {
  297.  
  298. // Temp variable
  299. boolean ret = false;
  300.  
  301. // LEts see how to move cursor
  302. switch(pos) {
  303. case RESULT_FIRST:
  304. ret = rs.first();
  305. break;
  306.  
  307. case RESULT_LAST:
  308. ret = rs.last();
  309. break;
  310.  
  311. case RESULT_NEXT:
  312. if(rs.isLast()) {
  313. ret = rs.first();
  314. }
  315. else {
  316. ret = rs.next();
  317. }
  318. break;
  319.  
  320. case RESULT_PREVIOUS:
  321. if(rs.isFirst()) {
  322. ret = rs.last();
  323. }
  324. else {
  325. ret = rs.previous();
  326. }
  327. break;
  328. }
  329.  
  330. // Fill in UI
  331. if(ret) {
  332. txtCustNo.setText(rs.getString(1));
  333. txtName.setText(rs.getString(2));
  334. txtPhone.setText(rs.getString(3));
  335. txtBDay.setText((rs.getDate(4)).toString());
  336. }
  337. }
  338. }
  339. catch(Exception ex) {
  340. JOptionPane.showMessageDialog(this, ex.getMessage(), "Error Executing",JOptionPane.ERROR_MESSAGE);;
  341. }
  342. }
  343.  
  344. public void getDBReady() {
  345. try {
  346. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  347. connection = DriverManager.getConnection("jdbc:odbc:CIS355");
  348. //connection = DriverManager.getConnection("jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb);" +
  349. // "DBQ=C:\\CIS435\\CIS435_lab1_customers.mdb");
  350. stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  351. ResultSet.CONCUR_UPDATABLE);
  352. sqlQuery = "select Cust_no, Name, Phone, Bday from customers";
  353. rs = stmt.executeQuery(sqlQuery);
  354.  
  355. // Get the first result and show it to the screen
  356. showResult(Data.RESULT_NEXT);
  357. }
  358. catch(Exception ex){
  359. JOptionPane.showMessageDialog(this, ex.getMessage(), "Error Initializing database",JOptionPane.ERROR_MESSAGE);;
  360. }
  361. }
  362.  
  363.  
  364. } // @jve:decl-index=0:visual-constraint="10,10"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement