Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SearchTripTest extends JPanel {
- private JTextArea taOutput = new JTextArea();
- private JLabel lblDest = new JLabel("Destination");
- private JLabel lblDatum = new JLabel("Datum");
- private JTextField tfAvgang = new JTextField("Malmö");
- private JTextField tfDest = new JTextField("Madrid");
- private JTextField tfDatum = new JTextField("24/5");
- private JButton btnSearch = new JButton("Sök");
- private JButton btnReg = new JButton("Registrera");
- private ResultSet myRs;
- public SearchTripTest() {
- setPreferredSize(new Dimension(600, 600));
- setLayout(null);
- taOutput.setLocation(50, 50);
- taOutput.setSize(400, 330);
- add(taOutput);
- lblDest.setLocation(55, 385);
- lblDest.setSize(80, 50);
- add(lblDest);
- tfDest.setLocation(50, 420);
- tfDest.setSize(400, 50);
- add(tfDest);
- lblDatum.setLocation(55, 465);
- lblDatum.setSize(80, 50);
- add(lblDatum);
- tfDatum.setLocation(50, 500);
- tfDatum.setSize(400, 50);
- add(tfDatum);
- btnSearch.setLocation(500, 500);
- btnSearch.setSize(70, 50);
- add(btnSearch);
- btnReg.setLocation(500, 450);
- btnReg.setSize(70, 50);
- add(btnReg);
- createListeners();
- writeData();
- }
- public void createListeners() {
- Listener list = new Listener();
- btnSearch.addActionListener(list);
- btnReg.addActionListener(list);
- }
- public void writeData() {
- try {
- // 1 Get a connection to database
- Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Name", "User", "pw");
- // 2. Create a statement
- Statement myStmt = myConn.createStatement();
- // 3. Execute SQL query
- myRs = myStmt.executeQuery("select * from stad");
- // 5. Create so write all elements
- ResultSetMetaData rsmd = myRs.getMetaData();
- int columnsNumber = rsmd.getColumnCount();
- // 4. Process the result set
- while (myRs.next()) {
- for (int i = 1; i <= columnsNumber; i++) {
- taOutput.setText(myRs.getString("land") + ", " + myRs.getString("språk") + ", "
- + myRs.getString("valuta") + ", " + myRs.getString("stad"));
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private class Listener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnSearch) {
- } else if (e.getSource() == btnReg) {
- }
- }
- }
- public static void main(String[] a) {
- JFrame frame = new JFrame("Sök Resa");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(new SearchTripTest());
- frame.pack();
- frame.setVisible(true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement