Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package restaurante;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.sql.SQLException;
- public class MainApp {
- public static void main(String[] args) throws IOException, IllegalAccessException,ClassNotFoundException,InstantiationException,SQLException {
- // TODO Auto-generated method stub
- MyFrame frm=new MyFrame();
- frm.setVisible(true);
- }
- }
- package restaurante;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Vector;
- import javax.swing.DefaultListModel;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JList;
- import javax.swing.JTextField;
- public class MyFrame extends JFrame {
- private static final long serialVersionUID = 1L;
- JList<Object>myList;
- DefaultListModel<Object> myModel;
- Vector<Restaurant>restaurante=new Vector<>();
- private JTextField txtDenumire;
- private JButton btnSterge;
- String url="jdbc:mysql://localhost:3306/test";
- Connection con=(Connection)DriverManager.getConnection(url,"root","root");
- Statement sql=(Statement)con.createStatement();
- ResultSet rs=sql.executeQuery("SELECT *From restaurant");
- public MyFrame() throws IOException,SQLException,InstantiationException,ClassNotFoundException,IllegalAccessException
- {
- super("Afisare");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLocation(400,400);
- setSize(440,470);
- getContentPane().setLayout(null);
- myList=new JList<Object>();
- myModel=new DefaultListModel<Object>();
- JLabel titlu=new JLabel("Lista restaurante");
- titlu.setBounds(100,50,100,30);
- myList.setBounds(20,80,360,170);
- getContentPane().add(titlu);
- getContentPane().add(myList);
- JLabel lblDenumire=new JLabel("Denumire");
- txtDenumire=new JTextField();
- btnSterge=new JButton("Sterge");
- lblDenumire.setBounds(20,10,100,20);
- txtDenumire.setBounds(90,10,150,20);
- btnSterge.setBounds(40,40,100,20);
- getContentPane().add(lblDenumire);
- getContentPane().add(txtDenumire);
- getContentPane().add(btnSterge);
- btnSterge.addActionListener(new ButonApasat());
- setVisible(true);
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- while(rs.next())
- {
- Restaurant res=new Restaurant(rs.getString("denumire"),rs.getString("specific"),rs.getString("zona"));
- restaurante.add(res);
- }
- for(int i=0;i<restaurante.size();i++)
- {
- Restaurant res=(Restaurant)restaurante.elementAt(i);
- myModel.addElement(res.toString());
- myList.setModel(myModel);
- }
- con.close();
- sql.close();
- rs.close();
- }
- class ButonApasat implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- JButton source=(JButton)e.getSource();
- if(source==btnSterge)
- {
- //myModel.clear();
- String melodieS=txtDenumire.getText();
- for(int i=0;i<restaurante.size();i++)
- {
- if(restaurante.elementAt(i).equals(melodieS))
- {
- restaurante.remove(i);
- myModel.removeElementAt(i);
- try {
- PreparedStatement deleteStmt=con.prepareStatement("Delete from restaurant where denumire=\""+melodieS+ "\"");
- deleteStmt.execute();
- rs=sql.executeQuery("Select *from restaurant");
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- }
- /* for(int i=0;i<restaurante.size();i++)
- {
- Restaurant res=(Restaurant)restaurante.elementAt(i);
- myModel.addElement(res.toString());
- myList.setModel(myModel);
- }*/
- }
- }
- }
- }
- package restaurante;
- public class Restaurant {
- private String denumire;
- private String specific;
- private String zona;
- public Restaurant(String denumire, String specific, String zona) {
- this.denumire = denumire;
- this.specific = specific;
- this.zona = zona;
- }
- public String getDenumire() {
- return denumire;
- }
- public void setDenumire(String denumire) {
- this.denumire = denumire;
- }
- public String getSpecific() {
- return specific;
- }
- public void setSpecific(String specific) {
- this.specific = specific;
- }
- public String getZona() {
- return zona;
- }
- public void setZona(String zona) {
- this.zona = zona;
- }
- @Override
- public String toString() {
- return "denumire=" + denumire + ", specific=" + specific
- + ", zona=" + zona + "]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement