Advertisement
Guest User

Untitled

a guest
May 30th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.82 KB | None | 0 0
  1. public int getTurId() {
  2. if (turId == 0) {
  3. return 0;
  4. }
  5. return turId;
  6. }
  7.  
  8. public class SearchTripList extends JPanel {
  9.  
  10. private JList list;
  11. private DefaultListModel clientListElements;
  12. private PersonInfo pi;
  13. private String forNamn;
  14. // private static String[] colornames = { "black", "blue", "red", "white" };
  15. // private static Color[] colors = { Color.BLACK, Color.BLUE, Color.RED,
  16. // Color.WHITE };
  17. private JTextField tfDest = new JTextField();
  18. private int listRader = 0;
  19. private ResultSet myRs;
  20. private JPanel pnlCenter = new JPanel(new BorderLayout());
  21. private JPanel pnlSouth = new JPanel(new GridLayout(3, 1));
  22. private JPanel pnlNorth = new JPanel(new GridLayout(1, 4));
  23. private String dest;
  24. private JLabel lvlVeckaAvgang = new JLabel("Vecka avgång");
  25. private JLabel lblStad = new JLabel("Destination");
  26. private JLabel lblTidAvgang = new JLabel("Tid avgång");
  27. private JLabel lblAvgangFran = new JLabel("Avgång från");
  28. private String m = " ";
  29. JButton btnSearch = new JButton("sök");
  30. JButton btnDone = new JButton("Färdigställ");
  31. private Controller controller;
  32. private static JFrame searchFrame;
  33. private int[] idList = new int[27];
  34. private int turId;
  35.  
  36. public SearchTripList() throws SQLException {
  37. setLayout(new BorderLayout());
  38. clientListElements = new DefaultListModel();
  39. list = new JList(clientListElements);
  40. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  41. list.setVisibleRowCount(listRader);
  42. pnlCenter.add(new JScrollPane(list));
  43. add(pnlCenter, BorderLayout.CENTER);
  44. add(pnlNorth, BorderLayout.NORTH);
  45. add(pnlSouth, BorderLayout.SOUTH);
  46. pnlNorth.add(lvlVeckaAvgang);
  47. pnlNorth.add(lblStad);
  48. pnlNorth.add(lblTidAvgang);
  49. pnlNorth.add(lblAvgangFran);
  50. // // pnlCenter.add(new JScrollPane(list));
  51.  
  52. list.addListSelectionListener(new ListSelectionListener() {
  53. public void valueChanged(ListSelectionEvent event) {
  54. turId = idList[list.getSelectedIndex()];
  55. System.out.println(turId);
  56.  
  57. }
  58. });
  59. pnlSouth.add(tfDest);
  60. pnlSouth.add(btnSearch);
  61. pnlSouth.add(btnDone);
  62. createListeners();
  63.  
  64. }
  65. public int getTurId() {
  66. if (turId == 0) {
  67. return 0;
  68. }
  69. return turId;
  70. }
  71.  
  72. public void createListeners() {
  73. Listener list = new Listener();
  74.  
  75. btnSearch.addActionListener(list);
  76. btnDone.addActionListener(list);
  77. }
  78.  
  79. private class Listener implements ActionListener {
  80. public void actionPerformed(ActionEvent e) {
  81. if (e.getSource() == btnSearch) {
  82.  
  83. dest = tfDest.getText();
  84. try {
  85. connect();
  86. writeData();
  87. } catch (SQLException e1) {
  88. // TODO Auto-generated catch block
  89. e1.printStackTrace();
  90. }
  91.  
  92. } else if (e.getSource() == btnDone) {
  93. JFrame frameKvitto = new JFrame("Sök Resa");
  94. frameKvitto.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  95. try {
  96. frameKvitto.add(new Kvitto());
  97. } catch (SQLException e1) {
  98. // TODO Auto-generated catch block
  99. e1.printStackTrace();
  100. }
  101. frameKvitto.pack();
  102. frameKvitto.setVisible(true);
  103. }
  104.  
  105. }
  106.  
  107. }
  108.  
  109. public void writeData() throws SQLException {
  110. String temp = null;
  111. while (myRs.next()) {
  112.  
  113. temp = myRs.getString("veckaAvgang") + m + myRs.getString("destination") + m + myRs.getString("avgangTid")
  114. + m + myRs.getString("avgangFran") + m + myRs.getInt("turId");
  115. clientListElements.addElement(temp);
  116. idList[listRader] = myRs.getInt("turId");
  117. listRader++;
  118. }
  119. // for(int i =0; i<idList.length; i++){
  120. // System.out.println(idList[i]);
  121. // }
  122. System.out.println(Arrays.toString(idList));
  123.  
  124. }
  125.  
  126. public void connect() {
  127. try {
  128. // 1 Get a connection to database
  129. java.sql.PreparedStatement myStmt = null;
  130. Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/amar", "Amar", "zh11ok");
  131. String sql = "SELECT * FROM Tur WHERE destination = ? ";
  132. // 2. Create a statement
  133. myStmt = myConn.prepareStatement(sql);
  134. myStmt.setString(1, dest);
  135. // 3. Execute SQL query
  136. myRs = myStmt.executeQuery();
  137. System.out.println("Connect");
  138. } catch (Exception e) {
  139. e.printStackTrace();
  140. }
  141. }
  142.  
  143. // public static void main(String[] a) throws SQLException {
  144. // searchFrame = new JFrame();
  145. // searchFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  146. // searchFrame.add(new SearchTripList());
  147. // searchFrame.pack();
  148. // searchFrame.setSize(400, 400);
  149. // searchFrame.setVisible(true);
  150. // }
  151.  
  152. public class Kvitto extends JPanel {
  153.  
  154. private JLabel lblTResa = new JLabel("Resa:");
  155. private JLabel lblTPris = new JLabel ("Pris:");
  156. private JLabel lblTPerson = new JLabel ("Bokad av:");
  157. private JLabel lblTVecka = new JLabel ("vecka:");
  158. private JLabel lblTTid = new JLabel ("Klockan: ");
  159.  
  160. private JLabel lblResa = new JLabel("Resa");
  161. private JLabel lblPris = new JLabel("Pris");
  162. private JLabel lblPerson = new JLabel("Bokad av");
  163. private JLabel lblVecka = new JLabel("Vecka");
  164. private JLabel lblTid = new JLabel("1Sven");
  165.  
  166. private JButton btnSearch = new JButton("Sök");
  167. private PersonInfo pi = new PersonInfo();
  168. private SearchTripList stl = new SearchTripList();
  169. private java.sql.PreparedStatement myStmt ;
  170. private ResultSet myRs;
  171. private Connection myConn;
  172.  
  173. private String resa;
  174.  
  175. public Kvitto() throws SQLException {
  176. connect();
  177. writeData();
  178. insertBokning();
  179. setPreferredSize(new Dimension(300, 300));
  180. setLayout(null);
  181. lblTResa.setLocation(50, 40);
  182. lblTResa.setSize(80, 50);
  183. add(lblTResa);
  184. lblResa.setLocation(90, 40);
  185. lblResa.setSize(250, 50);
  186. add(lblResa);
  187.  
  188. lblTVecka.setLocation(50, 80);
  189. lblTVecka.setSize(80, 50);
  190. add(lblTVecka);
  191. lblVecka.setLocation(90, 80);
  192. lblVecka.setSize(80, 50);
  193. add(lblVecka);
  194.  
  195. lblTPerson.setLocation(50, 120);
  196. lblTPerson.setSize(80, 50);
  197. add(lblTPerson);
  198. lblPerson.setLocation(110, 120);
  199. lblPerson.setSize(300, 50);
  200. add(lblPerson);
  201.  
  202. lblTPris.setLocation(50, 160);
  203. lblTPris.setSize(80, 50);
  204. add(lblTPris);
  205. lblPris.setLocation(80, 160);
  206. lblPris.setSize(80, 50);
  207. add(lblPris);
  208.  
  209. lblTTid.setLocation(50, 200);
  210. lblTTid.setSize(80, 50);
  211. add(lblTTid);
  212. lblTid.setLocation(100, 200);
  213. lblTid.setSize(80, 50);
  214. add(lblTid);
  215.  
  216. btnSearch.setLocation(500, 500);
  217. btnSearch.setSize(70, 50);
  218. add(btnSearch);
  219.  
  220. createListeners();
  221. System.out.println(pi.getForNamn());
  222. System.out.println(stl.getTurId());
  223. }
  224.  
  225. public void createListeners() {
  226. Listener list = new Listener();
  227.  
  228. btnSearch.addActionListener(list);
  229.  
  230. }
  231.  
  232. public void writeData() throws SQLException {
  233. StringBuilder sbResult = new StringBuilder();
  234. int reseAlt = 1;
  235. while (myRs.next()) {
  236. sbResult.append(+reseAlt + ". " + myRs.getString("land") + ", " + myRs.getString("språk") + ", "
  237. + myRs.getString("valuta") + ", " + myRs.getString("stadNamn"));
  238. sbResult.append("n");
  239. reseAlt++;
  240. }
  241.  
  242. }
  243.  
  244. public void connect() {
  245. try {
  246. // 1 Get a connection to database
  247. myStmt = null;
  248. myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/amar", "Amar", "zh11ok");
  249. String sql = "SELECT * from Tur WHERE turId = ?";
  250.  
  251. myStmt = myConn.prepareStatement(sql);
  252. // myStmt = myConn.prepareStatement(sq2);
  253. myStmt.setInt(1, 16);
  254. // myStmt = myConn.prepareStatement(sq2);
  255. // myStmt.setString(1,"9604233552");
  256. // 3. Execute SQL query
  257. myRs = myStmt.executeQuery();
  258. while(myRs.next()){
  259. resa = myRs.getString("avgangFran")+"-"+myRs.getString("destination");
  260. lblResa.setText(resa);
  261. lblVecka.setText(myRs.getString("veckaAvgang"));
  262. lblPris.setText(myRs.getString("pris"));
  263. // lblPerson.setText(myRs.getString("personNr"));
  264. }
  265. } catch (Exception e) {
  266. e.printStackTrace();
  267. }
  268. }
  269. public String getresa(){
  270. return resa;
  271. }
  272. public void insertBokning() throws SQLException{
  273. // (3,'9504233852', '3');
  274. String sq2 = "insert into Bokning" + " (turId, personNr)" + "values("+ "'3' ," + "'9504233852')";
  275. myStmt.executeUpdate(sq2);
  276. System.out.println("complete");
  277. }
  278. private class Listener implements ActionListener {
  279. public void actionPerformed(ActionEvent e) {
  280. if (e.getSource() == btnSearch) {
  281. // try {
  282. // if(tfDest.getText().equals(myRs.getString("stad"))){
  283. // taOutput.setText("du valde "+myRs.getString("Stad")+" som
  284. // rese destination");
  285. try {
  286. writeData();
  287. } catch (SQLException e1) {
  288. // TODO Auto-generated catch block
  289. e1.printStackTrace();
  290. }
  291. // }else{
  292. // taOutput.setText("Denna resedestination finns ej, välj någon
  293. // annan");
  294. // }
  295. // } catch (SQLException e1) {
  296. // // TODO Auto-generated catch block
  297. // e1.printStackTrace();
  298. // }
  299. }
  300.  
  301. }
  302.  
  303. }
  304.  
  305. public static void main(String[] a) throws SQLException {
  306. JFrame frame = new JFrame("Sök Resa");
  307. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  308. frame.add(new Kvitto());
  309. frame.pack();
  310. frame.setVisible(true);
  311. }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement