Advertisement
gyhuji93

1. method db

Apr 5th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3. import java.util.logging.*;
  4.  
  5. //java -cp postgresql-9.4-1201.jdbc4.jar:. DBtest
  6. public class DBtest {
  7.  
  8.     public static void main(String[] args) {
  9.         String url = "jdbc:postgresql://localhost:5432/afl1_db";
  10.         String user = "postgres";
  11.         String password = "1234";
  12.         Connection con = null;
  13.  
  14.         //CONNECTING
  15.         try {
  16.             con = DriverManager.getConnection(url, user, password);
  17.  
  18.         } catch (SQLException ex) {
  19.             Logger lgr = Logger.getLogger(DBtest.class.getName());
  20.             lgr.log(Level.WARNING, ex.getMessage(), ex);
  21.  
  22.         }
  23.         Scanner input = new Scanner(System.in);
  24.         String userChoice;
  25.         boolean running = true;
  26.  
  27.         while (running) {
  28.  
  29.             System.out.println("Hvilken knap skal du trykke på?????");
  30.             userChoice = input.nextLine();
  31.            
  32.             //How to quit the appplication
  33.             if (userChoice.equals("q")) {
  34.                 running = false;
  35.             //Price  list including  all  components  and  their  selling  prices  grouped  by kind
  36.             } else if (userChoice.equals("p")) {
  37.                 try {
  38.                     System.out.println("List of components and their price");
  39.                     Statement st = con.createStatement();
  40.                     String query1 = "SELECT * FROM component ORDER BY kind";
  41.                     ResultSet rs1 = st.executeQuery(query1);
  42.                     while (rs1.next()) {
  43.                         System.out.print(rs1.getString(3));
  44.                         System.out.print("  ->   ");
  45.                         System.out.print(rs1.getString(4));
  46.                         System.out.print("    -    Costs: ");
  47.                         System.out.println(rs1.getInt(2));
  48.  
  49.                     }
  50.                 } catch (SQLException ex) {
  51.                     Logger.getLogger(DBtest.class.getName()).log(Level.SEVERE, null, ex);
  52.                 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement