WebDev Hell

This commit is contained in:
2023-11-03 13:34:02 -05:00
parent 45656bf9ed
commit 473a7e61e9
133 changed files with 4855 additions and 179 deletions

View File

@@ -4,6 +4,7 @@
*/
package edu.slcc.asdv.bl;
import java.sql.SQLException;
import java.util.List;
/**
@@ -12,4 +13,7 @@ import java.util.List;
*/
public interface DBase<T> {
List<? extends List<T>> listAll();
int delete( T t)
throws SQLException;
}

View File

@@ -8,7 +8,6 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;
@@ -18,17 +17,17 @@ import jakarta.inject.Named;
*/
@Named(value = "suppliers")
@ApplicationScoped
public class Suppliers implements Serializable {
public class Suppliers implements Serializable
{
String snumber = "";
String sname = "";
int status = 0;
String birthday = "";
String city = "";
String result = "";
public Suppliers()
{
connection();
@@ -87,53 +86,91 @@ public class Suppliers implements Serializable {
public String getConnectionResponse()
{
Connection con = connection();
if (con == null) {
result = "cannot connect to database";
if (con == null)
{
result = "cannot connect to database" ;
return null;
}
if (con != null) {
}
if (con != null)
{
return "<p style=\"color:green\">Connection succesfull! <br />";
} else {
}
else
{
connection();
return "<p style=\"color:red\">Connection failed! <br />";
}
}
}
public static Connection getConnection(String databaseName, String userName, String password) {
private Connection connection() //throws InstantiationException, IllegalAccessException
{
/*
String databaseName = "suppliers_parts_23";
String userName = "admin";
String password = "RangerDog01!";
String URL2 = "com.mysql.jdbc.Driver";
Connection con = null;
*/
String URL2 = "com.mysql.jdbc.Driver";
Connection con = null;
try {// Load Sun's jdbc driver
Class.forName(URL2).newInstance();
System.out.println("JDBC Driver loaded!");
} catch (Exception e) // driver not found
}
catch (Exception e) // driver not found
{
System.err.println("Unable to load database driver");
System.err.println("Details : " + e);
return null;
}
String ip = "localhost"; //internet connection
String url = "jdbc:mysql://" + ip + ":3306/" + databaseName + "?useSSL=false";
System.out.println(url);
String url = "jdbc:mysql://" + ip + ":8889/" + databaseName + "?useSSL=false";
try {
con = DriverManager.getConnection(url, userName, password);
con.setReadOnly(false);
} catch (Exception e) {
}
catch (Exception e) {
System.err.println(e.toString());
return null;
}
System.out.println("connection successfull");
return con;
}
private Connection connection() //throws InstantiationException, IllegalAccessException
{
String databaseName = "suppliers_parts_23";
String userName = "root";
String password = "root";
String URL2 = "com.mysql.jdbc.Driver";
Connection con = null;
try
{// Load Sun's jdbc driver
Class.forName(URL2).newInstance();
System.out.println("JDBC Driver loaded!");
}
catch (Exception e) // driver not found
{
System.err.println("Unable to load database driver");
System.err.println("Details : " + e);
return null;
}
String ip = "localhost"; //internet connection
String url = "jdbc:mysql://" + ip + ":8889/" + databaseName + "?useSSL=false";
try
{
con = DriverManager.getConnection(url, userName, password);
con.setReadOnly(false);
}
catch (Exception e)
{
System.err.println(e.toString());
return null;
}
System.out.println("connection successfull");
return con;
}
public void clear()
{
status = 0;
@@ -147,63 +184,72 @@ public class Suppliers implements Serializable {
public void listAll()
{
Connection con = connection();
if (con == null) {
result = "cannot connect to database";
return;
}
if (con == null)
{
result = "cannot connect to database" ;
return ;
}
String table = "";
PreparedStatement ps = null;
ResultSet rs = null;
String sqlStr = "SELECT * FROM supplier";
try {
try
{
//prepare statement
ps = con.prepareStatement(sqlStr);
//execute
rs = ps.executeQuery();
int row = 0;
while (rs.next()) {
while (rs.next())
{
String sNumber = rs.getString(1) + " ";
String sName = rs.getString(2) + " ";
String bdate = rs.getDate(3) + " ";
String status = rs.getInt(4) + " ";
String bdate = rs.getDate(3) + " ";
String city = rs.getString(5) + " ";
table += sNumber + sName + bdate + status + city + "</br>";
row++;
}
} catch (Exception ex) {
}
}
catch (Exception ex)
{
ex.printStackTrace();
} finally {
try {
closeDatabaseConnection(con);
}
finally
{
try
{
closeDatabaseConnection( con);
// close the resources
if (ps != null) {
if (ps != null)
{
ps.close();
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}
}
catch (SQLException sqle){ sqle.printStackTrace(); }
}
result = table;
}
public void viewSupplier()
{
Connection con = connection();
if (con == null) {
result = "cannot connect to database";
return;
}
if (con == null)
{
result = "cannot connect to database" ;
return ;
}
String ret = "";
PreparedStatement ps = null;
ResultSet rs = null;
String sqlStr = "SELECT snumber, sname, status, city, birthday FROM supplier WHERE snumber=?";
try {
String sqlStr = "SELECT * FROM supplier WHERE snumber=?";
try
{
//prepare statement
ps = con.prepareStatement(sqlStr);
ps.setString(1, snumber);
//execute
rs = ps.executeQuery();
if (rs.next()) {
if (rs.next())
{
this.snumber = rs.getString("snumber");
ret += this.snumber + " ";
this.sname = rs.getString("sname");
@@ -212,110 +258,133 @@ public class Suppliers implements Serializable {
ret += this.status + " ";
this.city = rs.getString("city");
ret += this.city + " ";
Object bDate = rs.getObject("birthday");
this.birthday = bDate.toString();
ret += this.birthday;
} else {
ret += this.birthday;
}
else
ret = this.snumber + " doesn't exist.";
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
}
catch (Exception ex){ex.printStackTrace();}
finally
{
try
{
this.closeDatabaseConnection(con);
if (ps != null) {
if (ps != null)
ps.close();
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}
catch (SQLException sqle){ sqle.printStackTrace();}
}
this.result = ret;
}
public void updateSupplier()
{
Connection con = connection();
if (con == null) {
result = "cannot connect to database";
return;
}
if (con == null)
{
result = "cannot connect to database" ;
return ;
}
PreparedStatement updateSupplier = null;
try {
try
{
updateSupplier = con.prepareStatement(
"UPDATE supplier SET snumber=?, sname=?, status=?, birthday=?, city=? WHERE snumber=?");
updateSupplier.setString(1, snumber);
updateSupplier.setString(2, sname);
updateSupplier.setInt(3, status);
java.sql.Date bd = stringDateToSqlDate(this.birthday);
updateSupplier.setDate(4, bd);
java.sql.Date bd = stringDateToSqlDate(this.birthday);
updateSupplier.setDate(4, bd );
updateSupplier.setString(5, city);
updateSupplier.setString(6, snumber);
int updateCount = updateSupplier.executeUpdate();
result = "number of rows affected: " + updateCount;
} catch (Exception ex) {
}
catch (Exception ex)
{
System.err.println(ex.toString());
} finally {
try {
}
finally
{
try
{
this.closeDatabaseConnection(con);
// close the resources
if (updateSupplier != null) {
if (updateSupplier != null)
{
updateSupplier.close();
}
}
} catch (SQLException sqlee) {
}
catch (SQLException sqlee)
{
sqlee.printStackTrace();
}
}
}
}
}
public void deleteSupplier()
{
Connection con = connection();
if (con == null) {
result = "cannot connect to database";
return;
}
if (con == null)
{
result = "cannot connect to database" ;
return ;
}
PreparedStatement ps = null;
int rowsAffected = -1;
try {
try
{
String query = "DELETE FROM supplier WHERE snumber=? ";
ps = con.prepareStatement(query);
ps.setString(1, snumber);
rowsAffected = ps.executeUpdate();
result = "number of rows affected: " + rowsAffected;
} catch (Exception ex) {
}
catch (Exception ex)
{
System.err.println(ex.toString());
} finally {
try {
}
finally
{
try
{
this.closeDatabaseConnection(con);
// close the resources
if (ps != null) {
if (ps != null)
{
ps.close();
}
}
} catch (SQLException sqlee) {
}
catch (SQLException sqlee)
{
sqlee.printStackTrace();
}
}
}
}
}
public void insertSupplier()
{
Connection con = connection();
if (con == null) {
result = "cannot connect to database";
return;
}
Connection con = connection();
if (con == null)
{
result = "cannot connect to database" ;
return ;
}
PreparedStatement updateSupplier = null;
try {
try
{
updateSupplier = con.prepareStatement(
"INSERT INTO supplier (snumber, sname, status, birthday, city ) "
+ "VALUES ( ?, ?, ? , ? ,? )");
@@ -329,35 +398,45 @@ public class Suppliers implements Serializable {
int updateCount = updateSupplier.executeUpdate();
result = "number of rows affected: " + updateCount;
} catch (Exception ex) {
}
catch (Exception ex)
{
System.err.println(ex.toString());
result = ex.toString();
} finally {
try {
}
finally
{
try
{
this.closeDatabaseConnection(con);
// close the resources
if (updateSupplier != null) {
if (updateSupplier != null)
{
updateSupplier.close();
}
}
} catch (SQLException e) {
}
catch (SQLException e)
{
System.err.println(e.toString());
result = e.toString();
}
}
}
}
}
private java.sql.Date stringDateToSqlDate(String sDate)
private java.sql.Date stringDateToSqlDate(String sDate)
{
java.util.Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
try
{
date = sdf.parse(sDate);
} catch (ParseException e) {
}
catch (ParseException e)
{
e.printStackTrace();
}
return new java.sql.Date(date.getTime());
}
return new java.sql.Date( date.getTime() );
}
public String getResult()
@@ -366,15 +445,21 @@ public class Suppliers implements Serializable {
}
public void closeDatabaseConnection(Connection con)
public void closeDatabaseConnection( Connection con)
{
try {
if (con != null) {
try
{
if (con != null)
{
con.close();
}
} catch (SQLException e) {
}
}
catch (SQLException e)
{
result = e.toString();
e.printStackTrace();
}
}
}
}

View File

@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<!--
Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
@@ -15,6 +14,7 @@
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
<class-loader delegate="true"/>
<jsp-config>