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(yyyy-mm-dd): ");
- String dato = input.next();
- //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("Form(1 - 10) ");
- String form = input.next();
- //Prestasjon
- System.out.print("Prestasjon(1 - 10): ");
- String prestasjon = input.next();
- //notat
- System.out.print("Notat: ");
- String notat = input.next();
- try{
- String query = "insert into regcustomers (DEFAULT, dato, starttid, varighet, form, prestasjon, notat) values(?,?,?,?,?,?,?)";
- stmt.setString(1, "DEFAULT");
- stmt.setString(2,dato);
- stmt.setString(3,starttid);
- stmt.setString(4,varighet);
- stmt.setString(5,form);
- stmt.setString(6,prestasjon);
- stmt.setString(7,notat);
- //String sql = "INSERT INTO 'treningdb'.'treningsokt' ('OktNr`, `Dato`, `StartTid`, `Varighet`, `PersonligForm`, `Prestasjon`, `Notat`) VALUES (DEFAULT, `"+dato+"`, `"+starttid+"`, `"+varighet+"`, "+form+", "+form+", `"+notat+"`)";
- stmt.executeUpdate(sql);
- }catch(SQLException se){
- //Handle errors for JDBC
- System.out.println("nope");
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class
- e.printStackTrace();
- }
- }
- 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