Guest User

Untitled

a guest
Jul 22nd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.29 KB | None | 0 0
  1. public class supplier extends javax.swing.JFrame {
  2.  
  3. ResultSet rs;
  4. Statement stmt;
  5. Connection con;PreparedStatement ptst;
  6. int toggle=0;//1 for inserting and 2 for deleting
  7. String host_name="jdbc:derby://localhost:1527/Supplier";
  8. String user="supplier";
  9. String pass="supp123";
  10. String sql="SELECT * FROM SUPPLIER.SUPPLIER_TB";
  11. public supplier() {
  12. initComponents();
  13. doconnect();
  14. }
  15.  
  16.  
  17. public void doconnect(){
  18.  
  19. try{
  20.  
  21.  
  22. Connection con=DriverManager.getConnection(host_name,user, pass);
  23.  
  24. Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  25.  
  26.  
  27.  
  28. ResultSet rs=stmt.executeQuery(sql);
  29.  
  30. btnSave.setEnabled(false);
  31. btnCancel.setEnabled(false);
  32.  
  33. rs.next();
  34.  
  35. int i=rs.getInt(1);
  36. String n=rs.getString(2);
  37. String c=rs.getString(3);
  38. String p=rs.getString(5);
  39.  
  40. txtID.setText(Integer.toString(i));
  41. txtName.setText(n);
  42. txtCountry.setText(c);
  43. txtProd.setText(p);
  44.  
  45. btnNext.addActionListener(new ActionListener(){
  46. public void actionPerformed(ActionEvent e){
  47. try{
  48. if(rs.next()){
  49.  
  50. int i1=rs.getInt(1);
  51. String n1=rs.getString(2);
  52. String c1=rs.getString(3);
  53. String p1=rs.getString(5);
  54.  
  55. txtID.setText(Integer.toString(i1));
  56. txtName.setText(n1);
  57. txtCountry.setText(c1);
  58. txtProd.setText(p1);}
  59. else
  60. {rs.previous(); JOptionPane.showMessageDialog(null,"End of this file");}
  61. }
  62. catch(SQLException err){
  63. JOptionPane.showMessageDialog(null,err.getMessage());
  64. }
  65. }
  66. });
  67.  
  68. //THE PREVIOUS BUTTON
  69.  
  70. btnPrev.addActionListener(new ActionListener(){
  71. public void actionPerformed(ActionEvent e){
  72. try{
  73. if(rs.previous()){
  74.  
  75. int i1=rs.getInt(1);
  76. String n1=rs.getString(2);
  77. String c1=rs.getString(3);
  78. String p1=rs.getString(5);
  79.  
  80. txtID.setText(Integer.toString(i1));
  81. txtName.setText(n1);
  82. txtCountry.setText(c1);
  83. txtProd.setText(p1);}
  84. else
  85. rs.next();
  86. }
  87. catch(SQLException err){
  88. JOptionPane.showMessageDialog(null,err.getMessage());
  89. }
  90. }
  91. });
  92. //THE SEARCH BUTTON
  93.  
  94. btnSearch.addActionListener(new ActionListener() {
  95. @Override
  96. public void actionPerformed(ActionEvent e) {
  97. try{
  98. if(txtID.getText()!=null)
  99. {
  100. int i1=Integer.valueOf(txtID.getText());
  101.  
  102. rs.absolute(i1);
  103.  
  104. String n1=rs.getString(2);
  105. String c1=rs.getString(3);
  106. String p1=rs.getString(5);
  107.  
  108. txtID.setText(Integer.toString(i1));
  109. txtName.setText(n1);
  110. txtCountry.setText(c1);
  111. txtProd.setText(p1);
  112. }
  113.  
  114. else if(txtName.getText()!=null)
  115. {
  116. String n1=txtName.getText();
  117. txtName.setText("Hello");
  118.  
  119. rs.first();
  120. do{
  121. if(n1.equals(rs.getString(2)))
  122. {
  123. break;
  124. }
  125.  
  126. }while(rs.next());
  127.  
  128. int i1=rs.getInt(1);
  129. String c1=rs.getString(3);
  130. String p1=rs.getString(5);
  131.  
  132. txtID.setText(Integer.toString(i1));
  133. txtName.setText(n1);
  134. txtCountry.setText(c1);
  135. txtProd.setText(p1);
  136. }
  137. else
  138. JOptionPane.showMessageDialog(null,"Not Found");
  139. }
  140. catch(SQLException err){
  141. JOptionPane.showMessageDialog(null,err.getMessage());
  142. }
  143. }
  144. });
  145.  
  146. //THE ADD BUTTON
  147.  
  148. btnInsert.addActionListener(new ActionListener() {
  149. @Override
  150. public void actionPerformed(ActionEvent e) {
  151.  
  152. txtID.setText("");
  153. txtName.setText("");
  154. txtCountry.setText("");
  155. txtProd.setText("");
  156. btnNext.setEnabled(false);
  157. btnPrev.setEnabled(false);
  158. btnClear.setEnabled(false);
  159. btnSearch.setEnabled(false);
  160. btnFirst.setEnabled(false);
  161. btnLast.setEnabled(false);
  162. btnDelete.setEnabled(false);
  163. btnInsert.setEnabled(false);
  164.  
  165. btnSave.setEnabled(true);
  166. btnCancel.setEnabled(true);
  167.  
  168. }
  169. });
  170.  
  171. //THE DELETE BUTTON
  172.  
  173. btnDelete.addActionListener(new ActionListener() {
  174. @Override
  175. public void actionPerformed(ActionEvent e) {
  176.  
  177. }
  178. });
  179.  
  180. // THE SAVE BUTTON
  181.  
  182. btnSave.addActionListener(new ActionListener() {
  183. @Override
  184. public void actionPerformed(ActionEvent e) {
  185. try{
  186. ptst=con.prepareStatement("INSERT INTO SUPPLIER_TB (SUPP_ID,SUPP_NAME,SUPP_COUNTRY,SUPP_PROD) VALUES (?,?,?,?)");
  187.  
  188. int i1=Integer.valueOf(txtID.getText());
  189. String n1=txtName.getText();
  190. String c1=txtCountry.getText();
  191. String p1=txtProd.getText();
  192.  
  193. ptst.setInt(1,i1);
  194. ptst.setString(2,n1);
  195. ptst.setString(3,c1);
  196. ptst.setString(4,p1);
  197.  
  198. int a=ptst.executeUpdate();
  199.  
  200. ResultSet rs=stmt.executeQuery(sql);
  201. rs.next();
  202.  
  203. int i2=rs.getInt(1);
  204. String n2=rs.getString(2);
  205. String c2=rs.getString(3);
  206. String p2=rs.getString(5);
  207.  
  208. txtID.setText(Integer.toString(i2));
  209. txtName.setText(n2);
  210. txtCountry.setText(c2);
  211. txtProd.setText(p2);
  212.  
  213. btnInsert.setEnabled(true);
  214. btnEdit.setEnabled(true);
  215. btnDelete.setEnabled(true);
  216. btnFirst.setEnabled(true);
  217. btnLast.setEnabled(true);
  218. btnNext.setEnabled(true);
  219. btnPrev.setEnabled(true);
  220.  
  221. btnSave.setEnabled(false);
  222. btnCancel.setEnabled(false);
  223.  
  224.  
  225. }
  226.  
  227. catch(SQLException err){
  228. JOptionPane.showMessageDialog(null,err.getMessage());
  229. }
  230.  
  231.  
  232.  
  233.  
  234.  
  235. }
  236. });
  237.  
  238. //THE CANCEL BUTTON
  239.  
  240. btnCancel.addActionListener(new ActionListener() {
  241. @Override
  242. public void actionPerformed(ActionEvent e) {
  243.  
  244. try{
  245. int i=rs.getInt(1);
  246. String n=rs.getString(2);
  247. String c=rs.getString(3);
  248. String p=rs.getString(5);
  249.  
  250. txtID.setText(Integer.toString(i));
  251. txtName.setText(n);
  252. txtCountry.setText(c);
  253. txtProd.setText(p);
  254.  
  255. btnInsert.setEnabled(true);
  256. btnDelete.setEnabled(true);
  257. btnSearch.setEnabled(true);
  258. btnClear.setEnabled(true);
  259. btnNext.setEnabled(true);
  260. btnPrev.setEnabled(true);
  261. btnFirst.setEnabled(true);
  262. btnLast.setEnabled(true);
  263.  
  264. btnSave.setEnabled(false);
  265. btnCancel.setEnabled(false);
  266. }
  267.  
  268. catch(SQLException err){
  269. JOptionPane.showMessageDialog(null,err.getMessage());
  270. }
  271. }
  272. });
  273.  
  274. //THE FIRST BUTTON
  275.  
  276. btnFirst.addActionListener(new ActionListener() {
  277. @Override
  278. public void actionPerformed(ActionEvent e) {
  279.  
  280.  
  281. try{
  282.  
  283. rs.first();
  284.  
  285. int i1=rs.getInt(1);
  286. String n1=rs.getString(2);
  287. String c1=rs.getString(3);
  288. String p1=rs.getString(5);
  289.  
  290. txtID.setText(Integer.toString(i1));
  291. txtName.setText(n1);
  292. txtCountry.setText(c1);
  293. txtProd.setText(p1);
  294.  
  295. }
  296.  
  297. catch(SQLException err){
  298. JOptionPane.showMessageDialog(null,err.getMessage());
  299. }
  300.  
  301. }
  302.  
  303. });
  304.  
  305. //THE LAST BUTTON
  306.  
  307. btnLast.addActionListener(new ActionListener() {
  308. @Override
  309. public void actionPerformed(ActionEvent e) {
  310. try{
  311.  
  312. rs.last();
  313.  
  314. int i1=rs.getInt(1);
  315. String n1=rs.getString(2);
  316. String c1=rs.getString(3);
  317. String p1=rs.getString(5);
  318.  
  319. txtID.setText(Integer.toString(i1));
  320. txtName.setText(n1);
  321. txtCountry.setText(c1);
  322. txtProd.setText(p1);
  323.  
  324. }
  325.  
  326. catch(SQLException err){
  327. JOptionPane.showMessageDialog(null,err.getMessage());
  328. }
  329. }
  330.  
  331. });
  332.  
  333.  
  334. //THE CLEAR BUTTON
  335.  
  336. btnClear.addActionListener(new ActionListener() {
  337. @Override
  338. public void actionPerformed(ActionEvent e) {
  339.  
  340. txtID.setText(null);
  341. txtName.setText(null);
  342. txtCountry.setText(null);
  343. txtProd.setText(null);
  344.  
  345. }
  346. });
  347. }
  348. catch(SQLException E){
  349. JOptionPane.showMessageDialog(supplier.this,E.getMessage());
  350. }
  351. }
  352.  
  353.  
  354. @SuppressWarnings("unchecked")
  355.  
  356.  
  357. private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
  358.  
  359.  
  360. }
  361.  
  362.  
  363.  
  364.  
  365. private void btnMenuActionPerformed(java.awt.event.ActionEvent evt) {
  366. Main_menu m=new Main_menu();
  367. m.setVisible(true);
  368. this.dispose();
  369. }
  370.  
  371.  
  372.  
  373. public static void main(String args[]) {
  374.  
  375. java.awt.EventQueue.invokeLater(new Runnable()
  376. {
  377. public void run() {
  378. new supplier().setVisible(true);
  379. }
  380. });
  381. }
  382.  
  383. // Variables declaration - do not modify
  384. private javax.swing.JButton btnCancel;
  385. private javax.swing.JButton btnClear;
  386. private javax.swing.JButton btnDelete;
  387. private javax.swing.JButton btnEdit;
  388. private javax.swing.JButton btnFirst;
  389. private javax.swing.JButton btnInsert;
  390. private javax.swing.JButton btnLast;
  391. private javax.swing.JButton btnMenu;
  392. private javax.swing.JButton btnNext;
  393. private javax.swing.JButton btnPrev;
  394. private javax.swing.JButton btnSave;
  395. private javax.swing.JButton btnSearch;
  396. private javax.swing.JPanel jPanel1;
  397. private javax.swing.JPanel jPanel2;
  398. private javax.swing.JPanel jPanel3;
  399. private javax.swing.JPanel jPanel4;
  400. private javax.swing.JPanel jPanel5;
  401. private javax.swing.JLabel lblCountry;
  402. private javax.swing.JLabel lblID;
  403. private javax.swing.JLabel lblName;
  404. private javax.swing.JLabel lblProd;
  405. private javax.swing.JTextField txtCountry;
  406. private javax.swing.JTextField txtID;
  407. private javax.swing.JTextField txtName;
  408. private javax.swing.JTextField txtProd;
  409. // End of variables declaration
Add Comment
Please, Sign In to add comment