daily pastebin goal
39%
SHARE
TWEET

Untitled

a guest Sep 23rd, 2016 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package gui;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9. import java.text.SimpleDateFormat;
  10. import java.util.ArrayList;
  11. import java.util.Date;
  12. import java.util.Scanner;
  13.  
  14. import javafx.application.Application;
  15. import javafx.geometry.Insets;
  16. import javafx.scene.Scene;
  17. import javafx.scene.control.Button;
  18. import javafx.scene.control.Label;
  19. import javafx.scene.control.ListView;
  20. import javafx.scene.control.TextField;
  21. import javafx.scene.layout.GridPane;
  22. import javafx.stage.Stage;
  23.  
  24. public class MainApp extends Application
  25. {
  26.     public static void main(String[] args)
  27.     {
  28.     Application.launch(args);
  29.     }
  30.  
  31.     @Override
  32.     public void start(Stage stage)
  33.     {
  34.     stage.setTitle("SQL");
  35.     GridPane pane = new GridPane();
  36.     this.initContent(pane);
  37.  
  38.     Scene scene = new Scene(pane);
  39.     stage.setScene(scene);
  40.     stage.show();
  41.     }
  42.  
  43.     // -------------------------------------------------------------------------
  44.  
  45.     private TextField txfDato;
  46.     private ListView<String> lvwNames;
  47.     private Button btnLav, btnHent;
  48.  
  49.     private final Controller controller = new Controller();
  50.  
  51.     private void initContent(GridPane pane)
  52.     {
  53.     pane.setGridLinesVisible(false);
  54.     pane.setPadding(new Insets(20));
  55.     pane.setHgap(10);
  56.     pane.setVgap(10);
  57.  
  58.     Label lblName = new Label("Skriv og læs filer");
  59.     pane.add(lblName, 0, 0);
  60.  
  61.     txfDato = new TextField();
  62.     pane.add(txfDato, 1, 1, 2, 1);
  63.  
  64.     lvwNames = new ListView<String>();
  65.     pane.add(lvwNames, 0, 1, 1, 3);
  66.     lvwNames.setPrefWidth(200);
  67.     lvwNames.setPrefHeight(100);
  68.  
  69.     btnLav = new Button("Lav fil");
  70.     pane.add(btnLav, 2, 2);
  71.     btnLav.setOnAction(event -> this.controller.lavFilAction());
  72.  
  73.     btnHent = new Button("Hent fil");
  74.     pane.add(btnHent, 2, 3);
  75.     btnHent.setOnAction(event -> this.controller.hentAction());
  76.     }
  77.  
  78.     // -------------------------------------------------------------------------
  79.     // Button actions
  80.  
  81.     private class Controller
  82.     {
  83.     private ArrayList<String> names;
  84.  
  85.     public Controller()
  86.     {
  87.     }
  88.  
  89.     private void lavFilAction()
  90.     {
  91.         try
  92.         {
  93.         String timeStamp = txfDato.getText();
  94.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  95.         Date date = formatter.parse(timeStamp);
  96.  
  97.         PrintWriter printWriter = new PrintWriter(
  98.             "C:/SQLTempFiler/" + timeStamp + ".txt");
  99.  
  100.         Connection minConnection;
  101.         minConnection = DriverManager.getConnection(
  102.             "jdbc:sqlserver://DESKTOP-V3T8H49;databaseName=YodaFitness;user=sa;password=Jensskov;");
  103.  
  104.         Statement stmt = minConnection.createStatement();
  105.  
  106.         ResultSet res = stmt.executeQuery("exec MedlemmerHoldDato 'date'");
  107.         while (res.next())
  108.         {
  109.             printWriter.println(res.getString(1) + "    "
  110.                 + res.getString(2) + "\t" + res.getString(3));
  111.             printWriter.flush();
  112.         }
  113.         printWriter.close();
  114.         if (res != null)
  115.         {
  116.             res.close();
  117.         }
  118.         if (stmt != null)
  119.         {
  120.             stmt.close();
  121.         }
  122.         if (minConnection != null)
  123.         {
  124.             minConnection.close();
  125.         }
  126.         }
  127.         catch (Exception e)
  128.         {
  129.         System.out.println("fejl:  " + e.getMessage());
  130.         }
  131.     }
  132.  
  133.     private void hentAction()
  134.     {
  135.         names = new ArrayList<String>();
  136.  
  137.         try
  138.         {
  139.         String filenavn = txfDato.getText();
  140.         FileInputStream filein = new FileInputStream(
  141.             "C:/SQLTempFiler/" + filenavn + ".txt");
  142.  
  143.         Scanner scan = new Scanner(filein);
  144.         while (scan.hasNext())
  145.         {
  146.             String s = scan.nextLine();
  147.             names.add(s);
  148.         }
  149.         lvwNames.getItems().setAll(names);
  150.         }
  151.         catch (Exception e)
  152.         {
  153.         System.out.println("fejl:  " + e.getMessage());
  154.         }
  155.     }
  156.     }
  157. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top