Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pack_subiect_destinatii_2;
- public class Destinatie
- {
- private String tara;
- private String statiune;
- private int nr_obiective;
- public Destinatie(String tara, String statiune, int nr_obiective)
- {
- this.tara = tara;
- this.statiune = statiune;
- this.nr_obiective = nr_obiective;
- }
- public String getTara() {
- return tara;
- }
- public String getStatiune() {
- return statiune;
- }
- public int getNr_obiective() {
- return nr_obiective;
- }
- public void setTara(String tara) {
- this.tara = tara;
- }
- public void setStatiune(String statiune) {
- this.statiune = statiune;
- }
- public void setNr_obiective(int nr_obiective) {
- this.nr_obiective = nr_obiective;
- }
- @Override
- public String toString() {
- return tara + "," + statiune + "," + nr_obiective;
- }
- }
- package pack_subiect_destinatii_2;
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.AbstractAction;
- import javax.swing.Action;
- import javax.swing.DefaultListModel;
- import javax.swing.JButton;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import javax.swing.JList;
- import javax.swing.JTextField;
- import javax.swing.KeyStroke;
- import com.mysql.jdbc.Statement;
- public class MyFrame extends JFrame
- {
- private JTextField adaugare;
- private JList lista;
- private DefaultListModel model;
- private JButton stergere;
- private List<Destinatie> destinatie=new ArrayList<Destinatie>();
- public MyFrame() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
- {
- super("Statiuni");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(600,600);
- getContentPane().setLayout(new BorderLayout());
- model=new DefaultListModel<>();
- lista=new JList(model);
- adaugare=new JTextField();
- stergere=new JButton("Sterge");
- getContentPane().add(BorderLayout.NORTH,adaugare);
- getContentPane().add(BorderLayout.CENTER,lista);
- getContentPane().add(BorderLayout.SOUTH,stergere);
- stergere.addActionListener(new ButonApasat());
- //conectare la baza de date
- String url="jdbc:mysql://localhost:3306/test";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection con=DriverManager.getConnection(url,"root","");
- Statement sql=(Statement) con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- ResultSet rs=sql.executeQuery("SELECT* FROM destinatii");
- while(rs.next())
- {
- Destinatie d=new Destinatie(rs.getString(1),rs.getString(2),rs.getInt(3));
- destinatie.add(d);
- }
- for(Destinatie i:destinatie)
- {
- model.addElement(i.toString());
- }
- adaugare.addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
- if(e.getKeyCode() == KeyEvent.VK_ENTER){
- //adaugare pe JList
- String textS=adaugare.getText();
- model.addElement(textS.toString());
- //adaugare in baza
- //rs.moveToInsertRow();
- //rs.updateString("tara", textS.getText());
- //rs.updateString("statiune", statiune1);
- //rs.updateInt("nr_obiective",nr_obiective1);
- //rs.insertRow();
- }
- }
- });
- }
- class ButonApasat implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- JButton source=(JButton) e.getSource();
- if(source == stergere)
- {
- int []n=lista.getSelectedIndices();
- for(int i=n.length-1;i>=0;i--)
- {
- model.removeElementAt(n[i]);
- }
- }
- }
- }
- }
- class MainApp
- {
- public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
- {
- JFrame fereastra=new MyFrame();
- fereastra.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement