Example usage for java.sql ResultSet getDouble

List of usage examples for java.sql ResultSet getDouble

Introduction

In this page you can find the example usage for java.sql ResultSet getDouble.

Prototype

double getDouble(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a double in the Java programming language.

Usage

From source file:UpdateableRs.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    printRs(rs);/*from  w ww.j  a  v  a 2s.com*/

    rs.beforeFirst();

    while (rs.next()) {
        double newSalary = rs.getDouble("salary") * 1.053;
        rs.updateDouble("salary", newSalary);
        rs.updateRow();
    }
    printRs(rs);
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    printRs(rs);//from www . ja v  a2s .co  m

    rs.beforeFirst();

    while (rs.next()) {
        double newSalary = rs.getDouble(3) * 1.053;
        rs.updateDouble("salary", newSalary);
        rs.updateRow();
    }
    printRs(rs);
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", "");

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_UPDATABLE);

    String query = "SELECT id, code, name, quantity, price FROM products";
    ResultSet uprs = statement.executeQuery(query);

    while (uprs.next()) {
        System.out.println(uprs.getString("id") + ":" + uprs.getString("code") + ":" + uprs.getString("name")
                + ":" + uprs.getInt("quantity") + ":" + uprs.getDouble("price"));
    }//from w w  w .jav a 2 s. co m
    uprs.first();
    uprs.updateString("name", "Java");
    uprs.updateRow();
    uprs.next();
    uprs.deleteRow();

    uprs.moveToInsertRow();
    uprs.updateString("code", "1");
    uprs.updateString("name", "Data Structures");
    uprs.updateInt("quantity", 1);
    uprs.updateDouble("price", 5.99);
    uprs.insertRow();

    uprs.beforeFirst();
    while (uprs.next()) {
        System.out.println(uprs.getString("id") + "\t" + uprs.getString("code") + "\t" + uprs.getString("name")
                + "\t" + uprs.getInt("quantity") + "\t" + uprs.getDouble("price"));
    }
    connection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", "");

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = statement.executeQuery("SELECT * FROM products");
    while (resultSet.next()) {
        String productCode = resultSet.getString("product_code");
        String productName = resultSet.getString("product_name");
        int quantity = resultSet.getInt("quantity");
        double price = resultSet.getDouble("price");

        System.out.println(productCode + "\t" + productName + "\t" + quantity + "\t" + price);
    }/*w w w.java  2  s .  co m*/

    while (resultSet.previous()) {
        String productCode = resultSet.getString("product_code");
        String productName = resultSet.getString("product_name");
        int quantity = resultSet.getInt("quantity");
        double price = resultSet.getDouble("price");

        System.out.println(productCode + "\t" + productName + "\t" + quantity + "\t" + price);
    }
    connection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", "");

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = statement.executeQuery("SELECT * FROM products");
    resultSet.afterLast();//  w ww.  ja  v  a 2  s  .c  o  m
    while (resultSet.previous()) {
        String productCode = resultSet.getString("product_code");
        String productName = resultSet.getString("product_name");
        int quantity = resultSet.getInt("quantity");
        double price = resultSet.getDouble("price");

        System.out.println(productCode + "\t" + productName + "\t" + quantity + "\t" + price);
    }
    connection.close();
}

From source file:geocodingissues.Main.java

/**
 * @param args the command line arguments
 *//*w ww. ja v a 2  s  . c  o  m*/
public static void main(String[] args) throws JSONException {
    Main x = new Main();
    ResultSet rs = null;

    x.establishConnection();
    //x.addColumns(); //already did this

    rs = x.getLatLong();

    int id;
    double latitude;
    double longitude;
    String req;

    String street_no;
    String street;
    String neighborhood;
    String locality;
    String PC;

    JSONObject jObject;
    JSONArray resultArray;
    JSONArray compArray;

    try {
        while (rs.next()) {
            id = rs.getInt("id");
            latitude = rs.getDouble("latitude");
            longitude = rs.getDouble("longitude");

            //System.out.println("id: " + id);

            req = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + Double.toString(latitude) + ","
                    + Double.toString(longitude)
                    + "&result_type=street_address|neighborhood|locality|postal_code&key=" + key;

            try {
                URL url = new URL(req + "&sensor=false");
                URLConnection conn = url.openConnection();
                ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
                IOUtils.copy(conn.getInputStream(), output);
                output.close();
                req = output.toString();
            } catch (Exception e) {
                System.out.println("Geocoding Error");
            }
            if (req.contains("OVER_QUERY_LIMIT")) {
                System.out.println("Over Daily Query Limit");
                System.exit(0);
            }
            if (!req.contains("ZERO_RESULTS")) {
                //System.out.println("req: ");
                //System.out.println(req);
                jObject = new JSONObject(req);
                resultArray = jObject.getJSONArray("results");

                // Retrieve information on street address and insert into table
                compArray0 = resultArray.getJSONObject(0).getJSONArray("address_components");
                street_no = compArray0.getJSONObject(0).getString("long_name");
                street = compArray0.getJSONObject(1).getString("long_name");
                x.insertValues(id, street_no, street);

                // Retrieve information on neighborhood and insert into table
                compArray1 = resultArray.getJSONObject(1).getJSONArray("address_components");
                neighborhood = compArray1.getJSONObject(0).getString("long_name");
                x.insertNbhd(id, neighborhood);

                // Retrieve information on locality and insert into table
                compArray2 = resultArray.getJSONObject(2).getJSONArray("address_components");
                locality = compArray2.getJSONObject(0).getString("long_name");
                x.insertLocality(id, locality);

                // Retrieve information on postal code and insert into table
                compArray3 = resultArray.getJSONObject(3).getJSONArray("address_components");
                PC = compArray3.getJSONObject(0).getString("long_name");
                x.insertPC(id, PC);
            }
        }
    } catch (Exception e) {
        System.out.println("Problem when updating the database.");
    }
    x.closeConnection();
}

From source file:driver.ieSystem2.java

public static void main(String[] args) throws SQLException {

    Scanner sc = new Scanner(System.in);
    boolean login = false;
    int check = 0;
    int id = 0;//from w  w w .  ja v a 2s  .  co  m
    String user = "";
    String pass = "";
    Person person = null;
    Date day = null;

    JOptionPane.showMessageDialog(null, "WELCOME TO SYSTEM", "Starting Project",
            JOptionPane.INFORMATION_MESSAGE);
    do {
        System.out.println("What do you want?");
        System.out.println("press 1 : Login");
        System.out.println("press 2 : Create New User");
        System.out.println("Press 3 : Exit ");
        System.out.println("");
        do {
            try {
                System.out.print("Enter: ");
                check = sc.nextInt();
            } catch (InputMismatchException e) {
                JOptionPane.showMessageDialog(null, "Invalid Input", "Message", JOptionPane.WARNING_MESSAGE);
                sc.next();
            }
        } while (check <= 1 && check >= 3);

        // EXIT 
        if (check == 3) {
            System.out.println("Close Application");
            System.exit(0);
        }
        // CREATE USER
        if (check == 2) {
            System.out.println("-----------------------------------------");
            System.out.println("Create New User");
            System.out.println("-----------------------------------------");
            System.out.print("Account ID: ");
            id = sc.nextInt();
            System.out.print("Username: ");
            user = sc.next();
            System.out.print("Password: ");
            pass = sc.next();
            try {
                Person.createUser(id, user, pass, 0, 0, 0, 0, 0);
                System.out.println("-----------------------------------------");
                System.out.println("Create Complete");
                System.out.println("-----------------------------------------");
            } catch (Exception e) {
                System.out.println("-----------------------------------------");
                System.out.println("Error, Try again");
                System.out.println("-----------------------------------------");
            }
        } else if (check == 1) {
            // LOGIN
            do {
                System.out.println("-----------------------------------------");
                System.out.println("LOGIN ");
                System.out.print("Username: ");
                user = sc.next();
                System.out.print("Password: ");
                pass = sc.next();
                if (Person.checkUser(user, pass)) {
                    System.out.println("-----------------------------------------");
                    System.out.println("Login Complete");
                } else {
                    System.out.println("-----------------------------------------");
                    System.out.println("Invalid Username / Password");
                }
            } while (!Person.checkUser(user, pass));
        }
    } while (check != 1);
    login = true;

    person = new Person(user);
    do {
        System.out.println("-----------------------------------------");
        System.out.println("Hi " + person.getPerName());
        System.out.println("Press 1 : Add Income");
        System.out.println("Press 2 : Add Expense");
        System.out.println("Press 3 : Add Save");
        System.out.println("Press 4 : History");
        System.out.println("Press 5 : Search");
        System.out.println("Press 6 : Analytics");
        System.out.println("Press 7 : Total");
        System.out.println("Press 8 : All Summary");
        System.out.println("Press 9 : Sign Out");
        do {
            try {
                System.out.print("Enter : ");
                check = sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("Invalid Input");
                sc.next();
            }
        } while (check <= 1 && check >= 5);
        // Add Income
        if (check == 1) {
            double Income;
            String catalog = "";
            double IncomeTotal = 0;
            catalog = JOptionPane.showInputDialog("What is your income : ");
            Income = Integer.parseInt(JOptionPane.showInputDialog("How much is it   "));

            person.addIncome(person.getPerId(), day, catalog, Income);
            person.update();
        } //Add Expense
        else if (check == 2) {
            double Expense;
            String catalog = "";
            catalog = JOptionPane.showInputDialog("What is your expense :");
            Expense = Integer.parseInt(JOptionPane.showInputDialog("How much is it   "));
            person.addExpense(person.getPerId(), day, catalog, Expense);
            person.update();
        } //Add Save 
        else if (check == 3) {
            double Saving;
            double SavingTotal = 0;
            String catalog = "";

            Saving = Integer.parseInt(JOptionPane.showInputDialog("How much is it "));
            SavingTotal += Saving;
            person.addSave(person.getPerId(), day, catalog, Saving);
            person.update();
        } //History
        else if (check == 4) {
            String x;
            do {
                System.out.println("-----------------------------------------");
                System.out.println("YOUR HISTORY");
                System.out.println("Date                Type           Amount");
                System.out.println("-----------------------------------------");
                List<History> history = person.getHistory();
                if (history != null) {
                    int count = 1;
                    for (History h : history) {
                        if (count++ <= 1) {
                            System.out.println(h.getHistoryDateTime() + "   " + h.getHistoryDescription()
                                    + "           " + h.getAmount());
                        } else {
                            System.out.println(h.getHistoryDateTime() + "   " + h.getHistoryDescription()
                                    + "           " + h.getAmount());
                        }
                    }
                }
                System.out.println("-----------------------------------------");
                System.out.print("Back to menu (0 or back) : ");
                x = sc.next();
            } while (!x.equalsIgnoreCase("back") && !x.equalsIgnoreCase("0"));
        } //Searh
        else if (check == 5) {
            try {
                Connection conn = ConnectionDB.getConnection();
                long a = person.getPerId();
                String NAME = "Salary";
                PreparedStatement ps = conn.prepareStatement(
                        "SELECT * FROM  INCOME WHERE PERID = " + a + "  and CATALOG LIKE '%" + NAME + "%' ");
                ResultSet rec = ps.executeQuery();

                while ((rec != null) && (rec.next())) {
                    System.out.print(rec.getDate("Days"));
                    System.out.print(" - ");
                    System.out.print(rec.getString("CATALOG"));
                    System.out.print(" - ");
                    System.out.print(rec.getDouble("AMOUNT"));
                    System.out.print(" - ");
                }
                ps.close();
                conn.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } //Analy 
        else if (check == 6) {
            String x;
            do {
                DecimalFormat df = new DecimalFormat("##.##");
                double in = person.getIncome();
                double ex = person.getExpense();
                double sum = person.getSumin_ex();
                System.out.println("-----------------------------------------");
                System.out.println("Income : " + df.format((in / sum) * 100) + "%");
                System.out.println("Expense : " + df.format((ex / sum) * 100) + "%");
                System.out.println("\n\n");
                System.out.print("Back to menu (0 or back) : ");
                x = sc.next();
            } while (!x.equalsIgnoreCase("back") && !x.equalsIgnoreCase("0"));
        } //TOTAL
        else if (check == 7) {
            String x;
            do {
                System.out.println("-----------------------------------------");
                System.out.println("TOTALSAVE             TOTAL");
                System.out.println(
                        person.getTotalSave() + " Baht" + "             " + person.getTotal() + " Baht");
                System.out.println("\n\n");
                System.out.print("Back to menu (0 or back) : ");
                x = sc.next();
            } while (!x.equalsIgnoreCase("back") && !x.equalsIgnoreCase("0"));
        } //ALL Summy
        else if (check == 8) {
            String x;
            do {
                DecimalFormat df = new DecimalFormat("##.##");
                double in = person.getIncome();
                double ex = person.getExpense();
                double sum = person.getSumin_ex();
                double a = ((in / sum) * 100);
                double b = ((ex / sum) * 100);
                System.out.println("-----------------------------------------");
                System.out.println("ALL SUMMARY");
                System.out.println("Account: " + person.getPerName());
                System.out.println("");
                System.out.println("Total Save ------------- Total");
                System.out
                        .println(person.getTotalSave() + " Baht               " + person.getTotal() + " Baht");
                System.out.println("");

                System.out.println("INCOME --------------- EXPENSE");
                System.out.println(df.format(a) + "%" + "                  " + df.format(b) + "%");

                System.out.println("-----------------------------------------");
                System.out.println("\n\n");
                System.out.print("Back to menu (0 or back) : ");
                x = sc.next();
            } while (!x.equalsIgnoreCase("back") && !x.equalsIgnoreCase("0"));
        } //LOG OUT 
        else {
            System.out.println("See ya.\n");
            login = false;
            break;
        }
    } while (true);
}

From source file:ScrollableRs.java

public static void printRow(ResultSet rs) throws SQLException {
    int ssn = rs.getInt("ssn");
    String name = rs.getString("name");
    double salary = rs.getDouble("salary");

    System.out.print("Row Number=" + rs.getRow());
    System.out.print(", SSN: " + ssn);
    System.out.print(", Name: " + name);
    System.out.println(", Salary: $" + salary);
}

From source file:ExecuteMethod.java

public static void processExecute(Statement stmt, boolean executeResult) throws SQLException {
    if (!executeResult) {
        int updateCount = stmt.getUpdateCount();
        System.out.println(updateCount + " row was " + "inserted into Employee table.");

    } else {/*www  .  ja  v a  2  s . c om*/
        ResultSet rs = stmt.getResultSet();
        while (rs.next()) {
            System.out.println(rs.getInt("SSN") + rs.getString("Name") + rs.getDouble("Salary")
                    + rs.getDate("Hiredate") + rs.getInt("Loc_id"));

        }
    }
}

From source file:common.utility.ChartHelper.java

public static CategoryDataset createDataset() throws SQLException {

    // create the dataset...
    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    ResultSet rs = new PersonDetailsDAO().selectAllPersonofArea();
    while (rs.next()) {
        dataset.setValue(rs.getDouble("NumOfCitizens"), "Area Code : " + rs.getString("AreaCode"), "Areas");
    }/*from  w  ww  .  java 2s.c  om*/

    return dataset;

}