Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package juna;
- import com.mysql.jdbc.Connection;
- import java.sql.DriverManager;
- import java.util.Properties;
- import java.sql.*;
- import javax.swing.JFrame;
- /**
- *
- * @author Mikko
- */
- public class Main {
- public static void main(String[] args) {
- // Tieto tie = new Tieto();
- try {
- // Vaihe 1: Ladataan JDBC-ajuri
- System.out.println("ripuli");
- String driver = "org.gjt.mm.mysql.Driver";
- Class.forName(driver).newInstance();
- // Vaihe 2: Määritellään yhteyden parametrit
- /* Properties props = new Properties();
- props.put("user", "mikkokoi");
- props.put("password", "8Fjkl12A");
- */
- // Vaihe 3: Avataan yhteys
- String serverName= "localhost:3306";
- String url = "jdbc:mysql://" + serverName + "/mikkokoi?user=mikkokoi&password=mikkokoi";
- Connection con = (Connection) DriverManager.getConnection(url);
- // Vaihe 4: Luodaan PreparedStatement, joka
- // sisältää tiedot hakevan SQL-kyselyn
- PreparedStatement s = con.prepareStatement("Select * From mikkokoi");
- // Vaihe 5: Suoritetaan kysely, tulokset
- // tallentuvat ResultSet-muuttujaan
- ResultSet rs = s.executeQuery();
- // Vaihe 6: Käydään kaikki kyselyn
- // palauttamat rivit
- // läpi ja tulostetaan rivien sisältö
- while (rs.next()) {
- System.out.println("nimi: " + rs.getString(1) + ", numero: " + rs.getString(2));
- }
- // Vaihe 7: Suljetaan lopuksi yhteydet
- rs.close();
- con.close();
- } catch (Exception e) {
- System.out.println("paskaa koodia");
- // e.printStackTrace();
- }
- run();
- // JFrame etu = Juna.luoIkkuna();
- }
- public static void run(){
- Juna juna = new Juna();
- juna.ekaIkkuna(/*etu,tie*/);
- }
- }
- package juna;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- /* @author LasseA */
- public class Kalenteri {
- public void Kalenteri(){
- }
- int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
- int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
- int pvm = java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH);
- int pvk = java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_WEEK);
- JLabel l = new JLabel("", JLabel.CENTER);
- public String day, title = "";
- JDialog d;
- int i=0;
- public JButton[] button = new JButton[49];
- public String maaritaTitle () {
- if (month == 0){
- title = "Tammikuu";
- } else if (month == 1) {
- title = "Helmikuu";
- }
- else if (month == 2) {
- title = "Maaliskuu";
- }
- else if (month == 3) {
- title = "Huhtikuu";
- }
- else if (month == 4) {
- title = "Toukokuu";
- }
- else if (month == 5) {
- title = "Kesäkuu";
- }
- else if (month == 6) {
- title = "Heinäkuu";
- }
- else if (month == 7) {
- title = "Elokuu";
- }
- else if (month == 8) {
- title = "Syyskuu";
- }
- else if (month == 9) {
- title = "Lokakuu";
- }
- else if (month == 10) {
- title = "Marraskuu";
- }
- else if (month == 11) {
- title = "Joulukuu";
- }else if (month == 12){
- year++;
- month = 0;
- title = "Tammikuu";
- }
- return title;
- }
- public Kalenteri (JFrame parent, final Tieto tie) {
- d = new JDialog();
- d.setModal(true);
- String[] header = { "Su", "Ma", "Ti", "Ke", "To", "Pe", "La" };
- JPanel p1 = new JPanel(new GridLayout(7, 7));
- p1.setPreferredSize(new Dimension(480, 120));
- //combokk.setSelectedIndex(4);
- //combokk.addActionListener(null);
- for (int x = 0; x < button.length; x++) {
- final int selection = x;
- for(i = 1; i<= pvm; i++){
- }
- button[x] = new JButton("" + pvm);
- button[x].setFocusPainted(false);
- button[x].setBackground(Color.white);
- if (x > 6)
- button[x].addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- day = button[selection].getActionCommand();
- tie.setPvm(day);
- d.dispose();
- }
- });
- if (x < 7) {
- button[x].setText(header[x]);
- button[x].setForeground(Color.red);
- }
- p1.add(button[x]);
- }
- JPanel p2 = new JPanel(new GridLayout(1, 3));
- JButton previous = new JButton("<< Edellinen kk");
- previous.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent ae) {
- //Jos mennään edelliseen vuoteen, tarvitaan siihen if lause joka tarkistaa napin painalluksessa
- if(month == 0){
- month = 12;
- year--;
- }
- month--;
- maaritaTitle();
- d.setTitle(title);
- }
- });
- p2.add(previous);
- p2.add(l);
- JButton next = new JButton("Seuraava kk >>");
- next.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- month++;
- maaritaTitle();
- d.setTitle(title);
- // System.out.println(title);
- }
- });
- p2.add(next);
- d.add(p1, BorderLayout.CENTER);
- d.add(p2, BorderLayout.SOUTH);
- d.pack();
- d.setLocationRelativeTo(parent);
- maaritaTitle();
- d.setTitle(title);
- d.setVisible(true);
- }
- /*public static void main (String [] args) {
- Kalenteri kalu = new Kalenteri(null);
- }*/
- }
- package juna;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.Graphics;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.GridLayout;
- import java.awt.Image;
- import java.awt.Insets;
- import java.awt.MediaTracker;
- import java.awt.Toolkit;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Properties;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.*;
- import javax.swing.KeyStroke;
- import java.util.*;
- /**
- *
- * @author Mikko
- */
- public class Juna {
- public void Juna(){}
- /**
- * LUODAAN MAIN METODISSA TIETOKANTAYHTEYS SEKÄ KUTSUTAAN IKKUNAA
- */
- /* @SuppressWarnings("CallToThreadDumpStack")
- public static void main(String[] args) {
- Tieto tie = new Tieto();
- try {
- // Vaihe 1: Ladataan JDBC-ajuri
- System.out.println("ripuli");
- String driver = "org.apache.derby.jdbc.ClientDriver";
- Class.forName(driver).newInstance();
- // Vaihe 2: Määritellään yhteyden parametrit
- Properties props = new Properties();
- props.put("user", "test");
- props.put("password", "test");
- // Vaihe 3: Avataan yhteys
- String url = "jdbc:derby://localhost:1527/Juna";
- Connection conn = DriverManager.getConnection(url , props);
- // Vaihe 4: Luodaan PreparedStatement, joka
- // sisältää tiedot hakevan SQL-kyselyn
- PreparedStatement s = conn.prepareStatement("Select nimi, numero from APP.VAUNU");
- // Vaihe 5: Suoritetaan kysely, tulokset
- // tallentuvat ResultSet-muuttujaan
- ResultSet rs = s.executeQuery();
- // Vaihe 6: Käydään kaikki kyselyn
- // palauttamat rivit
- // läpi ja tulostetaan rivien sisältö
- while (rs.next()) {
- System.out.println("nimi: " + rs.getString(1) + ", numero: " + rs.getString(2));
- }
- // Vaihe 7: Suljetaan lopuksi yhteydet
- rs.close();
- conn.close();
- } catch (Exception e) {
- System.out.println("paskaa koodia");
- e.printStackTrace();
- }
- JFrame etu =luoIkkuna();
- ekaIkkuna(etu,tie);
- }*/
- public JFrame luoIkkuna(){
- JFrame etu = new JFrame("Junanvaraus");
- //MEnun teko
- JMenuBar bar = new JMenuBar();
- JMenu menu = new JMenu("File");
- bar.add(menu);
- JMenuItem nappi = new JMenuItem("help");
- JMenuItem nappi2 = new JMenuItem("exit");
- menu.add(nappi);
- menu.add(new JSeparator()); // SEPARATOR
- menu.add(nappi2);
- etu.setJMenuBar(bar);
- /*
- *
- *
- *
- *
- *
- Tehdään actionlistenerit suorittamaan nappuloista tapahtuvia toimintoja mm menuvalikoista.
- */ etu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- ActionListener actionListener = new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent actionEvent){
- System.out.println("nappia painettu");
- System.exit(0);
- }
- };
- ActionListener actionListener3 = new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent actionEvent){
- kaynnistaHelp();
- }
- };
- nappi.addActionListener(actionListener3);
- nappi2.addActionListener(actionListener);
- //säädetään ikkunan kokoa ja asetetaan ikkuna näkyväksi
- etu.setSize(750,500);
- etu.setVisible(true);
- return etu;
- }
- /*
- Tehdään help toiminto MUISTA KIRJOTTAA TUO TEKSTI SINNE JOSSAIN VÄLISSÄ
- */
- public void kaynnistaHelp(){
- String msg = "<html>KIRJOTA TÄHÄN helppi<br>BR VAIHTAA RIVIÄ";
- JOptionPane optionPane = new NarrowOptionPane();
- optionPane.setMessage(msg);
- optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
- JDialog dialog = optionPane.createDialog(null, "HELP");
- dialog.setVisible(true);
- }
- public void ekaIkkuna(/*final JFrame etu,final Tieto tie*/){
- final Tieto tie = new Tieto();
- final JFrame etu = luoIkkuna();
- JButton b1 = new JButton("kemi-oulu");
- JButton b2 = new JButton("Oulu-Kemi");
- JButton b3 = new JButton("Oulu-Kempele");
- JButton b4 = new JButton("Kempele-Oulu");
- JButton b5 = new JButton("Jatka");
- JButton b6 = new JButton ("Peruuta");
- // b1.setPreferredSize(new Dimension(20,30));
- /*etu.setLayout(null);
- etu.setBounds(50, 50, 300, 300);*/
- // Container content = etu.getContentPane();
- // content.setLayout(new GridBagLayout());
- JPanel napit = new JPanel(new GridBagLayout());
- etu.getContentPane().add(napit, BorderLayout.NORTH);
- GridBagConstraints grid = new GridBagConstraints();
- napit.setBackground(Color.BLUE);
- grid.fill = GridBagConstraints.HORIZONTAL;
- grid.gridwidth = 1;
- grid.gridheight = 1;
- grid.weightx = 0;
- grid.insets = new Insets(10,10,10,10);
- grid.fill = GridBagConstraints.HORIZONTAL;
- grid.gridx = 5;
- grid.gridy = 1;
- napit.add(b1, grid);
- grid.fill = GridBagConstraints.HORIZONTAL;
- grid.weightx = 0;
- grid.gridx = 5;
- grid.gridy = 2;
- napit.add(b2, grid);
- grid.fill = GridBagConstraints.HORIZONTAL;
- grid.weightx = 0;
- grid.gridx = 5;
- grid.gridy =3;
- napit.add(b3, grid);
- grid.fill = GridBagConstraints.HORIZONTAL;
- grid.weightx = 0;
- grid.gridx = 5;
- grid.gridy = 4;
- napit.add(b4, grid);
- JPanel apupane = new JPanel(new GridBagLayout());
- etu.getContentPane().add(apupane, BorderLayout.LINE_END);
- apupane.setBackground(Color.PINK);
- grid.weightx =0.5;
- grid.weighty = 1;
- grid.gridx = 3;
- grid.gridy = 1;
- grid.anchor = GridBagConstraints.LAST_LINE_END;
- apupane.add(b5, grid);
- //grid.fill = GridBagConstraints.HORIZONTAL;
- // grid.fill = GridBagConstraints.HORIZONTAL;
- grid.weightx = 0.5;
- grid.gridx = 2;
- grid.gridy = 1;
- apupane.add(b6,grid);
- ActionListener actionListener2 = new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent actionEvent){
- System.out.println("nappia painettu");
- TokaIkkuna toka = new TokaIkkuna();
- etu.setVisible(false);
- toka.teeKentat(luoIkkuna(),tie);
- }
- };
- ActionListener actionListener4 = new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent actionEvent){
- String kohde = "kemi-Oulu";
- tie.setKohde(kohde);
- tie.getKohde();
- Kalenteri kal = new Kalenteri(null, tie);
- tie.getPvm();
- }
- };
- ActionListener actionListener5 = new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent actionEvent){
- String kohde = "Oulu-Kemi";
- tie.setKohde(kohde);
- tie.getKohde();
- Kalenteri kal = new Kalenteri(null,tie);
- tie.getPvm();
- }
- };
- ActionListener actionListener6 = new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent actionEvent){
- String kohde = "Oulu-Kempele";
- tie.setKohde(kohde);
- tie.getKohde();
- Kalenteri kal = new Kalenteri(null,tie);
- tie.getPvm();
- }
- };
- ActionListener actionListener7 = new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent actionEvent){
- String kohde = "Kempele-Oulu";
- tie.setKohde(kohde);
- tie.getKohde();
- Kalenteri kal = new Kalenteri(null,tie);
- tie.getPvm();
- }
- };
- //liitetään actionlistenerit nappiin
- b1.addActionListener(actionListener4);
- b2.addActionListener(actionListener5);
- b3.addActionListener(actionListener6);
- b4.addActionListener(actionListener7);
- b5.addActionListener(actionListener2);
- }
- }
- class NarrowOptionPane extends JOptionPane {
- NarrowOptionPane() {
- }
- }
- package juna;
- import java.io.*;
- import javax.swing.*;
- import javax.swing.JOptionPane;
- import java.awt.Component;
- import java.awt.*;
- import java.awt.event.*;
- public class TokaIkkuna{
- public void TokaIkkuna(){}
- public void teeKentat( final JFrame etu, final Tieto tie){
- //Luodaan napit ja asetetaan gridbaglayout paneen.
- JButton button, button2;
- final JPanel pane = new JPanel();
- JPanel pane2 = new JPanel();
- JPanel pane3 = new JPanel();
- JPanel pane4 = new JPanel();
- pane4.setLayout(new GridBagLayout());
- pane3.setLayout(new GridBagLayout());
- pane2.setLayout(new GridBagLayout());
- pane.setLayout(new GridBagLayout());
- pane.setBackground(Color.blue);
- pane4.setBackground(Color.cyan);
- pane2.setBackground(Color.red);
- pane3.setBackground(Color.green);
- GridBagConstraints c = new GridBagConstraints();
- etu.add(pane, BorderLayout.SOUTH);
- etu.add(pane2, BorderLayout.CENTER);
- etu.add(pane3, BorderLayout.LINE_START);
- etu.add(pane4, BorderLayout.PAGE_START);
- button = new JButton("Jatka");
- c.weighty = 1;
- c.weightx = 3;
- c.gridx = 3;
- c.gridy = 1;
- c.anchor = GridBagConstraints.LAST_LINE_END;
- pane.add(button, c);
- button2 = new JButton("Takaisin");
- c.weighty = 1;
- c.weightx = 210;
- c.gridheight = 1;
- c.gridx = 2;
- c.gridy = 1;
- pane.add(button2, c);
- //*****************************************************************************
- c.gridwidth = 0;
- ///LUODAAN TEKSTIKENT�T JA NIIDEN LABELIT
- Tieto tiet = new Tieto();
- final JTextField tekstiKentta = new JTextField(tiet.getNimi(),20);
- JLabel label1 = new JLabel ("Nimi: ");
- label1.setLabelFor(tekstiKentta);
- label1.setDisplayedMnemonic(KeyEvent.VK_N);
- c.insets= new Insets(5,5,5,5);
- c.weightx= 0;
- c.weighty = 0;
- c.gridx = 1;
- c.gridy = 0;
- pane2.add(tekstiKentta, c);
- c.weighty= 0;
- c.weightx = 0;
- c.gridx = 0;
- c.gridy = 0;
- pane2.add(label1, c);
- final JTextField tekstiKentta2 = new JTextField(tiet.getPuh(),20);
- JLabel label2 = new JLabel ("Osoite: ");
- label2.setDisplayedMnemonic(KeyEvent.VK_N);
- label2.setLabelFor(tekstiKentta2);
- tekstiKentta2.setHorizontalAlignment(JTextField.CENTER);
- c.weighty = 0;
- c.weightx = 0;
- c.gridx = 1;
- c.gridy = 1;
- pane2.add(tekstiKentta2, c);
- c.weighty = 0;
- c.weightx = 0;
- c.gridx =0;
- c.gridy = 1;
- pane2.add(label2, c);
- final JTextField tekstiKentta3 = new JTextField(tiet.getOsote(),20);
- JLabel label3 = new JLabel ("PUH: ");
- label3.setDisplayedMnemonic(KeyEvent.VK_N);
- label3.setLabelFor(tekstiKentta3);
- c.weighty = 0;
- c.weightx = 0;
- c.gridx=0;
- c.gridy=2;
- pane2.add(label3, c);
- c.weighty = 0;
- c.weightx = 0;
- c.gridx = 1;
- c.gridy = 2;
- // pane2.add(label3, c);
- pane2.add(tekstiKentta3, c);
- c.weighty = 0;
- c.weightx = 0;
- c.gridx = 0;
- c.gridy = 0;
- //Actionlistenerit jatka ja takaisin nappeihin
- ActionListener actionListener1 = new ActionListener(){
- public void actionPerformed(ActionEvent actionEvent){
- String nimi = tekstiKentta.getText();
- String osoite = tekstiKentta2.getText();
- String puh = tekstiKentta3.getText();
- //Tsekataan jos on sy�tetty nimeen numeroita tai puhelinnumeroon kirjaimia, errorit sen mukaan jos tarpeellista.
- for (int i=0; i<nimi.length();i++){
- if(Character.isDigit(nimi.charAt(i))){
- JOptionPane.showMessageDialog(pane, "�l� pist� numeroita nimees!");
- break;
- }
- }
- for (int i=0; i<puh.length();i++){
- if(Character.isLetter(puh.charAt(i))){
- JOptionPane.showMessageDialog(pane, "Puhelinnumeroon sallitaan vain numeroita :-)");
- break;
- }
- }
- System.out.println(nimi + osoite + puh);
- KolmasIkkuna kolmas = new KolmasIkkuna();
- etu.setVisible(false);
- Juna juna = new Juna();
- kolmas.kolmas(juna.luoIkkuna(),tie);
- //T�h�n viel� siirtym�, nimi osoite ja puh sis�lt�v�t tiedot.
- }
- };
- ActionListener actionListener2 = new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent actionEvent){
- System.out.println("Takaisin painettu");
- //T�h�n mit� tapahtuu ku painaa takaisin
- etu.setVisible(false);
- Juna juna = new Juna();
- juna.ekaIkkuna();
- }
- };
- //Jos klikkaa tekstikentt�� niin l�htee ohjeteksti pois
- tekstiKentta.addMouseListener(new java.awt.event.MouseAdapter(){
- @Override
- public void mouseClicked(java.awt.event.MouseEvent e) {
- tekstiKentta.setText("");
- }
- });
- tekstiKentta2.addMouseListener(new java.awt.event.MouseAdapter(){
- @Override
- public void mouseClicked(java.awt.event.MouseEvent e){
- tekstiKentta2.setText("");
- }
- });
- tekstiKentta3.addMouseListener(new java.awt.event.MouseAdapter(){
- public void mouseClicked(java.awt.event.MouseEvent e){
- tekstiKentta3.setText("");
- }
- });
- //Lis�t��n napeille actionlistenerit
- button.addActionListener(actionListener1);
- button2.addActionListener(actionListener2);
- pane.setVisible(true);
- }
- }
- package juna;
- import javax.swing.JFrame;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- /**
- *
- * @author Mikko
- */
- class KolmasIkkuna {
- public void KolmaIkkuna(){}
- public void kolmas(JFrame etu, Tieto tie){
- JPanel pane= new JPanel(new BorderLayout());
- etu.add(pane, BorderLayout.CENTER);
- JTextArea are = new JTextArea( tie.getNimi(),10,10);
- pane.add(are, BorderLayout.NORTH);
- pane.setBackground(Color.blue);
- alanapit(etu,tie);
- }
- public void alanapit(final JFrame etu, final Tieto tie ){
- JButton b5 = new JButton("Hyväksy");
- JButton b6 = new JButton ("Peruuta");
- JPanel napit = new JPanel(new GridBagLayout());
- GridBagConstraints grid = new GridBagConstraints();
- JPanel apupane = new JPanel(new GridBagLayout());
- etu.getContentPane().add(apupane, BorderLayout.LINE_END);
- apupane.setBackground(Color.PINK);
- grid.weightx =0.5;
- grid.weighty = 1;
- grid.gridx = 3;
- grid.gridy = 1;
- grid.anchor = GridBagConstraints.LAST_LINE_END;
- apupane.add(b5, grid);
- //grid.fill = GridBagConstraints.HORIZONTAL;
- // grid.fill = GridBagConstraints.HORIZONTAL;
- grid.weightx = 0.5;
- grid.gridx = 2;
- grid.gridy = 1;
- apupane.add(b6,grid);
- tie.getKohde();
- tie.getNimi();
- tie.getOsote();
- tie.getPuh();
- tie.getPvm();
- ActionListener actionListener2 = new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent actionEvent){
- System.out.println("nappia painettu");
- Juna juna = new Juna();
- TokaIkkuna toka = new TokaIkkuna();
- etu.setVisible(false);
- toka.teeKentat(juna.luoIkkuna(),tie);
- }
- };
- ActionListener actionListener1 = new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent actionEvent){
- System.out.println("nappia painettu");
- Juna juna = new Juna();
- TokaIkkuna toka = new TokaIkkuna();
- etu.setVisible(false);
- toka.teeKentat(juna.luoIkkuna(),tie);
- }
- };
- b6.addActionListener(actionListener2);
- b5.addActionListener(actionListener1);
- }
- }
- package juna;
- /**
- *
- * @author Mikko
- */
- public class Tieto {
- public void Tieto(){}
- public String kohde=null;
- public String nimi=null;
- public String pvm=null;
- public String osoite=null;
- public String puh=null;
- public void setKohde(String koh){
- this.kohde = koh;
- }
- public String getKohde(){
- System.out.println(kohde);
- return kohde;
- }
- public void setNimi(String nimi){
- this.nimi=nimi;
- }
- public String getNimi(){
- return nimi;
- }
- public void setPvm(String pvm){
- this.pvm=pvm;
- }
- public String getPvm(){
- System.out.println(pvm);
- return pvm;
- }
- public void setOsote(String osote){
- this.osoite=osote;
- }
- public String getOsote(){
- return osoite;
- }
- public void setPuh(String puh){
- this.puh=puh;
- }
- public String getPuh(){
- return puh;
- }
- }
Add Comment
Please, Sign In to add comment