Create a pseudocoode and a brief description for the next RectangleDB Class in J
ID: 3713182 • Letter: C
Question
Create a pseudocoode and a brief description for the next RectangleDB Class in Java:
package murach.db;
import murach.rectangle.Rectangle;
import java.util.List;
import java.util.ArrayList;
import java.sql.*;
/**
*
* @author Zachary Cousineau
*/
public class RectangleDB {
/**
* Inserts the passed Rectangle into the database.
* @param r Rectangle to be stored.
* @author Zachary Cousineau
*/
public static void insert(Rectangle r)
{
try
{
Connection connection = DBUtil.getConnection();
Statement statement = connection.createStatement();
String query = "INSERT INTO Rectangle (Length, Width)" +
"VALUES (" + r.getLength() + ", " + r.getWidth() + ")";
statement.executeUpdate(query);
connection.close();
}
catch (SQLException e)
{
System.out.println(e);
}
}
/**
* Retrieves a list of all the Rectangles stored in the database.
* @return List containing the retrieved Rectangles.
* @author Zachary Cousineau
*/
public static List<Rectangle> getAll()
{
try
{
Connection connection = DBUtil.getConnection();
Statement statement = connection.createStatement();
ArrayList<Rectangle> Rectangles = new ArrayList<>();
ResultSet customerInfo = statement.executeQuery(
"SELECT * FROM Rectangle"
);
while (customerInfo.next())
{
Rectangles.add(new Rectangle(customerInfo.getDouble(2), customerInfo.getDouble(3)));
}
connection.close();
return Rectangles;
}
catch (SQLException e)
{
System.out.println(e);
return null;
}
}
}
Explanation / Answer
Note : We always write a pseudo code of class which have the main method your class does not have any main method. So now i will give pseudo code for the 2 method of your class. Insert method pseudo code 1. create connection 2. create statement 3. declare and initialize the query 4. execute the query 5. close the connection getAll method pseudo code 1. create connection 2. create statement 3. declare a list of rectangle objects 4. declare and initialize the query 5. execute the query and store the result 6. set the result retrieved to list 7. close the connection 8. return the list Explanation: Below is the Java code for above problem with proper description provided within comments itself. If you need any other help for this Comment me below. Thanks Java code : package murach.db; // package name // import classs import murach.rectangle.Rectangle; import java.util.List; import java.util.ArrayList; import java.sql.*; /** * * @author Zachary Cousineau */ public class RectangleDB { // class name /** * Inserts the passed Rectangle into the database. * * @param r * Rectangle to be stored. * @author Zachary Cousineau */ public static void insert(Rectangle r) { // insert method try { Connection connection = DBUtil.getConnection(); // create connection with data base to our application Statement statement = connection.createStatement(); // create statement iobject which execute the query String query = "INSERT INTO Rectangle (Length, Width)" + "VALUES (" + r.getLength() + ", " + r.getWidth() + ")"; // query statement.executeUpdate(query); // execute query connection.close(); // close the connection with data base } catch (SQLException e) { // handle exception System.out.println(e); // print error } } /** * Retrieves a list of all the Rectangles stored in the database. * * @return List containing the retrieved Rectangles. * @author Zachary Cousineau */ public static List getAll() { // get all method which return all rectangle object try { Connection connection = DBUtil.getConnection(); // create connection with data base to our application Statement statement = connection.createStatement(); // create statement iobject which execute the query ArrayList Rectangles = new ArrayList(); // list of rectangle oobject ResultSet customerInfo = statement.executeQuery("SELECT * FROM Rectangle"); // query execute and store result while (customerInfo.next()) { // loop untill result is not empty Rectangles.add(new Rectangle(customerInfo.getDouble(2), customerInfo.getDouble(3))); // set to our list } connection.close(); // close the connection return Rectangles; // return rectangles } catch (SQLException e) { System.out.println(e); return null; } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.