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/world";
- // Database credentials
- static final String USER = "root";
- static final String PASS = "burger1605";
- //------------------------------- Metoder for DB-manipulering -------------------------------------
- public static void nyTrening(){
- }
- 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 ID, Name FROM city";
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()){
- //Retrieve by column name
- int ID = rs.getInt("ID");
- String Name = rs.getString("Name");
- //Display values
- System.out.print("ID: " + ID);
- System.out.println(", Name: " + Name);
- }
- }
- catch(SQLException se){
- //Handle errors for JDBC
- 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();
- }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