Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Scanner;
- import java.util.logging.*;
- //java -cp postgresql-9.4-1201.jdbc4.jar:. DBtest
- public class DBtest {
- public static void main(String[] args) {
- String url = "jdbc:postgresql://localhost:5432/afl1_db";
- String user = "postgres";
- String password = "1234";
- Connection con = null;
- //CONNECTING
- try {
- con = DriverManager.getConnection(url, user, password);
- } catch (SQLException ex) {
- Logger lgr = Logger.getLogger(DBtest.class.getName());
- lgr.log(Level.WARNING, ex.getMessage(), ex);
- }
- Scanner input = new Scanner(System.in);
- String userChoice;
- boolean running = true;
- while (running) {
- System.out.println("Hvilken knap skal du trykke på?????");
- userChoice = input.nextLine();
- //How to quit the appplication
- if (userChoice.equals("q")) {
- running = false;
- //Price list including all components and their selling prices grouped by kind
- } else if (userChoice.equals("p")) {
- try {
- System.out.println("List of components and their price");
- Statement st = con.createStatement();
- String query1 = "SELECT * FROM component ORDER BY kind";
- ResultSet rs1 = st.executeQuery(query1);
- while (rs1.next()) {
- System.out.print(rs1.getString(3));
- System.out.print(" -> ");
- System.out.print(rs1.getString(4));
- System.out.print(" - Costs: ");
- System.out.println(rs1.getInt(2));
- }
- } catch (SQLException ex) {
- Logger.getLogger(DBtest.class.getName()).log(Level.SEVERE, null, ex);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement