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();
- 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 oktnr = rs.getInt("OktNr");
- System.out.println("Velykket opprettelse av treningsokt! OktNr: "+oktnr+"");
- }
- }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;
- 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.println("Luft: ");
- luft = input.nextLine();
- System.out.println("antall tilskuere: ");
- tilskuere = input.nextInt();
- break;
- //Om ute:
- }else if(inne.equals("ute")){
- System.out.println("Temperatur: ");
- temperatur = input.nextInt();
- System.out.println("Værtype: ");
- vartype = input.nextLine();
- break;
- //Om feil:
- }else{
- System.out.println("Feil format, prøv igjen!");
- continue;
- }
- }
- //Type trening:
- int belastning;
- int repetisjon;
- int sett;
- int km;
- int min;
- while (true){
- System.out.println("styrke eller utholdenhet(styrke/utholdenhet)? ");
- String inne = input.nextLine();
- //Om Styrke:
- if(inne.equals("inne")){
- System.out.println("Belastning: ");
- belastning = input.nextInt();
- System.out.println("Repetisjoner: ");
- repetisjon = input.nextInt();
- System.out.println("Sett: ");
- sett = input.nextInt();
- break;
- //Om Utholdenhet:
- }else if(inne.equals("ute")){
- System.out.println("Belastning: ");
- belastning = input.nextInt();
- System.out.println("Repetisjoner: ");
- repetisjon = input.nextInt();
- System.out.println("Sett: ");
- sett = input.nextInt();
- System.out.println("Km: ");
- km = input.nextInt();
- System.out.println("Min: ");
- min = input.nextInt();
- 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.nextLine();
- int malId;
- if(mal.equals("ja")){
- System.out.print("Dato(yyyymmdd): ");
- String malDato = input.nextLine();
- // insert into mal table
- //ta vare på malid ved sql spørring og bruk til når ovelse skal insertes
- }
- 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