Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int getTurId() {
- if (turId == 0) {
- return 0;
- }
- return turId;
- }
- public class SearchTripList extends JPanel {
- private JList list;
- private DefaultListModel clientListElements;
- private PersonInfo pi;
- private String forNamn;
- // private static String[] colornames = { "black", "blue", "red", "white" };
- // private static Color[] colors = { Color.BLACK, Color.BLUE, Color.RED,
- // Color.WHITE };
- private JTextField tfDest = new JTextField();
- private int listRader = 0;
- private ResultSet myRs;
- private JPanel pnlCenter = new JPanel(new BorderLayout());
- private JPanel pnlSouth = new JPanel(new GridLayout(3, 1));
- private JPanel pnlNorth = new JPanel(new GridLayout(1, 4));
- private String dest;
- private JLabel lvlVeckaAvgang = new JLabel("Vecka avgång");
- private JLabel lblStad = new JLabel("Destination");
- private JLabel lblTidAvgang = new JLabel("Tid avgång");
- private JLabel lblAvgangFran = new JLabel("Avgång från");
- private String m = " ";
- JButton btnSearch = new JButton("sök");
- JButton btnDone = new JButton("Färdigställ");
- private Controller controller;
- private static JFrame searchFrame;
- private int[] idList = new int[27];
- private int turId;
- public SearchTripList() throws SQLException {
- setLayout(new BorderLayout());
- clientListElements = new DefaultListModel();
- list = new JList(clientListElements);
- list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- list.setVisibleRowCount(listRader);
- pnlCenter.add(new JScrollPane(list));
- add(pnlCenter, BorderLayout.CENTER);
- add(pnlNorth, BorderLayout.NORTH);
- add(pnlSouth, BorderLayout.SOUTH);
- pnlNorth.add(lvlVeckaAvgang);
- pnlNorth.add(lblStad);
- pnlNorth.add(lblTidAvgang);
- pnlNorth.add(lblAvgangFran);
- // // pnlCenter.add(new JScrollPane(list));
- list.addListSelectionListener(new ListSelectionListener() {
- public void valueChanged(ListSelectionEvent event) {
- turId = idList[list.getSelectedIndex()];
- System.out.println(turId);
- }
- });
- pnlSouth.add(tfDest);
- pnlSouth.add(btnSearch);
- pnlSouth.add(btnDone);
- createListeners();
- }
- public int getTurId() {
- if (turId == 0) {
- return 0;
- }
- return turId;
- }
- public void createListeners() {
- Listener list = new Listener();
- btnSearch.addActionListener(list);
- btnDone.addActionListener(list);
- }
- private class Listener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnSearch) {
- dest = tfDest.getText();
- try {
- connect();
- writeData();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- } else if (e.getSource() == btnDone) {
- JFrame frameKvitto = new JFrame("Sök Resa");
- frameKvitto.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- try {
- frameKvitto.add(new Kvitto());
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- frameKvitto.pack();
- frameKvitto.setVisible(true);
- }
- }
- }
- public void writeData() throws SQLException {
- String temp = null;
- while (myRs.next()) {
- temp = myRs.getString("veckaAvgang") + m + myRs.getString("destination") + m + myRs.getString("avgangTid")
- + m + myRs.getString("avgangFran") + m + myRs.getInt("turId");
- clientListElements.addElement(temp);
- idList[listRader] = myRs.getInt("turId");
- listRader++;
- }
- // for(int i =0; i<idList.length; i++){
- // System.out.println(idList[i]);
- // }
- System.out.println(Arrays.toString(idList));
- }
- 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 destination = ? ";
- // 2. Create a statement
- myStmt = myConn.prepareStatement(sql);
- myStmt.setString(1, dest);
- // 3. Execute SQL query
- myRs = myStmt.executeQuery();
- System.out.println("Connect");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- // public static void main(String[] a) throws SQLException {
- // searchFrame = new JFrame();
- // searchFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // searchFrame.add(new SearchTripList());
- // searchFrame.pack();
- // searchFrame.setSize(400, 400);
- // searchFrame.setVisible(true);
- // }
- 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 PersonInfo pi = new PersonInfo();
- private SearchTripList stl = new SearchTripList();
- private java.sql.PreparedStatement myStmt ;
- private ResultSet myRs;
- private Connection myConn;
- private String resa;
- public Kvitto() throws SQLException {
- connect();
- writeData();
- insertBokning();
- 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();
- System.out.println(pi.getForNamn());
- System.out.println(stl.getTurId());
- }
- 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
- myStmt = null;
- myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/amar", "Amar", "zh11ok");
- String sql = "SELECT * from Tur WHERE turId = ?";
- myStmt = myConn.prepareStatement(sql);
- // myStmt = myConn.prepareStatement(sq2);
- myStmt.setInt(1, 16);
- // myStmt = myConn.prepareStatement(sq2);
- // myStmt.setString(1,"9604233552");
- // 3. Execute SQL query
- 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"));
- // lblPerson.setText(myRs.getString("personNr"));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public String getresa(){
- return resa;
- }
- public void insertBokning() throws SQLException{
- // (3,'9504233852', '3');
- String sq2 = "insert into Bokning" + " (turId, personNr)" + "values("+ "'3' ," + "'9504233852')";
- myStmt.executeUpdate(sq2);
- System.out.println("complete");
- }
- private class Listener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnSearch) {
- // try {
- // if(tfDest.getText().equals(myRs.getString("stad"))){
- // taOutput.setText("du valde "+myRs.getString("Stad")+" som
- // rese destination");
- try {
- writeData();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- // }else{
- // taOutput.setText("Denna resedestination finns ej, välj någon
- // annan");
- // }
- // } catch (SQLException e1) {
- // // TODO Auto-generated catch block
- // e1.printStackTrace();
- // }
- }
- }
- }
- public static void main(String[] a) throws SQLException {
- 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