Ajax Smajax
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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 beans;
|
||||
|
||||
import jakarta.inject.Named;
|
||||
import jakarta.enterprise.context.SessionScoped;
|
||||
import jakarta.faces.application.FacesMessage;
|
||||
import java.io.Serializable;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.primefaces.PrimeFaces;
|
||||
import pojo.DatabaseManipulator;
|
||||
import pojo.Passenger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
@Named(value="passengerBean")
|
||||
@SessionScoped
|
||||
public class PassengerBean implements Serializable {
|
||||
DatabaseManipulator dm = new DatabaseManipulator();
|
||||
|
||||
private Passenger selectedPassenger;
|
||||
|
||||
/**
|
||||
* Get the value of selectedPassenger
|
||||
*
|
||||
* @return the value of selectedPassenger
|
||||
*/
|
||||
public Passenger getSelectedPassenger()
|
||||
{
|
||||
return selectedPassenger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of selectedPassenger
|
||||
*
|
||||
* @param selectedPassenger new value of selectedPassenger
|
||||
*/
|
||||
public void setSelectedPassenger(Passenger selectedPassenger)
|
||||
{
|
||||
this.selectedPassenger = selectedPassenger;
|
||||
}
|
||||
|
||||
public List<Passenger> getPassengers() {
|
||||
List<Passenger> data = null;
|
||||
try {
|
||||
data = dm.listAll();
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(PassengerBean.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
System.out.println("Fetched data from database!");
|
||||
System.out.println(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/** Creates a new instance of PassengerBean */
|
||||
public PassengerBean() {
|
||||
}
|
||||
|
||||
public void showMessage() {
|
||||
String messageContent = "You selected the " + selectedPassenger.getBirthday().toString() + "birthday.";
|
||||
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", messageContent);
|
||||
PrimeFaces.current().dialog().showMessageDynamic(message);
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package edu.slcc.asdv.caleb.finalexamproblem2_calebfontenot;
|
||||
|
||||
import jakarta.ws.rs.ApplicationPath;
|
||||
import jakarta.ws.rs.core.Application;
|
||||
|
||||
/**
|
||||
* Configures Jakarta RESTful Web Services for the application.
|
||||
* @author Juneau
|
||||
*/
|
||||
@ApplicationPath("resources")
|
||||
public class JakartaRestConfiguration extends Application {
|
||||
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package edu.slcc.asdv.caleb.finalexamproblem2_calebfontenot.resources;
|
||||
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author
|
||||
*/
|
||||
@Path("jakartaee10")
|
||||
public class JakartaEE10Resource {
|
||||
|
||||
@GET
|
||||
public Response ping(){
|
||||
return Response
|
||||
.ok("ping Jakarta EE")
|
||||
.build();
|
||||
}
|
||||
}
|
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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 pojo;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Date;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import pojo.Passenger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class DatabaseManipulator {
|
||||
public List<Passenger> listAll()
|
||||
throws SQLException
|
||||
{
|
||||
// test exception code
|
||||
//if ( true)
|
||||
// throw new SQLException();
|
||||
|
||||
List<Passenger> passengerTable = new ArrayList<Passenger>();
|
||||
String databaseName = "travel";
|
||||
String userName = "java";
|
||||
String password = "8VCS49HT2xjsEZvC";
|
||||
String URL2 = "com.mysql.jdbc.Driver";
|
||||
Connection con = new UtilitiesDatabase().connection(
|
||||
databaseName, userName, password, URL2);
|
||||
PreparedStatement ps = null;
|
||||
try
|
||||
{
|
||||
if (con == null)
|
||||
{
|
||||
throw new RuntimeException("cannot connect to database");
|
||||
}
|
||||
String table = "";
|
||||
ResultSet rs = null;
|
||||
String sqlStr = "SELECT * FROM passenger";
|
||||
//prepare statement
|
||||
ps = con.prepareStatement(sqlStr);
|
||||
//execute
|
||||
rs = ps.executeQuery();
|
||||
int row = 0;
|
||||
while (rs.next())
|
||||
{
|
||||
int pid = rs.getInt(1);
|
||||
String pname = rs.getString(2);
|
||||
Date bdate = bdate = rs.getDate(3);
|
||||
Passenger passenger = new Passenger(pid, pname, bdate);
|
||||
passengerTable.add(passenger);
|
||||
row++;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
new UtilitiesDatabase().closeDatabaseConnection(con);
|
||||
// close the resources
|
||||
if (ps != null)
|
||||
{
|
||||
ps.close();
|
||||
}
|
||||
}
|
||||
catch (SQLException sqle)
|
||||
{
|
||||
System.out.println(sqle);
|
||||
sqle.printStackTrace();
|
||||
throw sqle;
|
||||
}
|
||||
}
|
||||
return passengerTable;
|
||||
}
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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 pojo;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Passenger {
|
||||
|
||||
private int pid;
|
||||
private String pname;
|
||||
private Date birthday;
|
||||
|
||||
public Passenger(int pid, String pname, Date birthday)
|
||||
{
|
||||
this.pid = pid;
|
||||
this.pname = pname;
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of birthday
|
||||
*
|
||||
* @return the value of birthday
|
||||
*/
|
||||
public Date getBirthday()
|
||||
{
|
||||
return birthday;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of birthday
|
||||
*
|
||||
* @param birthday new value of birthday
|
||||
*/
|
||||
public void setBirthday(Date birthday)
|
||||
{
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
|
||||
public String getPname()
|
||||
{
|
||||
return pname;
|
||||
}
|
||||
|
||||
public void setPname(String pname)
|
||||
{
|
||||
this.pname = pname;
|
||||
}
|
||||
|
||||
|
||||
public int getPid()
|
||||
{
|
||||
return pid;
|
||||
}
|
||||
|
||||
public void setPid(int pid)
|
||||
{
|
||||
this.pid = pid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return birthday.toString();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* 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 pojo;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author asdv5
|
||||
*/
|
||||
public class UtilitiesDatabase
|
||||
{
|
||||
public static Connection connection(
|
||||
String databaseName,
|
||||
String userName,
|
||||
String password,
|
||||
String URL2
|
||||
|
||||
) //throws InstantiationException, IllegalAccessException
|
||||
{
|
||||
/*
|
||||
String databaseName = "suppliers_parts_23";
|
||||
String userName = "root";
|
||||
String password = "root";
|
||||
String URL2 = "com.mysql.jdbc.Driver";
|
||||
Connection con = null;
|
||||
*/
|
||||
Connection con;
|
||||
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 + ":3306/" + databaseName + "?allowPublicKeyRetrieval=true&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 successful!");
|
||||
return con;
|
||||
}
|
||||
|
||||
|
||||
public void closeDatabaseConnection( Connection con)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (con != null)
|
||||
{
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
System.out.println(e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static java.sql.Date stringDateToSqlDate(String sDate)
|
||||
{
|
||||
java.util.Date date = null;
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
try
|
||||
{
|
||||
date = sdf.parse(sDate);
|
||||
}
|
||||
catch (ParseException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new java.sql.Date( date.getTime() );
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static java.util.Date stringDateToJavaUtilitiesDate(String sDate)
|
||||
{
|
||||
java.sql.Date sqldate = stringDateToSqlDate(sDate);
|
||||
return new java.util.Date(sqldate.getTime() );
|
||||
}
|
||||
public static void main(String[] args)
|
||||
{
|
||||
connection("travel", "java", "8VCS49HT2xjsEZvC", "com.mysql.jdbc.Driver");
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence version="3.0" xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd">
|
||||
<!-- Define Persistence Unit -->
|
||||
<persistence-unit name="my_persistence_unit">
|
||||
|
||||
</persistence-unit>
|
||||
</persistence>
|
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd"
|
||||
bean-discovery-mode="all">
|
||||
</beans>
|
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
|
||||
|
||||
This program and the accompanying materials are made available under the
|
||||
terms of the Eclipse Public License v. 2.0, which is available at
|
||||
http://www.eclipse.org/legal/epl-2.0.
|
||||
|
||||
This Source Code may also be made available under the following Secondary
|
||||
Licenses when the conditions for such availability set forth in the
|
||||
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
|
||||
version 2 with the GNU Classpath Exception, which is available at
|
||||
https://www.gnu.org/software/classpath/license.html.
|
||||
|
||||
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>
|
||||
<property name="keepgenerated" value="true">
|
||||
<description>Keep a copy of the generated servlet class' java code.</description>
|
||||
</property>
|
||||
</jsp-config>
|
||||
</glassfish-web-app>
|
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app version="5.0" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd">
|
||||
<context-param>
|
||||
<param-name>jakarta.faces.PROJECT_STAGE</param-name>
|
||||
<param-value>Development</param-value>
|
||||
</context-param>
|
||||
<servlet>
|
||||
<servlet-name>Faces Servlet</servlet-name>
|
||||
<servlet-class>jakarta.faces.webapp.FacesServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>Faces Servlet</servlet-name>
|
||||
<url-pattern>/faces/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
<session-config>
|
||||
<session-timeout>
|
||||
30
|
||||
</session-timeout>
|
||||
</session-config>
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.xhtml</welcome-file>
|
||||
</welcome-file-list>
|
||||
</web-app>
|
@@ -0,0 +1,40 @@
|
||||
<?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="jakarta.faces.html"
|
||||
xmlns:f="jakarta.faces.core"
|
||||
xmlns:p="http://primefaces.org/ui">
|
||||
<h:head>
|
||||
<title>Facelet Title</title>
|
||||
</h:head>
|
||||
<h:body>
|
||||
<h:form>
|
||||
<p:growl id="messages" showDetail="true" />
|
||||
<h:dataTable value="#{passengerBean.getPassengers()}" var="currentValue" border="1">
|
||||
<f:facet name="header">
|
||||
pid
|
||||
</f:facet>
|
||||
<f:facet name="header">
|
||||
pname
|
||||
</f:facet>
|
||||
<f:facet name="header">
|
||||
birthday
|
||||
</f:facet>
|
||||
<h:column>
|
||||
#{currentValue.pid}
|
||||
</h:column>
|
||||
<h:column>
|
||||
#{currentValue.pname}
|
||||
</h:column>
|
||||
<h:column>
|
||||
#{currentValue.birthday}
|
||||
</h:column>
|
||||
</h:dataTable>
|
||||
<h:selectOneMenu id="select-birthday" value="#{passengerBean.selectedPassenger}">
|
||||
<f:selectItems value="#{passengerBean.getPassengers()}" />
|
||||
</h:selectOneMenu>
|
||||
|
||||
<p:commandButton value="Show Birthday info" action="#{passengerBean.showMessage}" update="messages" />
|
||||
</h:form>
|
||||
</h:body>
|
||||
</html>
|
Reference in New Issue
Block a user