How To Traverse ResultSet Object Using Nested While Statements
Dec 24, 2014
I have a my result set object which has data as shown by the attached image.What i am trying to do is traverse these results and do certain operations. I want my program to do this, if I
View Replies
ADVERTISEMENT
Sep 11, 2014
I am in the process of creating a calculator GUI that calculates different answers based on inputs two main comboboxes and numbers in the appropriate textfields. The first one allows the user to choose from 18 different materials, while the second has the user choose between two different shapes. In a brief word explanation, here's how it's set up:
User chooses material.
User chooses shape.
User types in appropriate values.
'Calculate' button clicked.
If shape = rectangle, execute rectangle calculation.
Else if shape = cylinder, execute cylinder calculation.
Everything works just fine with zero errors on all 18 materials and all kinds of decimal numbers when the second shape is selected. So the math and layout is solid. However, when the first shape is selected, it returns a $0.00 answer regardless of the input values (still no red-line errors). In efforts to troubleshoot, when I have /* Cylinder section */, the rectangle section works to a 'T'. This, I believe is caused by poor formatting in the syntax of the 'if' 'else if' in regards to the shape combobox.
//If 'Rectangle' is selected:
if(shapeDropDown.getSelectedIndex()==1) {
//6061
if(materialDropDown.getSelectedIndex()==0) {
String msg0 = "The price is: $"
+ currencyFormat.format((0.098*(number2*number3*number4)*3.06));
totalPrice.setText(msg0);
[code]....
View Replies
View Related
Feb 19, 2015
How I am supposed to set up the units for my program, and how to correctly display them in the console. Here is a description of my program.
To display the nonzero denominations only, using singular words for single units such as 1 dollar and 1 penny, and plural words for more than one unit such as 2 dollars and 3 pennies. You must use nested if statement when displaying the output.
Sample run:
Enter an amount in double, for example 11.56: 1.56
Your amount 1.56 consists of
1 dollar
2 quarters
1 nickel
1 penny
View Replies
View Related
Dec 2, 2014
So basically i have to read from a text file and get some information back from that and print to screen. Im quite confused and im not sure what loop i should use first to scan the text and receive certain information back?
View Replies
View Related
May 5, 2014
So I'm pretty sure this is correct, as it follows most examples I can find online, but I keep getting an error that my return variable cannot be resolved. The error is on the return conn; statement. It says conn cannot be resolved. If I place it above within the try block it allows it but then I receive an error saying the method getDBConnection must return type Connection.
import java.sql.*;
public Connection getDBConnection() {
try {
Class.forName("org.sqlite.JDBC");
String path = "jdbc:sqlite::resource:project.db";
[Code] .....
I don't want to create this method. Basically I want to connect to the database in the main program, but I do want methods that can access the DB too. But however I place it, it doesn't let me touch any of the DB variables outside of the Try block.
View Replies
View Related
Jul 8, 2014
I have stumbled onto a problem with ArrayLists (not sure if nested ArrayList objects would be a more accurate description) ....
As simply put as I can describe it, I have an Inventory class which creates an ArrayList of a Product superclass, which has two subclasses, PerishableProduct and ItemisedProduct.
By default, the program is meant to have a starting inventory, which is why I have added them in the constructor
public class Inventory {
private List<Product> products;
public Inventory() {
addProduct(new Product("Frying pan", 15, 20));
addProduct(new PerishableProduct("Apple", 5.8, 30, 7));
addProduct(new ItemisedProduct("Cereal", 5.8, 0));
// this is where I am having problems. Wanting to add
// objects to the ItemisedProduct's ArrayList for cereal.
}
Within the ItemisedProduct subclass is yet another ArrayList which is meant to hold serial numbers.
public class ItemisedProduct extends Product {
private ArrayList<String> serialNumbers = new ArrayList();
public ItemisedProduct(String name, double price, int qty) {
super(name, price, qty)
[Code] .....
My problem is that I do not know how to populate the serialNumbers ArrayList from the Inventory class' constructor. Because technically, quantity is defined by how many serial numbers are created.
View Replies
View Related
Mar 12, 2014
I was wondering how to traverse through a hashtable. I am stuck on how to implement an iterator for the values of some hashtable.
Java Code:
public Iterator<V> values() {
return new Iterator<V>(){
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
[Code] ....
View Replies
View Related
Oct 14, 2014
I am very new to coding and I am struggling with one of my intro to Java Projects. This project involves creating a GUI that has a list of trees and their possible heights, giving each tree a range for their possible minimum and maximum heights.
I am having trouble creating this method for the TreeDatabase (TreeDB class):
public String queryByPossibleHeight(int key) Instructions for this method are as follows: this method creates and returns a single string that contains the trees that can be found such that the value key is in the range for possible height. Traverse the entire ArrayList of Trees and for each one of the trees contained in the ArrayList invoke the method inRange() from the class Tree. Pass to the method inRange() the parameter key. If inRange() returns true, invoke the method toString() from the class Tree. Append the value returned by invoking the method toString() to the String result, adding a “” at the end. Once the entire ArrayList has been traversed, return the String result.
My 3 classes so far (I haven't gotten to the actual GUI yet):
public class Range {
private int low;
private int high;
private int value;
public Range(int plow,int phigh){
low = plow;
high = phigh;
[Code]...
As you can see in the last TreeDB class, in the public String queryByPossibleHeight(int key) method I have something wrote in, but I was just testing random things.
View Replies
View Related
Nov 11, 2014
I have to make a method called search that goes through the linked list and checks to see if whatever String the user entered matches a String in the linked list. With this code, every time I enter an existing String it outputs "There is no element that contains that information". How come?
public class LinkedListExample {
List InfoList = new LinkedList();
public void doLinkedListExample()
{ // add original data to linked list
InfoList.add("Computer"); // string (original 3 elements)
InfoList.add("Programs");
InfoList.add("in Java");
[code]....
View Replies
View Related
Nov 12, 2014
I have to make a program that allows the user to make a linked list of Strings (names) and then choose who to eliminate.
For example: Five people in a linked list of Strings: I would like the program to ask the user to choose a number between 1 and 5. If the user chooses number 2, Mary is the second person in the linked list so she gets eliminated.
The problem is that I'm not sure how to traverse through the linked list because they are of type String value, and I am looking for the name using the corresponding integer I mentioned above. I think I have to use Nodes? Not very sure where to start right now.Here is my code:
package llchallenge;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
[code]...
View Replies
View Related
Jan 1, 2015
AddItemToFront(Item p) : This method will create a new Node with the Item object as its data value and then add the newly created node to the front of the linked list.
DisplayItems(): This method will traverse the linked list from first node to last node and print data value ( i.e., id, name, type and price of the Item object) of each node.
RemoveItemAtPosition(int n): This method will remove the node at position n in the linked list. Assume that the first node of the linked list has a position number of 1 and the second node has a position number of 2 and so on.
This is my Code
AddItemToFront
public void AddItemtoFront(Item p)
{
Node newNode = new Node(p);
newNode.setLink(head);
head = newNode;
[Code] ....
I don't know what am I suppose to do inside the remove method
View Replies
View Related
Nov 12, 2014
This is my first time using linked lists and for homework I have to make a program that allows the user to make a linked list of Strings (names) and then choose who to eliminate. For example,
Five people in a linked list of Strings: Joe, Mary, Sue, Tom, Hannah.
I would like the program to ask the user to choose a number between 1 and 5. If the user chooses number 2, Mary is the second person in the linked list so she gets eliminated.
The problem is that I'm not sure how to traverse through the linked list because they are of type String value, and I am looking for the name using the corresponding integer I mentioned above.
Here is my code:
package llchallenge;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
public class LLChallenge
{
List JosephusList = new LinkedList(); // order of names
[Code] ....
View Replies
View Related
Apr 17, 2014
I have a Java Code:
POIterator mh_sh_highlight_all('java');
class that implements an Iterator.
A new instance of the Java Code:
POIterator mh_sh_highlight_all('java');
Class is supposed to traverse a binary tree in Post Order. But when I attempt to test a
Java Code: POIterator mh_sh_highlight_all('java');
Object in my main class, it doesn't work as its supposed to. I might be implementing it wrong.
Java Code:
public PostOrderIterator<E> implements Iterator<E>{
Deque<BNode<E>> somestack = new LinkedList<BNode<E>>();
BNode<E> position;
public POIterator(BNode<E>root){
somestack = new LinkedList<BNode<E>>();
somestack.push(root);}
[Code] ....
View Replies
View Related
Aug 6, 2014
Ok here i am trying to pull out all the items in my Product table but all it does it last the row in the table. I have tried using do while but it still does not retrieve the information
Here the the Bean
public class Pageitem {
private int id;
private String name;
private double price;
private String description;
private Date modifyDate;
private int category;}//Has getter Setters and ToString methods for the items
[Code] .....
View Replies
View Related
Apr 20, 2015
I am trying to export a resultset to a csv file but the outputted csv file is blank with 0 bytes.The code I am using is below.
File file = new File(System.getProperty("user.dir")+"avatarDumps"+id+"_"+name+".csv");
try (FileOutputStream fop = new FileOutputStream(file)) {
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
String query = "SELECT * FROM avatars WHERE id='"+id+"'";
[code]...
View Replies
View Related
Feb 24, 2015
The below method shows that a JOptionPane should display when the result is null, meaning it has not found what I was trying to look for. However only the if statement that finds the query runs:
public void find(Person person) {
String query = "SELECT * from employee WHERE last LIKE'" + lastName
+ "'";
try {
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
[Code] ....
View Replies
View Related
Oct 28, 2014
I have a java query like this
query ="Select major_career.Major_Title, career.ISCOTitle,career.FS.... " //only partial,
// I swear the query is correct
resultSet = statement.executeQuery(query); //this executes it
relArr = new ArrayList<String>(); //don't worry it is intialized
In the code below I tried to store the resultset components into the arraylist
int j = 1;
while (resultSet.next()) {
while(j<=numberOfColumns){
relArr.add(resultSet.getObject(j).toString());
j++;
}
} // end while
I am not sure whether the arraylist is able to store the result set because when i try to display it like show below it only shows some rows and only the first column
Iterator it = relArr.iterator();
while (it.hasNext())
{
System.out.println(it.next());
I want to manipulate the resultset results in my program by copying the resultset values to other datastructures.
View Replies
View Related
Jun 8, 2014
I've been trying to get a handle on how to populate a jtable with data from my resultset and have found out that using DBUtils is probably the easiest method but unfortunately its not working for me at all. The program runs but the jtable remains empty as ever. I don't understand where I'm going wrong with it. I have import net.proteanit.sql.DbUtils; imported at the top and the jar added in class path. Here is my code:
private void jPanel3FocusGained(java.awt.event.FocusEvent evt) {
// TODO add your handling code here:
// JDBC driver name and database URL
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost/new";
[Code] .....
View Replies
View Related
Apr 2, 2014
I have retrieve the data in listbox from my database already and now again i want to select the data from this listbox and query on it , is it possible?
For Ex i have listbox as follows
<h4>Models For Selected Pattern:</h4>
</br>
<select Name="drop_model" multiple="true">
<%
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url,username,userPassword);
Statement stmt = conn.createStatement();
[Code] ....
In this listbox some values are retrieved from database as
<option value="xyz">xyz</option>
<option value="pqr">pqr</option>
<option value="abc">abc</option>
<option value="lmn">lmn</option>
I want to select this values from listbox and query on them....
View Replies
View Related
Apr 7, 2014
I'm working in a project where a particular part has been assigned to me. I've a ResultSet of informations like below:
EmpId EmpFirstName EmpLastName EmpAge 1 ABC DEF 43 2 PQR XYZ 37
Now I've to send this ResultSet(to another function) with EmpFullName in following manner.
EmpId EmpFirstName EmpLastName EmpFullName EmpAge 1 ABC DEF DEF, ABC 43 2 PQR XYZ XYZ, PQR 37
There is no permission for me that I can change SQL query(or anything like this), and I've to return the modified ResultSet(not TableModel). I googled a lot to implement AbstractResultSet, but no luck for me. How can I achieve this ?
View Replies
View Related
Mar 27, 2014
I am trying to use a custom class in a .jsp page, as part of a course I am taking on Web Technologies.The original version of the jsp page was working fine, until I moved part of the Java code to a separate class.Here is the original .jsp code that is working:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-GB" xml:lang="en-GB">
<%@page pageEncoding="utf-8" %>
<head>
<meta charset="UTF-8" />
<title>Forum</title>
<link rel="stylesheet" type="text/css" href="stocktails.css" />
</head>
<body>
[code]....
View Replies
View Related
Jul 7, 2014
It's always good to keep functions smaller and focused on one behavior. So is this safe:
Java Code:
public Unit findBySql(int id){
Unit unit=null;
DbConnectionPool dbPool = DbConnectionPool.getInstance();
HashMap<String, String> conditions = new HashMap<String, String>();
conditions.put("id", String.valueOf(id));
String sql = buildSelect("units", "*", conditions);
[Code] ......
As you can see a pass ResultSet to a function which populates the item. But I also make sure that the ResultSet that the passed object is pointing to is closed, so it doesn't leak resources.
View Replies
View Related
Aug 15, 2014
I'm want to call another statement within the same resultSet. But it gives an error that the result set is closed.
public static void updateAlarmStatus(int status) {
ResultSet rs = null;
database.restartDatabase();
try {
rs = database
.executeQuery("Select alarmId from alarms where alarmDate = '"
[Code] ....
I googled it and came to know that resultSet is automatically closed if we call another statement within in. I've also tried creating the connection again before the second statement but of no use.
View Replies
View Related
Mar 15, 2014
I am trying to get resultset into a string and then use split method to store it in an array but it is not working. i tried to work it seperately as java program but it throws exception "could not find or load main class java."
String ar = "This.is.a.split.method";
String[] temp = ar.split(".");
for(int i=0;i<temp.length;i++){
System.out.println(temp[i]);
}
View Replies
View Related
Mar 12, 2014
I am building an online quiz. I created a database , bean , controller and jsp. I connected database, wrote query , put resultset in arraylist of object and passed it to jsp. My program runs but arraylist size increases everytime and same question get displayed repeatedly . i cant find the error. Everytime i run the program arraylist size increases. I think it is adding same rows again ang again. here is my code for controller and jsp.
servlet code:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
[Code] .....
View Replies
View Related
Jul 18, 2014
I am very new to multithreading thus the code below reeks of ignorance,but i guess thats a place to start from.I am trying to obtain values that are already in my db and send them to a database by creating a new thread as follows:
//ADDED JUL 15 2014 12:07, LISTITEMS SO
public void getList() throws SQLException {
String sql = "select * from PRODUCTS";
out.print(m_Stmt);
ResultSet rs = m_Stmt.executeQuery(sql);
ExecutorService es=Executors.newCachedThreadPool();
es.execute(new Runnable(){
@Override
[code]...
View Replies
View Related