Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PersonInfo extends JPanel {
- private JLabel lblForNamn = new JLabel("För namn");
- private JLabel lblEfterNamn = new JLabel("Efter namn");
- private JLabel lblAdress = new JLabel("Address");
- private JLabel lblPersonNr = new JLabel("Person nummer (10 siffror)");
- private JLabel lblTelefonNr = new JLabel("Telefon nummer");
- private JTextField tfForNamn = new JTextField();
- private JTextField tfEfterNamn = new JTextField();
- private JTextField tfAdress = new JTextField();
- private JTextField tfPersonNr = new JTextField();
- private JTextField tfTelefonNr = new JTextField();
- private JButton btnVidare = new JButton("Vidare");
- private JButton btnAvbryt = new JButton("Avbryt");
- static JFrame mainFrame;
- private JFrame frame;
- private String forNamn;
- private String efterNamn;
- private String adress;
- private String prNr;
- private long telefonNr = 0;
- private Controller controller;
- public PersonInfo() {
- setPreferredSize(new Dimension(500, 600));
- setLayout(null);
- lblForNamn.setLocation(55, 15);
- lblForNamn.setSize(80, 50);
- add(lblForNamn);
- tfForNamn.setLocation(50, 50);
- tfForNamn.setSize(400, 50);
- add(tfForNamn);
- lblEfterNamn.setLocation(55, 100);
- lblEfterNamn.setSize(80, 50);
- add(lblEfterNamn);
- tfEfterNamn.setLocation(50, 135);
- tfEfterNamn.setSize(400, 50);
- add(tfEfterNamn);
- lblAdress.setLocation(55, 185);
- lblAdress.setSize(80, 50);
- add(lblAdress);
- tfAdress.setLocation(50, 220);
- tfAdress.setSize(400, 50);
- add(tfAdress);
- lblPersonNr.setLocation(55, 270);
- lblPersonNr.setSize(200, 50);
- add(lblPersonNr);
- tfPersonNr.setLocation(50, 305);
- tfPersonNr.setSize(400, 50);
- add(tfPersonNr);
- lblTelefonNr.setLocation(55, 355);
- lblTelefonNr.setSize(150, 50);
- add(lblTelefonNr);
- tfTelefonNr.setLocation(50, 390);
- tfTelefonNr.setSize(400, 50);
- add(tfTelefonNr);
- btnVidare.setLocation(275, 500);
- btnVidare.setSize(200, 50);
- add(btnVidare);
- btnAvbryt.setLocation(25, 500);
- btnAvbryt.setSize(200, 50);
- add(btnAvbryt);
- createListeners();
- this.controller = controller;
- }
- public void insertData() {
- try {
- // 1 Get a connection to database
- Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/a", "b", "c");
- // 2. Create a statement
- Statement myStmt = myConn.createStatement();
- // 3. Execute SQL query
- String sq1 = "insert into Resenar" + " (personNr, teleNr, adress, forNamn, efterNamn)" + "values(" + prNr
- + ", " + telefonNr + ", '" + adress + "', '" + forNamn + "', '" + efterNamn + "')";
- myStmt.executeUpdate(sq1);
- System.out.println("Insert complete");
- // Namn som primärnyckel fungerade inte så bra då det fanns fler
- // namn.
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void createListeners() {
- Listener list = new Listener();
- btnVidare.addActionListener(list);
- btnAvbryt.addActionListener(list);
- }
- private class Listener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnVidare) {
- forNamn = tfForNamn.getText();
- efterNamn = tfEfterNamn.getText();
- adress = tfAdress.getText();
- prNr = tfPersonNr.getText();
- telefonNr = Integer.parseInt(tfTelefonNr.getText());
- insertData();
- } else if (e.getSource() == btnAvbryt) {
- mainFrame.setVisible(false);
- JFrame frame = new JFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- try {
- frame.add(new SearchTripList());
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- frame.pack();
- frame.setSize(400, 400);
- frame.setVisible(true);
- }
- }
- }
- public static void main(String[] a) {
- PersonInfo pi = new PersonInfo();
- mainFrame = new JFrame("Person information");
- mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- mainFrame.add(new PersonInfo());
- mainFrame.pack();
- mainFrame.setResizable(false);
- mainFrame.setVisible(true);
- }
- public String getForNamn() {
- // TODO Auto-generated method stub
- return forNamn;
- }
- public class Kvitto extends JPanel {
- private JLabel lblTResa = new JLabel("Resa:");
- private JLabel lblTPris = new JLabel ("Pris:");
- private JLabel lblTPerson = new JLabel ("Bokad av:");
- private JLabel lblTVecka = new JLabel ("vecka:");
- private JLabel lblTTid = new JLabel ("Klockan: ");
- private JLabel lblResa = new JLabel("Resa");
- private JLabel lblPris = new JLabel("Pris");
- private JLabel lblPerson = new JLabel("Bokad av");
- private JLabel lblVecka = new JLabel("Vecka");
- private JLabel lblTid = new JLabel("1Sven");
- private JButton btnSearch = new JButton("Sök");
- private ResultSet myRs;
- private String resa;
- public Kvitto() {
- connect();
- setPreferredSize(new Dimension(300, 300));
- setLayout(null);
- lblTResa.setLocation(50, 40);
- lblTResa.setSize(80, 50);
- add(lblTResa);
- lblResa.setLocation(90, 40);
- lblResa.setSize(250, 50);
- add(lblResa);
- lblTVecka.setLocation(50, 80);
- lblTVecka.setSize(80, 50);
- add(lblTVecka);
- lblVecka.setLocation(90, 80);
- lblVecka.setSize(80, 50);
- add(lblVecka);
- lblTPerson.setLocation(50, 120);
- lblTPerson.setSize(80, 50);
- add(lblTPerson);
- lblPerson.setLocation(110, 120);
- lblPerson.setSize(300, 50);
- add(lblPerson);
- lblTPris.setLocation(50, 160);
- lblTPris.setSize(80, 50);
- add(lblTPris);
- lblPris.setLocation(80, 160);
- lblPris.setSize(80, 50);
- add(lblPris);
- lblTTid.setLocation(50, 200);
- lblTTid.setSize(80, 50);
- add(lblTTid);
- lblTid.setLocation(100, 200);
- lblTid.setSize(80, 50);
- add(lblTid);
- btnSearch.setLocation(500, 500);
- btnSearch.setSize(70, 50);
- add(btnSearch);
- createListeners();
- }
- public void createListeners() {
- Listener list = new Listener();
- btnSearch.addActionListener(list);
- }
- public void writeData() throws SQLException {
- StringBuilder sbResult = new StringBuilder();
- int reseAlt = 1;
- while (myRs.next()) {
- sbResult.append(+reseAlt + ". " + myRs.getString("land") + ", " + myRs.getString("språk") + ", "
- + myRs.getString("valuta") + ", " + myRs.getString("stadNamn"));
- sbResult.append("n");
- reseAlt++;
- }
- }
- public void connect() {
- try {
- // 1 Get a connection to database
- java.sql.PreparedStatement myStmt = null;
- Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/amar", "Amar", "zh11ok");
- String sql = "SELECT * from Tur WHERE turId = ?";
- myRs = myStmt.executeQuery();
- while(myRs.next()){
- resa = myRs.getString("avgangFran")+"-"+myRs.getString("destination");
- lblResa.setText(resa);
- lblVecka.setText(myRs.getString("veckaAvgang"));
- lblPris.setText(myRs.getString("pris"));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public String getresa(){
- return resa;
- }
- private class Listener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnSearch) {
- try {
- writeData();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- }
- }
- public static void main(String[] a) {
- JFrame frame = new JFrame("Sök Resa");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(new Kvitto());
- frame.pack();
- frame.setVisible(true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement