JAVA MSQL and NET BEANS USE mma ALTER TABLE Product ADD Not
JAVA M.S.Q.L and N.E.T B.E.A.N.S>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
USE mma;
ALTER TABLE Product ADD Note TEXT;
UPDATE Product
SET Note = \'\'
WHERE ProductID < 8;
***********Product*********************
package rooster.nest;
import java.text.NumberFormat;
public class Product {
private long id;
private String code;
private String description;
private double price;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String name) {
this.description = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPriceFormatted() {
NumberFormat currencyFormatter =
NumberFormat.getCurrencyInstance();
return currencyFormatter.format(getPrice());
}
}
********DBException************
package crows.foot;
/*
* This is just a wrapper class so we can throw a common exception for
* the UI to catch without tightly coupling the UI to the database layer.
*/
public class DBException extends Exception {
DBException() {}
DBException(Exception e) {
super(e);
}
}
*******DBUtil************
package crows.foot;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static Connection connection;
private DBUtil() {}
public static synchronized Connection getConnection() throws DBException {
if (connection != null) {
return connection;
}
else {
try {
// set the db url, username, and password
String url = \"jdbc:mysql://localhost:3306/mma\";
String username = \"mma_user\";
String password = \"sesame\";
// get and return connection
connection = DriverManager.getConnection(
url, username, password);
return connection;
} catch (SQLException e) {
throw new DBException(e);
}
}
}
public static synchronized void closeConnection() throws DBException {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
throw new DBException(e);
} finally {
connection = null;
}
}
}
}
*************ProductDB**************
package crows.foot;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import rooster.nest.Product;
public class ProductDB {
private static Product getProductFromRow(ResultSet rs) throws SQLException {
int productID = rs.getInt(1);
String code = rs.getString(2);
String description = rs.getString(3);
double price = rs.getDouble(4);
String note = rs.getString(5);
Product p = new Product();
p.setId(productID);
p.setCode(code);
p.setDescription(description);
p.setPrice(price);
return p;
}
public static List<Product> getAll() throws DBException {
String sql = \"SELECT * FROM Product ORDER BY ProductID\";
List<Product> products = new ArrayList<>();
Connection connection = DBUtil.getConnection();
try (PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
Product p = getProductFromRow(rs);
products.add(p);
}
rs.close();
return products;
} catch (SQLException e) {
throw new DBException(e);
}
}
public static Product get(String productCode) throws DBException {
String sql = \"SELECT * FROM Product WHERE Code = ?\";
Connection connection = DBUtil.getConnection();
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, productCode);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Product p = getProductFromRow(rs);
rs.close();
return p;
} else {
rs.close();
return null;
}
} catch (SQLException e) {
throw new DBException(e);
}
}
public static void add(Product product) throws DBException {
String sql
= \"INSERT INTO Product (Code, Description, ListPrice) \"
+ \"VALUES (?, ?, ?)\";
Connection connection = DBUtil.getConnection();
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, product.getCode());
ps.setString(2, product.getDescription());
ps.setDouble(3, product.getPrice());
ps.executeUpdate();
} catch (SQLException e) {
throw new DBException(e);
}
}
public static void update(Product product) throws DBException {
String sql = \"UPDATE Product SET \"
+ \"Code = ?, \"
+ \"Description = ?, \"
+ \"ListPrice = ?\"
+ \"WHERE ProductID = ?\";
Connection connection = DBUtil.getConnection();
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, product.getCode());
ps.setString(2, product.getDescription());
ps.setDouble(3, product.getPrice());
ps.setLong(4, product.getId());
ps.executeUpdate();
} catch (SQLException e) {
throw new DBException(e);
}
}
public static void delete(Product product)
throws DBException {
String sql = \"DELETE FROM Product \"
+ \"WHERE ProductID = ?\";
Connection connection = DBUtil.getConnection();
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setLong(1, product.getId());
ps.executeUpdate();
} catch (SQLException e) {
throw new DBException(e);
}
}
}
***********Console*****************
package dog.breath;
import java.util.Scanner;
public class Console {
private static Scanner sc = new Scanner(System.in);
public static void displayNewLine() {
System.out.println();
}
public static void display(String string) {
System.out.println(string);
}
public static String getString(String prompt) {
System.out.print(prompt);
String s = sc.nextLine(); // read the whole line
return s;
}
public static int getInt(String prompt) {
boolean isValid = false;
int i = 0;
while (isValid == false) {
System.out.print(prompt);
try {
i = Integer.parseInt(sc.nextLine());
isValid = true;
} catch (NumberFormatException e) {
System.out.println(\"Error! Invalid integer value. Try again.\");
}
}
return i;
}
public static int getInt(String prompt, int min, int max) {
int i = 0;
boolean isValid = false;
while (isValid == false) {
i = getInt(prompt);
if (i <= min) {
System.out.println(
\"Error! Number must be greater than \" + min);
} else if (i >= max) {
System.out.println(
\"Error! Number must be less than \" + max);
} else {
isValid = true;
}
}
return i;
}
public static double getDouble(String prompt) {
boolean isValid = false;
double d = 0;
while (isValid == false) {
System.out.print(prompt);
try {
d = Double.parseDouble(sc.nextLine());
isValid = true;
} catch (NumberFormatException e) {
System.out.println(\"Error! Invalid decimal value. Try again.\");
}
}
return d;
}
public static double getDouble(String prompt,
double min, double max) {
double d = 0;
boolean isValid = false;
while (isValid == false) {
d = getDouble(prompt);
if (d <= min) {
System.out.println(
\"Error! Number must be greater than \" + min);
} else if (d >= max) {
System.out.println(
\"Error! Number must be less than \" + max);
} else {
isValid = true;
}
}
return d;
}
}
*************Main***************
package dog.breath;
import java.util.List;
import rooster.nest.Product;
import crows.foot.DBException;
import crows.foot.DBUtil;
import crows.foot.ProductDB;
public class Main {
// declare a class variable
public static void main(String args[]) {
// display a welcome message
Console.displayNewLine();
Console.display(\"Welcome to the Product Manager\ \");
// display the command menu
displayMenu();
// perform 1 or more actions
String action = \"\";
while (!action.equalsIgnoreCase(\"exit\")) {
// get the input from the user
action = Console.getString(\"Enter a command: \");
Console.displayNewLine();
if (action.equalsIgnoreCase(\"list\")) {
displayAllProducts();
} else if (action.equalsIgnoreCase(\"add\")) {
addProduct();
} else if (action.equalsIgnoreCase(\"update\")) {
updateProduct();
} else if (action.equalsIgnoreCase(\"del\") ||
action.equalsIgnoreCase(\"delete\")) {
deleteProduct();
} else if (action.equalsIgnoreCase(\"help\") ||
action.equalsIgnoreCase(\"menu\")) {
displayMenu();
} else if (action.equalsIgnoreCase(\"exit\")) {
Console.display(\"Bye.\ \");
} else {
Console.display(\"Error! Not a valid command.\ \");
}
}
}
public static void displayMenu() {
Console.display(\"COMMAND MENU\");
Console.display(\"list - List all products\");
Console.display(\"add - Add a product\");
Console.display(\"update - Update a product\");
Console.display(\"del - Delete a product\");
Console.display(\"help - Show this menu\");
Console.display(\"exit - Exit this application\ \");
}
public static void displayAllProducts() {
Console.display(\"PRODUCT LIST\");
List<Product> products = null;
try {
products = ProductDB.getAll();
} catch (DBException e) {
Console.display(e + \"\ \");
}
if (products == null) {
Console.display(\"Error! Unable to get products.\ \");
} else {
Product p;
StringBuilder sb = new StringBuilder();
for (Product product : products) {
p = product;
sb.append(StringUtil.padWithSpaces(
p.getCode(), 12));
sb.append(StringUtil.padWithSpaces(
p.getDescription(), 34));
sb.append(p.getPriceFormatted());
sb.append(\"\ \");
}
Console.display(sb.toString());
}
}
public static void addProduct() {
String code = Console.getString(\"Enter product code: \");
String description = Console.getString(\"Enter product name: \");
double price = Console.getDouble(\"Enter price: \");
Product product = new Product();
product.setCode(code);
product.setDescription(description);
product.setPrice(price);
try {
ProductDB.add(product);
Console.display(product.getDescription()
+ \" was added to the database.\ \");
} catch (DBException e) {
Console.display(\"Error! Unable to add product.\");
Console.display(e + \"\ \");
}
}
public static void updateProduct() {
String code = Console.getString(\"Enter product code to update: \");
Product product;
try {
product = ProductDB.get(code);
if (product == null) {
throw new Exception(\"Product not found.\");
}
} catch (Exception e) {
Console.display(\"Error! Unable to update product.\");
Console.display(e + \"\ \");
return;
}
String description = Console.getString(\"Enter product name: \");
double price = Console.getDouble(\"Enter price: \");
product.setDescription(description);
product.setPrice(price);
try {
ProductDB.update(product);
} catch (DBException e) {
Console.display(\"Error! Unable to update product.\");
Console.display(e + \"\ \");
}
Console.display(product.getDescription() + \" was updated in the database.\ \");
}
public static void deleteProduct() {
String code = Console.getString(\"Enter product code to delete: \");
Product product;
try {
product = ProductDB.get(code);
if (product == null) {
throw new Exception(\"Product not found.\");
}
} catch (Exception e) {
Console.display(\"Error! Unable to delete product.\");
Console.display(e + \"\ \");
return;
}
try {
ProductDB.delete(product);
} catch (DBException e) {
Console.display(\"Error! Unable to delete product.\");
Console.display(e + \"\ \");
}
Console.display(product.getDescription() + \" was deleted from the database.\ \");
}
public static void exit() {
try {
DBUtil.closeConnection();
} catch (DBException e) {
Console.display(\"Error! Unable to close connection.\");
Console.display(e + \"\ \");
}
System.out.println(\"Bye.\ \");
System.exit(0);
}
}
***********StringUtil********************
package dog.breath;
public class StringUtil {
public static String padWithSpaces(String s, int length) {
if (s.length() < length) {
StringBuilder sb = new StringBuilder(s);
while (sb.length() < length) {
sb.append(\" \");
}
return sb.toString();
} else {
return s.substring(0, length);
}
}
}
Solution
Java is object-orientated, which means that, among different characteristics, an object can take advantage of being part of a class of objects and inherit code this is not unusual to the class. items are idea of as \"nouns\" that a user might relate to instead of the conventional procedural \"verbs.\" a technique may be thought of as one of the item\'s competencies or behaviors.
in addition to being executed on the client in preference to the server, a Java applet has different characteristics designed to make it run rapid.








