Advertisement
Guest User

Untitled

a guest
May 30th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. public class PersonInfo extends JPanel {
  2.  
  3. private JLabel lblForNamn = new JLabel("För namn");
  4. private JLabel lblEfterNamn = new JLabel("Efter namn");
  5. private JLabel lblAdress = new JLabel("Address");
  6. private JLabel lblPersonNr = new JLabel("Person nummer (10 siffror)");
  7. private JLabel lblTelefonNr = new JLabel("Telefon nummer");
  8.  
  9. private JTextField tfForNamn = new JTextField();
  10. private JTextField tfEfterNamn = new JTextField();
  11. private JTextField tfAdress = new JTextField();
  12. private JTextField tfPersonNr = new JTextField();
  13. private JTextField tfTelefonNr = new JTextField();
  14. private JButton btnVidare = new JButton("Vidare");
  15. private JButton btnAvbryt = new JButton("Avbryt");
  16. static JFrame mainFrame;
  17.  
  18. private JFrame frame;
  19.  
  20. private String forNamn;
  21. private String efterNamn;
  22. private String adress;
  23. private String prNr;
  24. private long telefonNr = 0;
  25.  
  26. private Controller controller;
  27.  
  28. public PersonInfo() {
  29. setPreferredSize(new Dimension(500, 600));
  30. setLayout(null);
  31.  
  32. lblForNamn.setLocation(55, 15);
  33. lblForNamn.setSize(80, 50);
  34. add(lblForNamn);
  35. tfForNamn.setLocation(50, 50);
  36. tfForNamn.setSize(400, 50);
  37. add(tfForNamn);
  38.  
  39. lblEfterNamn.setLocation(55, 100);
  40. lblEfterNamn.setSize(80, 50);
  41. add(lblEfterNamn);
  42. tfEfterNamn.setLocation(50, 135);
  43. tfEfterNamn.setSize(400, 50);
  44. add(tfEfterNamn);
  45.  
  46. lblAdress.setLocation(55, 185);
  47. lblAdress.setSize(80, 50);
  48. add(lblAdress);
  49. tfAdress.setLocation(50, 220);
  50. tfAdress.setSize(400, 50);
  51. add(tfAdress);
  52.  
  53. lblPersonNr.setLocation(55, 270);
  54. lblPersonNr.setSize(200, 50);
  55. add(lblPersonNr);
  56. tfPersonNr.setLocation(50, 305);
  57. tfPersonNr.setSize(400, 50);
  58. add(tfPersonNr);
  59.  
  60. lblTelefonNr.setLocation(55, 355);
  61. lblTelefonNr.setSize(150, 50);
  62. add(lblTelefonNr);
  63. tfTelefonNr.setLocation(50, 390);
  64. tfTelefonNr.setSize(400, 50);
  65. add(tfTelefonNr);
  66.  
  67. btnVidare.setLocation(275, 500);
  68. btnVidare.setSize(200, 50);
  69. add(btnVidare);
  70.  
  71. btnAvbryt.setLocation(25, 500);
  72. btnAvbryt.setSize(200, 50);
  73. add(btnAvbryt);
  74. createListeners();
  75. this.controller = controller;
  76. }
  77.  
  78. public void insertData() {
  79. try {
  80. // 1 Get a connection to database
  81. Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/a", "b", "c");
  82. // 2. Create a statement
  83. Statement myStmt = myConn.createStatement();
  84. // 3. Execute SQL query
  85. String sq1 = "insert into Resenar" + " (personNr, teleNr, adress, forNamn, efterNamn)" + "values(" + prNr
  86. + ", " + telefonNr + ", '" + adress + "', '" + forNamn + "', '" + efterNamn + "')";
  87.  
  88. myStmt.executeUpdate(sq1);
  89. System.out.println("Insert complete");
  90. // Namn som primärnyckel fungerade inte så bra då det fanns fler
  91. // namn.
  92.  
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. }
  97.  
  98. public void createListeners() {
  99. Listener list = new Listener();
  100.  
  101. btnVidare.addActionListener(list);
  102. btnAvbryt.addActionListener(list);
  103. }
  104.  
  105. private class Listener implements ActionListener {
  106. public void actionPerformed(ActionEvent e) {
  107. if (e.getSource() == btnVidare) {
  108. forNamn = tfForNamn.getText();
  109. efterNamn = tfEfterNamn.getText();
  110. adress = tfAdress.getText();
  111. prNr = tfPersonNr.getText();
  112. telefonNr = Integer.parseInt(tfTelefonNr.getText());
  113. insertData();
  114.  
  115. } else if (e.getSource() == btnAvbryt) {
  116. mainFrame.setVisible(false);
  117. JFrame frame = new JFrame();
  118. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  119. try {
  120. frame.add(new SearchTripList());
  121. } catch (SQLException e1) {
  122. e1.printStackTrace();
  123. }
  124. frame.pack();
  125. frame.setSize(400, 400);
  126. frame.setVisible(true);
  127. }
  128. }
  129. }
  130.  
  131. public static void main(String[] a) {
  132. PersonInfo pi = new PersonInfo();
  133. mainFrame = new JFrame("Person information");
  134. mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  135. mainFrame.add(new PersonInfo());
  136. mainFrame.pack();
  137. mainFrame.setResizable(false);
  138. mainFrame.setVisible(true);
  139. }
  140.  
  141. public String getForNamn() {
  142. // TODO Auto-generated method stub
  143. return forNamn;
  144. }
  145.  
  146. public class Kvitto extends JPanel {
  147.  
  148. private JLabel lblTResa = new JLabel("Resa:");
  149. private JLabel lblTPris = new JLabel ("Pris:");
  150. private JLabel lblTPerson = new JLabel ("Bokad av:");
  151. private JLabel lblTVecka = new JLabel ("vecka:");
  152. private JLabel lblTTid = new JLabel ("Klockan: ");
  153.  
  154. private JLabel lblResa = new JLabel("Resa");
  155. private JLabel lblPris = new JLabel("Pris");
  156. private JLabel lblPerson = new JLabel("Bokad av");
  157. private JLabel lblVecka = new JLabel("Vecka");
  158. private JLabel lblTid = new JLabel("1Sven");
  159.  
  160. private JButton btnSearch = new JButton("Sök");
  161. private ResultSet myRs;
  162. private String resa;
  163.  
  164. public Kvitto() {
  165. connect();
  166. setPreferredSize(new Dimension(300, 300));
  167. setLayout(null);
  168. lblTResa.setLocation(50, 40);
  169. lblTResa.setSize(80, 50);
  170. add(lblTResa);
  171. lblResa.setLocation(90, 40);
  172. lblResa.setSize(250, 50);
  173. add(lblResa);
  174.  
  175. lblTVecka.setLocation(50, 80);
  176. lblTVecka.setSize(80, 50);
  177. add(lblTVecka);
  178. lblVecka.setLocation(90, 80);
  179. lblVecka.setSize(80, 50);
  180. add(lblVecka);
  181.  
  182. lblTPerson.setLocation(50, 120);
  183. lblTPerson.setSize(80, 50);
  184. add(lblTPerson);
  185. lblPerson.setLocation(110, 120);
  186. lblPerson.setSize(300, 50);
  187. add(lblPerson);
  188.  
  189. lblTPris.setLocation(50, 160);
  190. lblTPris.setSize(80, 50);
  191. add(lblTPris);
  192. lblPris.setLocation(80, 160);
  193. lblPris.setSize(80, 50);
  194. add(lblPris);
  195.  
  196. lblTTid.setLocation(50, 200);
  197. lblTTid.setSize(80, 50);
  198. add(lblTTid);
  199. lblTid.setLocation(100, 200);
  200. lblTid.setSize(80, 50);
  201. add(lblTid);
  202.  
  203. btnSearch.setLocation(500, 500);
  204. btnSearch.setSize(70, 50);
  205. add(btnSearch);
  206.  
  207. createListeners();
  208. }
  209.  
  210. public void createListeners() {
  211. Listener list = new Listener();
  212.  
  213. btnSearch.addActionListener(list);
  214.  
  215. }
  216.  
  217. public void writeData() throws SQLException {
  218. StringBuilder sbResult = new StringBuilder();
  219. int reseAlt = 1;
  220. while (myRs.next()) {
  221. sbResult.append(+reseAlt + ". " + myRs.getString("land") + ", " + myRs.getString("språk") + ", "
  222. + myRs.getString("valuta") + ", " + myRs.getString("stadNamn"));
  223. sbResult.append("n");
  224. reseAlt++;
  225. }
  226.  
  227. }
  228.  
  229. public void connect() {
  230. try {
  231. // 1 Get a connection to database
  232. java.sql.PreparedStatement myStmt = null;
  233. Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/amar", "Amar", "zh11ok");
  234. String sql = "SELECT * from Tur WHERE turId = ?";
  235.  
  236. myRs = myStmt.executeQuery();
  237. while(myRs.next()){
  238. resa = myRs.getString("avgangFran")+"-"+myRs.getString("destination");
  239. lblResa.setText(resa);
  240. lblVecka.setText(myRs.getString("veckaAvgang"));
  241. lblPris.setText(myRs.getString("pris"));
  242. }
  243. } catch (Exception e) {
  244. e.printStackTrace();
  245. }
  246. }
  247. public String getresa(){
  248. return resa;
  249. }
  250. private class Listener implements ActionListener {
  251. public void actionPerformed(ActionEvent e) {
  252. if (e.getSource() == btnSearch) {
  253.  
  254. try {
  255. writeData();
  256. } catch (SQLException e1) {
  257. // TODO Auto-generated catch block
  258. e1.printStackTrace();
  259. }
  260.  
  261. }
  262.  
  263. }
  264.  
  265. }
  266.  
  267. public static void main(String[] a) {
  268. JFrame frame = new JFrame("Sök Resa");
  269. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  270. frame.add(new Kvitto());
  271. frame.pack();
  272. frame.setVisible(true);
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement