Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //STEP 1. Import required packages
- 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/EMP";
- // Database credentials
- static final String USER = "root";
- static final String PASS = "password";
- //------------------------------- Metoder for DB-manipulering -------------------------------------
- public static void nyTrening(Connection conn, Statement stmt){
- //STEP 4: Execute a query
- try{
- System.out.println("Creating statement...");
- stmt = conn.createStatement();
- String sql;
- //Creates a new entry in the Database
- sql = "SELECT id, first, last, age FROM Employees"; //Erstatt med mulig trenings-entry
- ResultSet rs = stmt.executeQuery(sql);
- }
- catch(SQLException se){
- //Handle errors for JDBC
- se.printStackTrace();
- }
- }
- public static void hentLogg(Resultset rs){
- //STEP 5: Extract data from result set
- try{
- //Retrieve by column name
- //Sett var Value + Print for hver verdi som skal hentes ut.
- int id = rs.getInt("id");
- int age = rs.getInt("age");
- String first = rs.getString("first");
- String last = rs.getString("last");
- //Display values
- System.out.print("ID: " + id);
- System.out.print(", Age: " + age);
- System.out.print(", First: " + first);
- System.out.println(", Last: " + last);
- }
- catch(SQLException se){
- //Handle errors for JDBC
- se.printStackTrace();
- }
- }
- public static void hentBeste(){
- }
- // + Mange flere metoder ..
- public static void main(String[] args) {
- //------------------------------ Connection-Oppsett -------------------------------------------
- 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, avslutt): ");
- handling = input.next();
- if (handling == "ny"){
- nyTrening(conn, stmt);
- } else if(handling == "hent"){
- hentLogg(rs);
- }else if(handling == "avslutt"){
- break;
- }else{
- System.out.println("Feil. Skriv inn enten 'ny' eller 'hent'");
- continue;
- }
- }
- // --------------------------- Clean-up / End--------------------------------------
- input.close();
- rs.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