Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package destinatie_grafica;
- 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 void setTara(String tara) {
- this.tara = tara;
- }
- public String getStatiune() {
- return statiune;
- }
- public void setStatiune(String statiune) {
- this.statiune = statiune;
- }
- public int getNr_obiective() {
- return nr_obiective;
- }
- public void setNr_obiective(int nr_obiective) {
- this.nr_obiective = nr_obiective;
- }
- @Override
- public String toString() {
- return tara + "," + statiune
- + "," + nr_obiective + " ";
- }
- }
- package destinatie_grafica;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.DefaultListModel;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JList;
- import javax.swing.JTextField;
- public class MyFrame extends JFrame {
- private JTextField txtAdaugare;
- private JButton btnStergere;
- private JList<Object> myList;
- private DefaultListModel<Object> myModel;
- private List<Destinatie> destinatie=new ArrayList<Destinatie>();
- public MyFrame() throws SQLException,ClassNotFoundException,IllegalAccessException,InstantiationException
- {
- super("Statiuni");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLocation(400,400);
- setSize(400,400);
- getContentPane().setLayout(null);
- txtAdaugare=new JTextField();
- btnStergere=new JButton("Sterge Statiune");
- myModel=new DefaultListModel<>();
- myList=new JList<Object>(myModel);
- txtAdaugare.setBounds(20,10,350,20);
- myList.setBounds(20,30,350,160);
- btnStergere.setBounds(20,200,350,30);
- getContentPane().add(txtAdaugare);
- getContentPane().add(myList);
- getContentPane().add(btnStergere);
- setVisible(true);
- String url="jdbc:mysql://localhost:3306/test";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection con=DriverManager.getConnection(url,"root","root");
- Statement sql=(Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- ResultSet rs=sql.executeQuery("select *from destinatie");
- while(rs.next())
- {
- Destinatie d=new Destinatie(rs.getString("tara"),rs.getString("statiune"),rs.getInt("nr_obiective"));
- destinatie.add(d);
- }
- for(Destinatie i:destinatie)
- {
- myModel.addElement(i.toString());
- myList.setModel(myModel);
- }
- txtAdaugare.addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent e)
- {
- if(e.getKeyCode()==KeyEvent.VK_ENTER)
- {
- String textS=txtAdaugare.getText();
- String[]parti=txtAdaugare.getText().split(",");
- myModel.addElement(textS.toString());
- try
- {
- rs.moveToInsertRow();
- rs.updateString("tara", parti[0]);
- rs.updateString("statiune", parti[1]);
- rs.updateInt("nr_obiective", Integer.parseInt(parti[2]));
- rs.insertRow();
- }catch(SQLException el)
- {
- el.printStackTrace();
- }
- }
- }
- });
- }
- }
- package destinatie_grafica;
- import java.sql.SQLException;
- public class MainApp {
- public static void main(String[] args) throws SQLException,ClassNotFoundException,IllegalAccessException,InstantiationException{
- // TODO Auto-generated method stub
- MyFrame frm=new MyFrame();
- frm.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement