Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. package destinatie_grafica;
  2.  
  3. public class Destinatie {
  4. private String tara;
  5. private String statiune;
  6. private int nr_obiective;
  7. public Destinatie(String tara, String statiune, int nr_obiective) {
  8.  
  9. this.tara = tara;
  10. this.statiune = statiune;
  11. this.nr_obiective = nr_obiective;
  12. }
  13. public String getTara() {
  14. return tara;
  15. }
  16. public void setTara(String tara) {
  17. this.tara = tara;
  18. }
  19. public String getStatiune() {
  20. return statiune;
  21. }
  22. public void setStatiune(String statiune) {
  23. this.statiune = statiune;
  24. }
  25. public int getNr_obiective() {
  26. return nr_obiective;
  27. }
  28. public void setNr_obiective(int nr_obiective) {
  29. this.nr_obiective = nr_obiective;
  30. }
  31. @Override
  32. public String toString() {
  33. return tara + "," + statiune
  34. + "," + nr_obiective + " ";
  35. }
  36. }
  37. package destinatie_grafica;
  38.  
  39. import java.awt.event.KeyAdapter;
  40. import java.awt.event.KeyEvent;
  41. import java.awt.event.KeyListener;
  42. import java.sql.Connection;
  43. import java.sql.DriverManager;
  44. import java.sql.ResultSet;
  45. import java.sql.SQLException;
  46. import java.sql.Statement;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49.  
  50. import javax.swing.DefaultListModel;
  51. import javax.swing.JButton;
  52. import javax.swing.JFrame;
  53. import javax.swing.JList;
  54. import javax.swing.JTextField;
  55.  
  56.  
  57.  
  58. public class MyFrame extends JFrame {
  59. private JTextField txtAdaugare;
  60. private JButton btnStergere;
  61. private JList<Object> myList;
  62. private DefaultListModel<Object> myModel;
  63. private List<Destinatie> destinatie=new ArrayList<Destinatie>();
  64. public MyFrame() throws SQLException,ClassNotFoundException,IllegalAccessException,InstantiationException
  65. {
  66. super("Statiuni");
  67. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  68. setLocation(400,400);
  69. setSize(400,400);
  70. getContentPane().setLayout(null);
  71. txtAdaugare=new JTextField();
  72. btnStergere=new JButton("Sterge Statiune");
  73. myModel=new DefaultListModel<>();
  74. myList=new JList<Object>(myModel);
  75. txtAdaugare.setBounds(20,10,350,20);
  76. myList.setBounds(20,30,350,160);
  77. btnStergere.setBounds(20,200,350,30);
  78. getContentPane().add(txtAdaugare);
  79. getContentPane().add(myList);
  80. getContentPane().add(btnStergere);
  81.  
  82. setVisible(true);
  83. String url="jdbc:mysql://localhost:3306/test";
  84. Class.forName("com.mysql.jdbc.Driver").newInstance();
  85. Connection con=DriverManager.getConnection(url,"root","root");
  86. Statement sql=(Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  87. ResultSet rs=sql.executeQuery("select *from destinatie");
  88. while(rs.next())
  89. {
  90. Destinatie d=new Destinatie(rs.getString("tara"),rs.getString("statiune"),rs.getInt("nr_obiective"));
  91. destinatie.add(d);
  92. }
  93. for(Destinatie i:destinatie)
  94. {
  95. myModel.addElement(i.toString());
  96. myList.setModel(myModel);
  97. }
  98.  
  99. txtAdaugare.addKeyListener(new KeyAdapter() {
  100. public void keyPressed(KeyEvent e)
  101. {
  102. if(e.getKeyCode()==KeyEvent.VK_ENTER)
  103. {
  104. String textS=txtAdaugare.getText();
  105. String[]parti=txtAdaugare.getText().split(",");
  106. myModel.addElement(textS.toString());
  107. try
  108. {
  109. rs.moveToInsertRow();
  110. rs.updateString("tara", parti[0]);
  111. rs.updateString("statiune", parti[1]);
  112. rs.updateInt("nr_obiective", Integer.parseInt(parti[2]));
  113. rs.insertRow();
  114. }catch(SQLException el)
  115. {
  116. el.printStackTrace();
  117. }
  118. }
  119.  
  120. }
  121.  
  122.  
  123.  
  124. });
  125.  
  126. }
  127. }
  128.  
  129. package destinatie_grafica;
  130.  
  131. import java.sql.SQLException;
  132.  
  133. public class MainApp {
  134.  
  135. public static void main(String[] args) throws SQLException,ClassNotFoundException,IllegalAccessException,InstantiationException{
  136. // TODO Auto-generated method stub
  137. MyFrame frm=new MyFrame();
  138. frm.setVisible(true);
  139. }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement