Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package databaseproject;
- import java.sql.*;
- import java.util.Scanner;
- import java.util.logging.*;
- // java -cp postgresql-9.4-1201.jdbc4.jar:. DBtest
- /**
- *
- * @author jonaspedersen
- */
- public class DatabaseProject {
- /**
- * @param args the command line arguments
- */
- static String query;
- static Connection con = null;
- public static void main(String[] args) {
- String url = "jdbc:postgresql://localhost:5432/postgres";
- String user = "postgres";
- String password = "Snuden123";
- //CONNECTING
- try {
- con = DriverManager.getConnection(url, user, password);
- } catch (SQLException ex) {
- Logger lgr = Logger.getLogger(DatabaseProject.class.getName());
- lgr.log(Level.WARNING, ex.getMessage(), ex);
- }
- databaseConnection(con);
- }
- public static void databaseConnection(Connection con){
- //RUN AN EXAMPLE QUERY
- selectComponents(con);
- pcSystemList(con);
- }
- public static void selectComponents(Connection con){
- query = "SELECT component.name, component.quantity FROM component";
- Scanner sc = new Scanner(System.in);
- int choice;
- System.out.println("0: Gå videre til næste mulighed 1: Liste af alle komponenter og deres antal på lager");
- choice = sc.nextInt();
- if(choice == 0){
- } else if (choice == 1){
- try {
- PreparedStatement st = con.prepareStatement(query);
- ResultSet rs = st.executeQuery();
- while (rs.next()) {
- System.out.println(rs.getString(1) + " | " + rs.getString(2));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- } else {
- System.out.println("Du har ikke indtastet et gyldigt tal, prøv igen \n");
- selectComponents(con);
- }
- }
- public static void pcSystemList(Connection con){
- 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";
- //query = "SELECT name FROM computer_system";
- Scanner sc = new Scanner(System.in);
- int choice;
- System.out.println("0: Gå videre til næste mulighed 1: Liste af alle computersystemer og hvor mange af hver som kan bygges");
- choice = sc.nextInt();
- if(choice == 0){
- } else if (choice == 1){
- try {
- PreparedStatement st = con.prepareStatement(query);
- ResultSet rs = st.executeQuery();
- while (rs.next()){
- System.out.println("ID: " + rs.getString(1) + " antal: " + rs.getString(2) + " computersystem: " + rs.getString(3));
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- } else {
- System.out.println("Du har ikke indtastet et gyldigt tal");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement