Advertisement
Guest User

Untitled

a guest
Apr 4th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.01 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7.  
  8. package databaseproject;
  9.  
  10.    
  11. import java.sql.*;
  12. import java.util.Scanner;
  13. import java.util.logging.*;
  14.  
  15. // java -cp postgresql-9.4-1201.jdbc4.jar:. DBtest
  16.  
  17. /**
  18.  *
  19.  * @author jonaspedersen
  20.  */
  21. public class DatabaseProject {
  22.  
  23.     /**
  24.      * @param args the command line arguments
  25.      */
  26.    
  27.     static String query;
  28.     static Connection con = null;
  29.    
  30.     public static void main(String[] args) {
  31.         String url = "jdbc:postgresql://localhost:5432/postgres";
  32.         String user = "postgres";
  33.         String password = "Snuden123";
  34.  
  35.         //CONNECTING
  36.         try {
  37.             con = DriverManager.getConnection(url, user, password);
  38.  
  39.         } catch (SQLException ex) {
  40.                 Logger lgr = Logger.getLogger(DatabaseProject.class.getName());
  41.                 lgr.log(Level.WARNING, ex.getMessage(), ex);
  42.  
  43.         }
  44.  
  45.         databaseConnection(con);
  46.     }
  47.        
  48.         public static void databaseConnection(Connection con){
  49.         //RUN AN EXAMPLE QUERY            
  50.                 selectComponents(con);
  51.                 pcSystemList(con);
  52.         }
  53.  
  54.        
  55.         public static void selectComponents(Connection con){
  56.            
  57.                 query = "SELECT component.name, component.quantity FROM component";
  58.                 Scanner sc = new Scanner(System.in);
  59.                 int choice;
  60.                
  61.                 System.out.println("0: Gå videre til næste mulighed    1: Liste af alle komponenter og deres antal på lager");
  62.                 choice = sc.nextInt();
  63.                
  64.                 if(choice == 0){
  65.                    
  66.                 } else if (choice == 1){
  67.                     try {
  68.                         PreparedStatement st = con.prepareStatement(query);
  69.                 ResultSet rs = st.executeQuery();
  70.                 while (rs.next()) {
  71.                             System.out.println(rs.getString(1) + "  |  " +  rs.getString(2));
  72.                 }
  73.                        
  74.                        
  75.                     } catch (SQLException e) {
  76.             e.printStackTrace();
  77.         }
  78.                      
  79.                    
  80.                 } else {
  81.                     System.out.println("Du har ikke indtastet et gyldigt tal, prøv igen \n");
  82.                     selectComponents(con);
  83.                 }
  84.  
  85.         }
  86.        
  87.         public static void pcSystemList(Connection con){
  88.            
  89.                 query = "SELECT component.id, component.quantity, computer_system.* FROM component, computer_system WHERE component.id = computer_system.cpu OR component.id = computer_system.ram OR component.id = computer_system.mainboard OR component.id = computer_system.pc_case OR component.id =  computer_system.gpu";
  90.                 //query = "SELECT name FROM computer_system";
  91.                 Scanner sc = new Scanner(System.in);
  92.                 int choice;
  93.                
  94.                 System.out.println("0: Gå videre til næste mulighed    1: Liste af alle computersystemer og hvor mange af hver som kan bygges");
  95.                 choice = sc.nextInt();
  96.                
  97.                 if(choice == 0){
  98.                    
  99.                 } else if (choice == 1){
  100.                     try {
  101.                         PreparedStatement st = con.prepareStatement(query);
  102.                         ResultSet rs = st.executeQuery();
  103.                         while (rs.next()){
  104.                            
  105.                             System.out.println("ID:    " + rs.getString(1) + "     antal:     " + rs.getString(2) + "     computersystem:     " + rs.getString(3));
  106.                         }
  107.  
  108.                     }
  109.                        
  110.                        
  111.                      catch (SQLException e) {
  112.             e.printStackTrace();
  113.         }
  114.                      
  115.                    
  116.                 } else {
  117.                     System.out.println("Du har ikke indtastet et gyldigt tal");
  118.                 }
  119.         }
  120.        
  121.  
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement