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 sql;
- import java.sql.*;
- import java.util.Scanner;
- import java.util.logging.*;
- // java -cp postgresql-9.4-1201.jdbc4.jar:. DBtest
- public class SQL {
- public static void main(String[] args) throws SQLException {
- String url = "jdbc:postgresql://localhost:5432/postgres";
- String user = "postgres";
- String password = "Rasmussen123";
- Connection con = null;
- //CONNECTING
- try {
- con = DriverManager.getConnection(url, user, password);
- } catch (SQLException ex) {
- Logger lgr = Logger.getLogger(SQL.class.getName());
- lgr.log(Level.WARNING, ex.getMessage(), ex);
- }
- task1(con);
- }
- public static void task1(Connection con) throws SQLException{
- //RUN AN EXAMPLE QUERY
- int userPrice = 0;
- String tempModel;
- float tempSpeed;
- int tempPrice;
- int priceOfClosest = 0;
- String modelOfClosest = "";
- float speedOfClosest = 0;
- Scanner scanner = new Scanner(System.in);
- System.out.println("Giv Price plz");
- userPrice = scanner.nextInt();
- try {
- Statement st = con.createStatement();
- String query = "SELECT model, price, speed FROM pc";
- ResultSet pcs = st.executeQuery(query);
- while (pcs.next()) {
- tempModel = pcs.getString(1);
- tempPrice = pcs.getInt(2);
- tempSpeed = pcs.getFloat(3);
- if(userPrice - tempPrice < tempPrice - priceOfClosest){
- modelOfClosest = tempModel;
- priceOfClosest = tempPrice;
- speedOfClosest = tempSpeed;
- }
- }
- System.out.println(modelOfClosest);
- } catch (SQLException e) {
- e.printStackTrace();
- con.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement