Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. package pack_subiect_destinatii_2;
  2.  
  3. public class Destinatie
  4. {
  5. private String tara;
  6. private String statiune;
  7. private int nr_obiective;
  8. public Destinatie(String tara, String statiune, int nr_obiective)
  9. {
  10. this.tara = tara;
  11. this.statiune = statiune;
  12. this.nr_obiective = nr_obiective;
  13. }
  14. public String getTara() {
  15. return tara;
  16. }
  17. public String getStatiune() {
  18. return statiune;
  19. }
  20. public int getNr_obiective() {
  21. return nr_obiective;
  22. }
  23. public void setTara(String tara) {
  24. this.tara = tara;
  25. }
  26. public void setStatiune(String statiune) {
  27. this.statiune = statiune;
  28. }
  29. public void setNr_obiective(int nr_obiective) {
  30. this.nr_obiective = nr_obiective;
  31. }
  32. @Override
  33. public String toString() {
  34. return tara + "," + statiune + "," + nr_obiective;
  35. }
  36.  
  37. }
  38.  
  39. package pack_subiect_destinatii_2;
  40.  
  41. import java.awt.BorderLayout;
  42. import java.awt.event.ActionEvent;
  43. import java.awt.event.ActionListener;
  44. import java.awt.event.KeyAdapter;
  45. import java.awt.event.KeyEvent;
  46. import java.sql.Connection;
  47. import java.sql.DriverManager;
  48. import java.sql.ResultSet;
  49. import java.sql.SQLException;
  50. import java.util.ArrayList;
  51. import java.util.List;
  52.  
  53. import javax.swing.AbstractAction;
  54. import javax.swing.Action;
  55. import javax.swing.DefaultListModel;
  56. import javax.swing.JButton;
  57. import javax.swing.JComponent;
  58. import javax.swing.JFrame;
  59. import javax.swing.JList;
  60. import javax.swing.JTextField;
  61. import javax.swing.KeyStroke;
  62.  
  63. import com.mysql.jdbc.Statement;
  64.  
  65. public class MyFrame extends JFrame
  66. {
  67. private JTextField adaugare;
  68. private JList lista;
  69. private DefaultListModel model;
  70. private JButton stergere;
  71. private List<Destinatie> destinatie=new ArrayList<Destinatie>();
  72. public MyFrame() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
  73. {
  74. super("Statiuni");
  75. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  76. setSize(600,600);
  77. getContentPane().setLayout(new BorderLayout());
  78.  
  79. model=new DefaultListModel<>();
  80. lista=new JList(model);
  81.  
  82. adaugare=new JTextField();
  83. stergere=new JButton("Sterge");
  84.  
  85. getContentPane().add(BorderLayout.NORTH,adaugare);
  86. getContentPane().add(BorderLayout.CENTER,lista);
  87. getContentPane().add(BorderLayout.SOUTH,stergere);
  88. stergere.addActionListener(new ButonApasat());
  89.  
  90.  
  91. //conectare la baza de date
  92. String url="jdbc:mysql://localhost:3306/test";
  93. Class.forName("com.mysql.jdbc.Driver").newInstance();
  94. Connection con=DriverManager.getConnection(url,"root","");
  95. Statement sql=(Statement) con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  96. ResultSet rs=sql.executeQuery("SELECT* FROM destinatii");
  97.  
  98. while(rs.next())
  99. {
  100. Destinatie d=new Destinatie(rs.getString(1),rs.getString(2),rs.getInt(3));
  101. destinatie.add(d);
  102. }
  103. for(Destinatie i:destinatie)
  104. {
  105. model.addElement(i.toString());
  106. }
  107.  
  108. adaugare.addKeyListener(new KeyAdapter() {
  109. @Override
  110. public void keyPressed(KeyEvent e) {
  111. if(e.getKeyCode() == KeyEvent.VK_ENTER){
  112.  
  113. //adaugare pe JList
  114. String textS=adaugare.getText();
  115. model.addElement(textS.toString());
  116. //adaugare in baza
  117. //rs.moveToInsertRow();
  118. //rs.updateString("tara", textS.getText());
  119. //rs.updateString("statiune", statiune1);
  120. //rs.updateInt("nr_obiective",nr_obiective1);
  121. //rs.insertRow();
  122. }
  123. }
  124.  
  125. });
  126.  
  127. }
  128.  
  129. class ButonApasat implements ActionListener {
  130.  
  131. @Override
  132. public void actionPerformed(ActionEvent e)
  133. {
  134. JButton source=(JButton) e.getSource();
  135. if(source == stergere)
  136. {
  137. int []n=lista.getSelectedIndices();
  138. for(int i=n.length-1;i>=0;i--)
  139. {
  140. model.removeElementAt(n[i]);
  141.  
  142. }
  143.  
  144. }
  145.  
  146. }
  147.  
  148. }
  149.  
  150.  
  151. }
  152. class MainApp
  153. {
  154.  
  155. public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
  156. {
  157. JFrame fereastra=new MyFrame();
  158. fereastra.setVisible(true);
  159.  
  160.  
  161. }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement