Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. package restaurante;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.sql.SQLException;
  7.  
  8. public class MainApp {
  9.  
  10. public static void main(String[] args) throws IOException, IllegalAccessException,ClassNotFoundException,InstantiationException,SQLException {
  11. // TODO Auto-generated method stub
  12. MyFrame frm=new MyFrame();
  13. frm.setVisible(true);
  14.  
  15.  
  16. }
  17.  
  18. }
  19. package restaurante;
  20.  
  21. import java.awt.event.ActionEvent;
  22. import java.awt.event.ActionListener;
  23. import java.io.BufferedReader;
  24. import java.io.IOException;
  25. import java.io.InputStreamReader;
  26. import java.sql.Connection;
  27. import java.sql.DriverManager;
  28. import java.sql.PreparedStatement;
  29. import java.sql.ResultSet;
  30. import java.sql.SQLException;
  31. import java.sql.Statement;
  32. import java.util.Vector;
  33.  
  34. import javax.swing.DefaultListModel;
  35. import javax.swing.JButton;
  36. import javax.swing.JFrame;
  37. import javax.swing.JLabel;
  38. import javax.swing.JList;
  39. import javax.swing.JTextField;
  40.  
  41.  
  42.  
  43. public class MyFrame extends JFrame {
  44. private static final long serialVersionUID = 1L;
  45.  
  46. JList<Object>myList;
  47. DefaultListModel<Object> myModel;
  48. Vector<Restaurant>restaurante=new Vector<>();
  49. private JTextField txtDenumire;
  50. private JButton btnSterge;
  51. String url="jdbc:mysql://localhost:3306/test";
  52. Connection con=(Connection)DriverManager.getConnection(url,"root","root");
  53. Statement sql=(Statement)con.createStatement();
  54. ResultSet rs=sql.executeQuery("SELECT *From restaurant");
  55.  
  56. public MyFrame() throws IOException,SQLException,InstantiationException,ClassNotFoundException,IllegalAccessException
  57. {
  58. super("Afisare");
  59.  
  60. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  61. setLocation(400,400);
  62. setSize(440,470);
  63. getContentPane().setLayout(null);
  64. myList=new JList<Object>();
  65. myModel=new DefaultListModel<Object>();
  66. JLabel titlu=new JLabel("Lista restaurante");
  67. titlu.setBounds(100,50,100,30);
  68. myList.setBounds(20,80,360,170);
  69. getContentPane().add(titlu);
  70. getContentPane().add(myList);
  71. JLabel lblDenumire=new JLabel("Denumire");
  72. txtDenumire=new JTextField();
  73. btnSterge=new JButton("Sterge");
  74. lblDenumire.setBounds(20,10,100,20);
  75. txtDenumire.setBounds(90,10,150,20);
  76. btnSterge.setBounds(40,40,100,20);
  77. getContentPane().add(lblDenumire);
  78. getContentPane().add(txtDenumire);
  79. getContentPane().add(btnSterge);
  80. btnSterge.addActionListener(new ButonApasat());
  81. setVisible(true);
  82.  
  83. Class.forName("com.mysql.jdbc.Driver").newInstance();
  84.  
  85.  
  86. while(rs.next())
  87. {
  88. Restaurant res=new Restaurant(rs.getString("denumire"),rs.getString("specific"),rs.getString("zona"));
  89. restaurante.add(res);
  90. }
  91. for(int i=0;i<restaurante.size();i++)
  92. {
  93. Restaurant res=(Restaurant)restaurante.elementAt(i);
  94. myModel.addElement(res.toString());
  95. myList.setModel(myModel);
  96.  
  97. }
  98.  
  99.  
  100.  
  101. con.close();
  102. sql.close();
  103. rs.close();
  104. }
  105. class ButonApasat implements ActionListener
  106. {
  107.  
  108. @Override
  109. public void actionPerformed(ActionEvent e) {
  110. // TODO Auto-generated method stub
  111. JButton source=(JButton)e.getSource();
  112. if(source==btnSterge)
  113. {
  114. //myModel.clear();
  115. String melodieS=txtDenumire.getText();
  116.  
  117. for(int i=0;i<restaurante.size();i++)
  118. {
  119. if(restaurante.elementAt(i).equals(melodieS))
  120. {
  121. restaurante.remove(i);
  122. myModel.removeElementAt(i);
  123. try {
  124. PreparedStatement deleteStmt=con.prepareStatement("Delete from restaurant where denumire=\""+melodieS+ "\"");
  125.  
  126. deleteStmt.execute();
  127. rs=sql.executeQuery("Select *from restaurant");
  128. } catch (SQLException e1) {
  129. // TODO Auto-generated catch block
  130. e1.printStackTrace();
  131. }
  132. }
  133. }
  134. /* for(int i=0;i<restaurante.size();i++)
  135. {
  136. Restaurant res=(Restaurant)restaurante.elementAt(i);
  137. myModel.addElement(res.toString());
  138. myList.setModel(myModel);
  139.  
  140. }*/
  141. }
  142.  
  143. }
  144.  
  145.  
  146. }
  147. }
  148. package restaurante;
  149.  
  150. public class Restaurant {
  151. private String denumire;
  152. private String specific;
  153. private String zona;
  154. public Restaurant(String denumire, String specific, String zona) {
  155.  
  156. this.denumire = denumire;
  157. this.specific = specific;
  158. this.zona = zona;
  159. }
  160. public String getDenumire() {
  161. return denumire;
  162. }
  163. public void setDenumire(String denumire) {
  164. this.denumire = denumire;
  165. }
  166. public String getSpecific() {
  167. return specific;
  168. }
  169. public void setSpecific(String specific) {
  170. this.specific = specific;
  171. }
  172. public String getZona() {
  173. return zona;
  174. }
  175. public void setZona(String zona) {
  176. this.zona = zona;
  177. }
  178. @Override
  179. public String toString() {
  180. return "denumire=" + denumire + ", specific=" + specific
  181. + ", zona=" + zona + "]";
  182. }
  183.  
  184.  
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement