WebDev Hell
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/JSF/JSFManagedBean.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.beans;
|
||||
|
||||
import edu.slcc.asdv.bl.DBase;
|
||||
import edu.slcc.asdv.bl.part.Part;
|
||||
import edu.slcc.asdv.bl.part.DatabaseManipulationPart;
|
||||
import jakarta.inject.Named;
|
||||
import jakarta.enterprise.context.SessionScoped;
|
||||
import jakarta.faces.application.FacesMessage;
|
||||
import jakarta.faces.application.FacesMessage.Severity;
|
||||
import jakarta.faces.context.FacesContext;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@Named(value = "partsBean")
|
||||
@SessionScoped
|
||||
public class PartsBean implements Serializable
|
||||
{
|
||||
DBase dms = new DatabaseManipulationPart();
|
||||
List<Part> parts;
|
||||
|
||||
public PartsBean()
|
||||
{
|
||||
parts = dms.listAll();
|
||||
}
|
||||
|
||||
public List<Part> getParts()
|
||||
{
|
||||
return parts;
|
||||
}
|
||||
|
||||
public void saveFromUpdate()
|
||||
{
|
||||
int totalRowsUpdated = 0;
|
||||
for (Part s : this.parts)
|
||||
{
|
||||
if (s.isModify())
|
||||
{
|
||||
try
|
||||
{
|
||||
int rowsUpdated = this.dms.update(s);
|
||||
if (rowsUpdated > 0)
|
||||
{
|
||||
totalRowsUpdated += rowsUpdated;
|
||||
s.setModify(false);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
String msg = ("Numbers of rows updated is 0.");
|
||||
addMessage(msg, e.getMessage(), FacesMessage.SEVERITY_ERROR);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
String msg = ("Numbers of rows updated: " + totalRowsUpdated);
|
||||
addMessage(msg, "no exception", FacesMessage.SEVERITY_INFO);
|
||||
}
|
||||
|
||||
public void addMessage(String summary, String detail, Severity severity)
|
||||
{
|
||||
FacesMessage msg = new FacesMessage(severity, summary, detail);
|
||||
FacesContext.getCurrentInstance().addMessage(null, msg);
|
||||
}
|
||||
}
|
@@ -59,6 +59,14 @@ public class SupplierBean implements Serializable
|
||||
String msg = ("Numbers of rows updated: " + totalRowsUpdated);
|
||||
addMessage(msg, "no exception", FacesMessage.SEVERITY_INFO);
|
||||
}
|
||||
|
||||
public void deleteRow() {
|
||||
for (Supplier s: this.suppliers) {
|
||||
if (s.isDelete()) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addMessage(String summary, String detail, Severity severity)
|
||||
{
|
||||
|
@@ -18,5 +18,7 @@ public interface DBase<T>
|
||||
|
||||
int update( T t)
|
||||
throws SQLException;
|
||||
|
||||
int delete (T t) throws SQLException;
|
||||
|
||||
}
|
||||
|
@@ -44,7 +44,7 @@ public class UtilitiesDatabase
|
||||
return null;
|
||||
}
|
||||
String ip = "localhost"; //internet connection
|
||||
String url = "jdbc:mysql://" + ip + ":8889/" + databaseName + "?useSSL=false";
|
||||
String url = "jdbc:mysql://" + ip + ":3306/" + databaseName + "?allowPublicKeyRetrieval=true&useSSL=false";
|
||||
try
|
||||
{
|
||||
con = DriverManager.getConnection(url, userName, password);
|
||||
|
@@ -24,8 +24,8 @@ public class DatabaseManipulationPart implements DBase<Part>
|
||||
{
|
||||
List<Part> tableParts = new ArrayList<Part>();
|
||||
String databaseName = "suppliers_parts_23";
|
||||
String userName = "root";
|
||||
String password = "root";
|
||||
String userName = "java";
|
||||
String password = "1KUZ4r6.73IGu*18";
|
||||
String URL2 = "com.mysql.jdbc.Driver";
|
||||
Connection con = new UtilitiesDatabase().connection(
|
||||
databaseName, userName, password, URL2);
|
||||
@@ -38,7 +38,7 @@ public class DatabaseManipulationPart implements DBase<Part>
|
||||
}
|
||||
String table = "";
|
||||
ResultSet rs = null;
|
||||
String sqlStr = "SELECT * FROM part";
|
||||
String sqlStr = "SELECT * FROM part";
|
||||
//prepare statement
|
||||
ps = con.prepareStatement(sqlStr);
|
||||
//execute
|
||||
@@ -84,8 +84,62 @@ public class DatabaseManipulationPart implements DBase<Part>
|
||||
System.out.println(dmp.listAll());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public int update(Part t)
|
||||
throws SQLException
|
||||
{
|
||||
String databaseName = "suppliers_parts_23";
|
||||
String userName = "java";
|
||||
String password = "1KUZ4r6.73IGu*18";
|
||||
String URL2 = "com.mysql.jdbc.Driver";
|
||||
Connection con = new UtilitiesDatabase().connection(
|
||||
databaseName, userName, password, URL2);
|
||||
int result = 0;
|
||||
PreparedStatement updatePart = null;
|
||||
try
|
||||
{
|
||||
if (con == null)
|
||||
{
|
||||
throw new RuntimeException("cannot connect to database");
|
||||
}
|
||||
|
||||
|
||||
updatePart = con.prepareStatement(
|
||||
"UPDATE part SET pnumber=?, pname=?, color=?, city=? where pnumber=?");
|
||||
updatePart.setString(1, t.getPnumber());
|
||||
updatePart.setString(2, t.getPname());
|
||||
updatePart.setString(3, t.getColor());
|
||||
updatePart.setString(4, t.getColor());
|
||||
updatePart.setString(5, t.getPnumber());
|
||||
result = updatePart.executeUpdate();
|
||||
}
|
||||
catch (SQLException ex)
|
||||
{
|
||||
System.err.println(ex.toString());
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
new UtilitiesDatabase().closeDatabaseConnection(con);
|
||||
// close the resources
|
||||
if (updatePart != null)
|
||||
{
|
||||
updatePart.close();
|
||||
}
|
||||
}
|
||||
catch (SQLException sqle)
|
||||
{
|
||||
sqle.printStackTrace();
|
||||
throw sqle;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int delete(Part t) throws SQLException
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
||||
}
|
||||
|
@@ -8,68 +8,65 @@ package edu.slcc.asdv.bl.part;
|
||||
*
|
||||
* @author asdv5
|
||||
*/
|
||||
public class Part
|
||||
{
|
||||
public class Part {
|
||||
|
||||
private String pnumber;
|
||||
private String pname;
|
||||
private String color;
|
||||
private String city;
|
||||
private boolean modify;
|
||||
|
||||
public Part(String pnumber, String pname, String color, String city)
|
||||
{
|
||||
public boolean isModify() {
|
||||
return modify;
|
||||
}
|
||||
|
||||
public void setModify(boolean modify) {
|
||||
this.modify = modify;
|
||||
}
|
||||
|
||||
|
||||
public Part(String pnumber, String pname, String color, String city) {
|
||||
this.pnumber = pnumber;
|
||||
this.pname = pname;
|
||||
this.color = color;
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
|
||||
public String getColor()
|
||||
{
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(String color)
|
||||
{
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getCity()
|
||||
{
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city)
|
||||
{
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getPname()
|
||||
{
|
||||
public String getPname() {
|
||||
return pname;
|
||||
}
|
||||
|
||||
public void setPname(String pname)
|
||||
{
|
||||
public void setPname(String pname) {
|
||||
this.pname = pname;
|
||||
}
|
||||
|
||||
public String getPnumber()
|
||||
{
|
||||
public String getPnumber() {
|
||||
return pnumber;
|
||||
}
|
||||
|
||||
public void setPnumber(String pnumber)
|
||||
{
|
||||
public void setPnumber(String pnumber) {
|
||||
this.pnumber = pnumber;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
public String toString() {
|
||||
return "Part{" + "pnumber=" + pnumber + ", pname=" + pname + ", color=" + color + ", city=" + city + '}';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@@ -25,8 +25,8 @@ public class DatabaseManipulationSupplier implements DBase<Supplier>
|
||||
{
|
||||
List<Supplier> tableSuppliers = new ArrayList<Supplier>();
|
||||
String databaseName = "suppliers_parts_23";
|
||||
String userName = "root";
|
||||
String password = "root";
|
||||
String userName = "java";
|
||||
String password = "1KUZ4r6.73IGu*18";
|
||||
String URL2 = "com.mysql.jdbc.Driver";
|
||||
Connection con = new UtilitiesDatabase().connection(
|
||||
databaseName, userName, password, URL2);
|
||||
@@ -85,8 +85,8 @@ public class DatabaseManipulationSupplier implements DBase<Supplier>
|
||||
throws SQLException
|
||||
{
|
||||
String databaseName = "suppliers_parts_23";
|
||||
String userName = "root";
|
||||
String password = "root";
|
||||
String userName = "java";
|
||||
String password = "1KUZ4r6.73IGu*18";
|
||||
String URL2 = "com.mysql.jdbc.Driver";
|
||||
Connection con = new UtilitiesDatabase().connection(
|
||||
databaseName, userName, password, URL2);
|
||||
@@ -137,4 +137,61 @@ public class DatabaseManipulationSupplier implements DBase<Supplier>
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
DatabaseManipulationSupplier dmp = new DatabaseManipulationSupplier();
|
||||
System.out.println(dmp.listAll());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int delete(Supplier t) throws SQLException
|
||||
{
|
||||
String databaseName = "suppliers_parts_23";
|
||||
String userName = "java";
|
||||
String password = "1KUZ4r6.73IGu*18";
|
||||
String URL2 = "com.mysql.jdbc.Driver";
|
||||
Connection con = new UtilitiesDatabase().connection(
|
||||
databaseName, userName, password, URL2);
|
||||
int result = 0;
|
||||
PreparedStatement updateSupplier = null;
|
||||
try
|
||||
{
|
||||
if (con == null)
|
||||
{
|
||||
throw new RuntimeException("cannot connect to database");
|
||||
}
|
||||
|
||||
|
||||
updateSupplier = con.prepareStatement(
|
||||
//"UPDATE supplier SET snumber=?, sname=?, status=?, birthday=?, city=? WHERE snumber=?"
|
||||
"DELETE FROM supplier WHERE snumber=? "
|
||||
);
|
||||
updateSupplier.setString(1, t.getSnumber());
|
||||
result = updateSupplier.executeUpdate();
|
||||
}
|
||||
catch (SQLException ex)
|
||||
{
|
||||
System.err.println(ex.toString());
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
new UtilitiesDatabase().closeDatabaseConnection(con);
|
||||
// close the resources
|
||||
if (updateSupplier != null)
|
||||
{
|
||||
updateSupplier.close();
|
||||
}
|
||||
}
|
||||
catch (SQLException sqle)
|
||||
{
|
||||
sqle.printStackTrace();
|
||||
throw sqle;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.bl.supplier;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
import jakarta.faces.application.FacesMessage;
|
||||
import jakarta.faces.component.UIComponent;
|
||||
import jakarta.faces.context.FacesContext;
|
||||
import jakarta.faces.validator.FacesValidator;
|
||||
import jakarta.faces.validator.Validator;
|
||||
import jakarta.faces.validator.ValidatorException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
@FacesValidator("dateValidator")
|
||||
public class DateValidator implements Validator {
|
||||
|
||||
@Override
|
||||
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
|
||||
String dateStr = (String) value;
|
||||
|
||||
if (dateStr == null || dateStr.trim().isEmpty()) {
|
||||
return; // Empty values are not validated.
|
||||
}
|
||||
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
dateFormat.setLenient(false);
|
||||
|
||||
try {
|
||||
dateFormat.parse(dateStr);
|
||||
} catch (ParseException e) {
|
||||
throw new ValidatorException(new FacesMessage("Invalid date format. Please use yyyy-MM-dd."));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.bl.supplier;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
|
||||
import jakarta.faces.application.FacesMessage;
|
||||
import jakarta.faces.component.UIComponent;
|
||||
import jakarta.faces.context.FacesContext;
|
||||
import jakarta.faces.validator.FacesValidator;
|
||||
import jakarta.faces.validator.Validator;
|
||||
import jakarta.faces.validator.ValidatorException;
|
||||
|
||||
|
||||
@FacesValidator("numericValidator")
|
||||
public class NumericValidator implements Validator {
|
||||
|
||||
@Override
|
||||
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
|
||||
String input = value.toString();
|
||||
|
||||
if (input == null || !input.matches("\\d+")) {
|
||||
throw new ValidatorException(new FacesMessage("Please enter a valid numeric value."));
|
||||
}
|
||||
}
|
||||
}
|
@@ -17,9 +17,19 @@ public class Supplier implements Serializable
|
||||
private String birthday;
|
||||
private int status;
|
||||
private String city;
|
||||
|
||||
private boolean delete;
|
||||
private boolean modify;
|
||||
|
||||
public boolean isDelete()
|
||||
{
|
||||
return delete;
|
||||
}
|
||||
|
||||
public void setDelete(boolean delete)
|
||||
{
|
||||
this.delete = delete;
|
||||
}
|
||||
|
||||
public boolean isModify()
|
||||
{
|
||||
return modify;
|
||||
@@ -39,6 +49,7 @@ public class Supplier implements Serializable
|
||||
this.status = status;
|
||||
this.city = city;
|
||||
this.modify = false;
|
||||
this.delete = false;
|
||||
}
|
||||
|
||||
public String getCity()
|
||||
|
@@ -0,0 +1,59 @@
|
||||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:h="http://xmlns.jcp.org/jsf/html"
|
||||
xmlns:f="http://xmlns.jcp.org/jsf/core"
|
||||
xmlns:p="http://primefaces.org/ui">
|
||||
<h:head>
|
||||
<title>Facelet Title</title>
|
||||
</h:head>
|
||||
<h:body>
|
||||
<h:form>
|
||||
<p:growl id="globalgrowl" showDetail="false" showSummary="true" life="2000"/>
|
||||
|
||||
<h:dataTable var="part"
|
||||
value="#{partsBean.getParts()}" >
|
||||
<f:facet name="header"> <h:outputText value="Parts Table" style="font-size: 1.2em"/></f:facet>
|
||||
|
||||
<h:column>
|
||||
<f:facet name="header"> <h:outputText value="pnumber" /></f:facet>
|
||||
|
||||
<h:inputText disabled ="true" value="#{part.pnumber}"/>
|
||||
|
||||
</h:column>
|
||||
|
||||
<h:column>
|
||||
<f:facet name="header"> <h:outputText value="pname" /></f:facet>
|
||||
|
||||
<h:inputText disabled ="#{!part.modify}" value="#{part.pname}">
|
||||
<f:validateLength minimum="0" maximum="20" />
|
||||
</h:inputText>
|
||||
</h:column>
|
||||
<h:column>
|
||||
<f:facet name="header"> <h:outputText value="color" /></f:facet>
|
||||
|
||||
<h:inputText disabled ="#{!part.modify}" value="#{part.color}">
|
||||
<f:validateLength minimum="0" maximum="20" />
|
||||
</h:inputText>
|
||||
</h:column>
|
||||
|
||||
<h:column>
|
||||
<f:facet name="header"> <h:outputText value="city" /></f:facet>
|
||||
|
||||
<h:inputText disabled ="#{!part.modify}" value="#{part.city}">
|
||||
<f:validateLength minimum="0" maximum="20" />
|
||||
</h:inputText>
|
||||
</h:column>
|
||||
|
||||
|
||||
<h:column>
|
||||
<f:facet name="header"> <h:outputText value="update" /></f:facet>
|
||||
<h:selectBooleanCheckbox onclick="submit()"
|
||||
value="#{part.modify}">
|
||||
</h:selectBooleanCheckbox >
|
||||
</h:column>
|
||||
</h:dataTable>
|
||||
<h:commandButton value="Save" action="#{partsBean.saveFromUpdate()}" />
|
||||
</h:form>
|
||||
</h:body>
|
||||
</html>
|
@@ -19,29 +19,34 @@
|
||||
<f:facet name="header"> <h:outputText value="snumber" /></f:facet>
|
||||
|
||||
<h:inputText disabled ="true" value="#{supplier.snumber}"/>
|
||||
|
||||
</h:column>
|
||||
|
||||
<h:column>
|
||||
<f:facet name="header"> <h:outputText value="sname" /></f:facet>
|
||||
|
||||
<h:inputText disabled ="#{!supplier.modify}" value="#{supplier.sname}"/>
|
||||
<h:inputText disabled ="#{!supplier.modify}" value="#{supplier.sname}">
|
||||
<f:validateLength minimum="3" maximum="20" />
|
||||
</h:inputText>
|
||||
</h:column>
|
||||
<h:column>
|
||||
<f:facet name="header"> <h:outputText value="birthday" /></f:facet>
|
||||
|
||||
<h:inputText disabled ="#{!supplier.modify}" value="#{supplier.birthday}"/>
|
||||
<h:inputText disabled ="#{!supplier.modify}" value="#{supplier.birthday}" validator="dateValidator"/>
|
||||
</h:column>
|
||||
|
||||
<h:column>
|
||||
<f:facet name="header"> <h:outputText value="status" /></f:facet>
|
||||
<f:facet name="header"> <h:outputText value="status"/></f:facet>
|
||||
|
||||
<h:inputText disabled ="#{!supplier.modify}" value="#{supplier.status}"/>
|
||||
<h:inputText disabled ="#{!supplier.modify}" value="#{supplier.status}" validator="numericValidator"/>
|
||||
</h:column>
|
||||
|
||||
<h:column>
|
||||
<f:facet name="header"> <h:outputText value="city" /></f:facet>
|
||||
|
||||
<h:inputText disabled ="#{!supplier.modify}" value="#{supplier.city}"/>
|
||||
<h:inputText disabled ="#{!supplier.modify}" value="#{supplier.city}">
|
||||
<f:validateLength minimum="3" maximum="20" />
|
||||
</h:inputText>
|
||||
</h:column>
|
||||
|
||||
|
||||
@@ -51,6 +56,9 @@
|
||||
value="#{supplier.modify}">
|
||||
</h:selectBooleanCheckbox >
|
||||
</h:column>
|
||||
<h:column>
|
||||
<h:commandButton value="Delete" action="#{supplierBean.}"
|
||||
</h:column>
|
||||
</h:dataTable>
|
||||
<h:commandButton value="Save" action="#{supplierBean.saveFromUpdate()}" />
|
||||
</h:form>
|
||||
|
Reference in New Issue
Block a user