Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package database;
- import java.sql.*;
- import java.util.Scanner;
- public class Treningslogg {
- //--------------------------------- Definere Variabler -------------------------------------------
- // JDBC driver name and database URL
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://localhost/treningdb";
- // Database credentials
- static final String USER = "root";
- static final String PASS = "burger1605";
- //------------------------------- Metoder for DB-manipulering -------------------------------------
- public static void nyTrening(Connection conn, Statement stmt){
- Scanner input = new Scanner(System.in);
- //Dato
- System.out.print("Dato for treningsøkt(yyymmdd): ");
- String dato = input.nextLine();
- //starttid
- System.out.print("Starttid(hh:mm:ss): ");
- String starttid = input.next();
- //Varighet
- System.out.print("Varighet(hh:mm:ss): ");
- String varighet = input.next();
- //form
- System.out.print("Personligform(1 - 10) ");
- int form = input.nextInt();
- //Prestasjon
- System.out.print("Prestasjon(1 - 10): ");
- int prestasjon = input.nextInt();
- //notat
- System.out.print("Notat: ");
- String notat = input.next();
- Integer oktnr = null;
- try{
- PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `treningsokt` (Dato,Starttid,Varighet,PersonligForm,Prestasjon,Notat) VALUE (?,?,?,?,?,?)");
- pstmt.setString(1, dato);
- pstmt.setString(2, starttid);
- pstmt.setString(3,varighet);
- pstmt.setInt(4,form );
- pstmt.setInt(5,prestasjon );
- pstmt.setString(6,notat);
- pstmt.executeUpdate();
- stmt = conn.createStatement();
- String sql;
- sql = "SELECT OktNr FROM treningsokt WHERE Dato = "+dato+"";
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()){
- //Retrieve by column name
- int oktnr1 = rs.getInt("OktNr");
- System.out.println("Velykket opprettelse av treningsokt! OktNr: "+oktnr1+"");
- oktnr = oktnr1;
- }
- }catch(SQLException se){
- //Handle errors for JDBC
- System.out.println("nope");
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class
- e.printStackTrace();
- }
- String handling = input.nextLine();
- // --------------------------------------------------------------------
- //NY ØVELSE -----------------------------------------------------------
- while(!handling.equals("ferdig")){
- //Øktnavn
- System.out.print("Øktnavn: ");
- String navn = input.nextLine();
- //ØktBeskrivelse
- System.out.print("Beskrivelse: ");
- String beskrivelse = input.nextLine();
- //Inne / Ute -----------------------------------------------------
- String luft;
- Integer id2 = null;
- int tilskuere;
- int temperatur;
- String vartype;
- while (true){
- System.out.println("Inne eller ute(inne/ute)? ");
- String inne = input.nextLine();
- //Om inne:
- if(inne.equals("inne")){
- System.out.print("ID2: ");
- id2 = input.nextInt();
- System.out.print("Luft: ");
- luft = input.next();
- System.out.print("antall tilskuere: ");
- tilskuere = input.nextInt();
- try{
- PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `innendors` (ID2,Luft,Tilskuere) VALUE (?,?,?)");
- pstmt.setInt(1, id2);
- pstmt.setString(2, luft);
- pstmt.setInt(3,tilskuere);
- pstmt.executeUpdate();
- }catch(SQLException se){
- //Handle errors for JDBC
- System.out.println("nope");
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class
- e.printStackTrace();
- }
- break;
- //Om ute:
- }else if(inne.equals("ute")){
- System.out.println("ID2: ");
- id2 = input.nextInt();
- System.out.println("Temperatur: ");
- temperatur = input.nextInt();
- System.out.println("Værtype: ");
- vartype = input.next();
- try{
- PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `utendors` (ID2,Luft,Tilskuere) VALUE (?,?,?)");
- pstmt.setInt(1, id2);
- pstmt.setInt(2, temperatur);
- pstmt.setString(3,vartype);
- pstmt.executeUpdate();
- }catch(SQLException se){
- //Handle errors for JDBC
- System.out.println("nope");
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class
- e.printStackTrace();
- }
- break;
- //Om feil:
- }else{
- System.out.println("Feil format, prøv igjen!");
- continue;
- }
- }
- //Type trening: -------------------------------------------------------------------------
- int belastning;
- int repitisjon;
- int sett;
- int km;
- int min;
- Integer id1 = null;
- while (true){
- System.out.print("styrke eller utholdenhet(styrke/utholdenhet)? ");
- String styrke = input.next();
- //Om Styrke:
- if(styrke.equals("styrke")){
- System.out.print("ID1: ");
- id1 = input.nextInt();
- System.out.print("Belastning: ");
- belastning = input.nextInt();
- System.out.print("Repetisjoner: ");
- repitisjon = input.nextInt();
- System.out.print("Sett: ");
- sett = input.nextInt();
- try{
- PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `styrke` (ID1,Belastning,Repitisjon,Sett) VALUE (?,?,?,?)");
- pstmt.setInt(1, id1);
- pstmt.setInt(2, belastning);
- pstmt.setInt(3,repitisjon);
- pstmt.setInt(4,sett);
- pstmt.executeUpdate();
- }catch(SQLException se){
- //Handle errors for JDBC
- System.out.println("nope");
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class
- e.printStackTrace();
- }
- break;
- //Om Utholdenhet:
- }else if(styrke.equals("utholdenhet")){
- System.out.print("Belastning: ");
- belastning = input.nextInt();
- System.out.print("Repetisjoner: ");
- repitisjon = input.nextInt();
- System.out.print("Sett: ");
- sett = input.nextInt();
- System.out.print("Km: ");
- km = input.nextInt();
- System.out.print("Min: ");
- min = input.nextInt();
- try{
- PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `utholdenhet` (ID1,Belastning,Repitisjoner,Sett,Km,Min) VALUE (?,?,?,?,?,?)");
- pstmt.setInt(1, id2);
- pstmt.setInt(2, belastning);
- pstmt.setInt(3,repitisjon);
- pstmt.setInt(4,sett);
- pstmt.setInt(5,km);
- pstmt.setInt(6,min);
- pstmt.executeUpdate();
- }catch(SQLException se){
- //Handle errors for JDBC
- System.out.println("nope");
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class
- e.printStackTrace();
- }
- break;
- //Om feil:
- }else{
- System.out.println("Feil format, prøv igjen!");
- continue;
- }
- }
- //Mål eller ikke mål-----------------------------------------------------------------------
- System.out.print("Er øvelsen et mål(ja/nai)?");
- String mal = input.next();
- Integer malId = null;
- if(mal.equals("ja")){
- System.out.print("Dato(yyyymmdd): ");
- String malDato = input.next();
- try{
- PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `mal` (Dato) VALUE (?)");
- pstmt.setString(1, malDato);
- pstmt.executeUpdate();
- }catch(SQLException se){
- //Handle errors for JDBC
- System.out.println("nope");
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class
- e.printStackTrace();
- }
- try {
- stmt = conn.createStatement();
- String sql;
- sql = "SELECT LAST(MalID) FROM mal";
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()){
- //Retrieve by column name
- int malId1 = rs.getInt("MalID");
- malId = malId1;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- // insert into mal table
- //ta vare på malid ved sql spørring og bruk til når ovelse skal insertes
- }
- // sql for ovelse ------------------------------------------------------------------------
- try{
- PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `ovelse` (Navn,OktNr,Beskrivelse,ID1,ID2,MalID) VALUE (?,?,?,?,?,?)");
- pstmt.setString(1, navn);
- pstmt.setInt(2, oktnr);
- pstmt.setString(3,beskrivelse);
- pstmt.setInt(4,id1);
- pstmt.setInt(5,id2);
- pstmt.setInt(6,malId);
- pstmt.executeUpdate();
- }catch(SQLException se){
- //Handle errors for JDBC
- System.out.println("nope");
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class
- e.printStackTrace();
- }
- //Bunn av koden
- handling = input.nextLine();
- }
- input.close();
- }
- public static void hentLogg(Connection conn, Statement stmt){
- //STEP 4: Execute a query
- try{
- System.out.println("Creating statement...");
- stmt = conn.createStatement();
- String sql;
- sql = "SELECT Navn, OktNr FROM ovelse WHERE OktNr = '1'";
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()){
- //Retrieve by column name
- String navn = rs.getString("Navn");
- int oktnr = rs.getInt("OktNr");
- //Display values
- System.out.print("navn: " + navn);
- System.out.println(", oktnr: " + oktnr);
- }
- System.out.println("Skipped while");
- }
- catch(SQLException se){
- //Handle errors for JDBC
- System.out.println("nope");
- se.printStackTrace();
- }
- }
- public static void hentBeste(){
- }
- // + Mange flere metoder ..
- //--------------------------------------MAIN---------------------------------------------------
- //------------------------------ Connection-Oppsett -------------------------------------------
- public static void main(String[] args) {
- Connection conn = null;
- Statement stmt = null;
- try{
- //STEP 2: Register JDBC driver
- Class.forName("com.mysql.jdbc.Driver");
- //STEP 3: Open a connection
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL,USER,PASS); //Connects with DB(URL, Name & Password)
- //---------------------------------- Logic(for tekst-UI) ------------------------------------
- //Lag loop som konstant spør om hva som skal gjøres + Text-input konstant
- //Kjører riktig funksjon med riktige parametere avhengig av input
- Scanner input = new Scanner(System.in);
- String handling;
- while (true){
- System.out.print("Skriv inn handling(ny/hent/beste/avslutt): ");
- handling = input.next();
- if (handling.equals("ny")){
- nyTrening(conn, stmt);
- }else if(handling.equals("hent")){
- hentLogg(conn, stmt);
- }else if(handling.equals("beste")){
- hentBeste();
- }else if(handling.equals("avslutt")){
- break;
- }else{
- System.out.println("Feil. Skriv inn 'ny, 'hent, 'beste' eller 'avslutt'");
- continue;
- }
- }
- // --------------------------- Clean-up / End--------------------------------------
- input.close();
- stmt.close();
- conn.close();
- }catch(SQLException se){
- //Handle errors for JDBC
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class.forName
- e.printStackTrace();
- }finally{
- //finally block used to close resources
- try{
- if(stmt!=null)
- stmt.close();
- }catch(SQLException se2){
- }// nothing we can do
- try{
- if(conn!=null)
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }//end finally try
- }//end try
- System.out.println("So long, Suckers!");
- }//End Main
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement