Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,899 members, 7,802,906 topics. Date: Saturday, 20 April 2024 at 02:55 AM

Learn Java EE With Me - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Learn Java EE With Me (6127 Views)

Is It Advisable To Learn Java As My First Programming Language? / Java EE Developer And Spring Developer In Here. / SOFTWARE DEVELOPER:- Java(ee And SE) And Android/ios (2) (3) (4)

(1) (2) (3) (4) (Reply) (Go Down)

Learn Java EE With Me by ugwum007(m): 1:00pm On Sep 18, 2016
Purpose of the thread:
I want to have a taste of Java EE after dwelling on SE. I am learning and will like advice and inputs from the gurus while others can also learn from the thread.

Materials:
I am making use of the tutorials from tutorialpoint.com:
Learn ejb
Learn jpa
Learn maven
Learn hibernate
Learn jsp

The example used in the learn ejb didn't work so I had to make use of things outside the box to make it work.



IDE: Netbeans 8.1
Application server: wildfly 10 formerly jboss
Database server: postgres sql database server

1 Like

Re: Learn Java EE With Me by ugwum007(m): 1:10pm On Sep 18, 2016
The example I will be using will be:

Developing a beer inventory application.
Its functionality:
- add beer to the inventory specifing the name, price, type, catalogue number.
- the beers will be classified into 3: herbs, bitters and pure acohcol.
- in order to avoid typo, the catalogue number will be tied to the beer type: 4500 - herbs, 5500 - bitters and 6600 - pure acohcol
Re: Learn Java EE With Me by ugwum007(m): 1:14pm On Sep 18, 2016
I started from the simplest i.e stateless bean without persistence
Re: Learn Java EE With Me by ugwum007(m): 1:21pm On Sep 18, 2016
Steps:

Download the necessary files: Netbeans ide, wildfly 10 server, postgres database server

Configure database server

Configure the wildfly server, add datasource (postgres)

Add the wildfly server instance to Netbeans and make sure everything is working fine.

Also connect your postgres database to Netbeans so it will be easy to create tables and execute queries.
Re: Learn Java EE With Me by ugwum007(m): 3:14pm On Sep 18, 2016
Configuring database server:

During installation, a database with these attributes below is created automatically but you can still create yours.

database name: postgresdb
username: postgres
password: you will be prompt to add that.


Configuring wildfly application server:

After downloading the zipped file, unpack in your favorite location. In my case, i unpacked it to C:/


Adding wildfly server Instance to netbeans:
-In the netbeans window, click on the Services tab, right-click on SERVER, choose add Server
-choose wildfly application server from the list and click next
-click on browse, navigate to the location where the wildfly was unpacked and choose it as the server location.
-server configuration field will be automatically filled if all is done right. Then click next
-on the next window, I changed my Port to 8050 because I have other server that use the port.
-management port was left at default and finally click on finish.
-
Re: Learn Java EE With Me by ugwum007(m): 3:25pm On Sep 18, 2016
Start up the server and watch the server console/Netbeans console for something like these:

Calling "C:\wildfly-10\bin\standalone.conf.bat"
"JAVA_OPTS already set in environment; overriding default settings with values: -Xms128m -Xmx512m -server -Dhttp.nonProxyHosts=localhost,127.0.0.1,STEINACOZ-PC -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true -Djboss.management.http.port=9990 -Djboss.http.port=8050"
Setting JAVA property to "C:\Program Files\Java\jdk1.8.0_51\bin\java"
===============================================================================

JBoss Bootstrap Environment

JBOSS_HOME: "C:\wildfly-10"

JAVA: "C:\Program Files\Java\jdk1.8.0_51\bin\java"

JAVA_OPTS: "-Dprogram.name=standalone.bat -Xms128m -Xmx512m -server -Dhttp.nonProxyHosts=localhost,127.0.0.1,STEINACOZ-PC -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true -Djboss.management.http.port=9990 -Djboss.http.port=8050"



if you see the warning of JAVA HOME not set, go back the server location (where it was unPacked), open the folder, open the BIN folder, use Visual studio code or any suitable editor to open STANDALONE.CONF.

Add these two lines to it probably after line 23:

$JAVA="C:\Program Files\Java\jre1.8.0_51\bin"
$JAVA_HOME="C:\Program Files\Java\jdk1.8.0_51"


the paths should be according to your own Java installation.

finally, restart the server again
Re: Learn Java EE With Me by ugwum007(m): 4:21pm On Sep 18, 2016
Create first project that will contain the Remote Interface and Session Bean:

-Using netbeans New Project wizard, Select JAVA EE and click on EJB Module
- Use Beer_EJBModule as project name
- Choose Wildfly application server
- Create two java packages:
com.Beer.Bean
com.Beer.Remote

-Create a Java Interface inside com.Beer.Remote named BeerBeanRemote
-Create a Java Class inside com.Beer.Bean named BeerBeanRemote, This class will implement the BeerBeanRemote Interface
Re: Learn Java EE With Me by ugwum007(m): 4:27pm On Sep 18, 2016
Inside the BeerBeanRemote Interface:

package com.Beer.Remote;

import java.util.List;
import javax.ejb.Remote;

@Remote //annotation showing that this is a remote interface
public interface BeerBeanRemote {


void addBeer(String beerName, int catalogueNumber, String type, double beerPrice); //adds a beer with its attributes

List getBeers(); // returns all the beers added

List getBeerName(); //returns all the beers name

List getBeerNumber(); //returns all the beers catalogue number

List getBeerType(); //returns all the beers type

List getBeerPrice(); // returns all the beers price in naira

}

I prefer using annotations, it keeps the code simple and easy to understand.
Re: Learn Java EE With Me by ugwum007(m): 4:45pm On Sep 18, 2016
Inside the BeerSessionBean class:

package com.Beer.Bean;

import com.Beer.Remote.BeerBeanRemote;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;



@Stateless //the session bean is stateless
public class BeerSessionBean implements BeerBeanRemote{ //This Bean class implements its remote interface (BeerbeanRemote)

List<String> beerAll; //List containing all the beers in the inventory

List<String> beerNameShelf; //List containing all the beer names in the inventory

List<Integer> beerNumberShelf; //List containing all the beer Catalogue Numbers in the inventory

List<String> beerTypeShelf; //List containing all the beer types in the inventory

List<Double> beerPriceShelf; //List containing all the beer prices in the inventory

public BeerSessionBean() {
this.beerAll = new ArrayList<String>();
this.beerNameShelf = new ArrayList<String>();
this.beerNumberShelf = new ArrayList<Integer>();
this.beerTypeShelf = new ArrayList<String>();
this.beerPriceShelf = new ArrayList<Double>();
}



@Override
public void addBeer(String beerName, int catalogueNumber, String type, double beerPrice) {
beerAll.add(beerName); //adds beers to the inventory

beerNameShelf.add(beerName); //adds beer names to the inventory

beerNumberShelf.add(catalogueNumber); //adds beer Catalogue numbers to the inventory

beerTypeShelf.add(beerName); //adds beer types to the inventory

beerPriceShelf.add(beerPrice); //adds beer prices to the inventory

}

//returns the beers according to names
public List<String> getBeers() {
return beerNameShelf;
}

//returns the beers according to names
public List getBeerName() {
return beerNameShelf;
}

//returns the beers according to Catalogue numbers
public List<Integer> getBeerNumber() {
return beerNumberShelf;
}

//returns the beers according to types
public List<String> getBeerType() {
return beerTypeShelf;
}

////returns the beers according to Price
public List<Double> getBeerPrice() {
return beerPriceShelf;
}


}



The session bean class will implement the methods from the remote interface.

finally, clean and build the Beer_EJBModule project and deploy to the application server.

There are two ways of deploying to Wildfly application Server:
- Right click on the Beer_EJBModule in netbeans project tab, choose clean and build. After that right click again, then choose deploy.
- Right-click on the Beer_EJBModule in netbeans project tab, choose clean and build, Switch to netebeans files tab, copy the beer_EJBModule.jar file and paste to wildfly/standalone/deployments. You can optionally restart the server.


After deploying, look out on the server console/netbeans console for these:

23:31:07,872 INFO [org.jboss.as.ejb3.deployment] (MSC service thread 1-2) WFLYEJB0473: JNDI bindings for session bean named 'BeerSessionBean' in deployment unit 'deployment "Beer_EJBModule.jar"' are as follows:

java:global/Beer_EJBModule/BeerSessionBean!com.Beer.Remote.BeerBeanRemote
java:app/Beer_EJBModule/BeerSessionBean!com.Beer.Remote.BeerBeanRemote
java:module/BeerSessionBean!com.Beer.Remote.BeerBeanRemote
java:jboss/exported/Beer_EJBModule/BeerSessionBean!com.Beer.Remote.BeerBeanRemote
java:global/Beer_EJBModule/BeerSessionBean
java:app/Beer_EJBModule/BeerSessionBean
java:module/BeerSessionBean


one of these JNDI bindings will be used for JNDI lookup later
Re: Learn Java EE With Me by ugwum007(m): 4:55pm On Sep 18, 2016
Is time to create the Client that the user will have access to in order to add beers to the inventory, and know the number of beers in the inventory. Remember the session bean is stateless and uses remote interface which means that calls to the server will slow down operations and retrieved data is discarded after each lookup.

-From Netbeans create new project wizard, choose Java, click on Java application
-Name the project BeerClientApp, name the package beerclientapp, and name the main class BeerClientApp
Re: Learn Java EE With Me by ugwum007(m): 6:55pm On Sep 18, 2016
Inside the BeerClientApp class:

package beerclientapp;

import com.Beer.Remote.BeerBeanRemote;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class BeerClientApp {
BufferedReader brConsoleReader = null; //i will take typed -in inputs from the console
Properties props;
InitialContext ctx;


public static void main(String[] args) {

BeerClientApp beerClientApp = new BeerClientApp();
beerClientApp.showingEJB();
}

private void showingEJB(){
try{
props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"wink;
props.put(Context.PROVIDER_URL, "http-remoting://localhost:8050"wink;
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"wink;
props.put(Context.SECURITY_PRINCIPAL, "steinacoz"wink;
props.put(Context.SECURITY_CREDENTIALS, "nkenna007"wink;
props.put("jboss.naming.client.ejb.context", true);

ctx = new InitialContext(props);

}catch(NamingException ex){
ex.printStackTrace();
}

brConsoleReader = new BufferedReader(new InputStreamReader(System.in));

try{
int choice = 1;
BeerBeanRemote beerBeanRemote = (BeerBeanRemote) ctx.lookup("Beer_EJBModule/BeerSessionBean!com.Beer.Remote.BeerBeanRemote"wink;
System.out.println("Beer Inventory Program"wink;
System.out.println("Enter 1 to add beer (Name, Catalogue Number, type, price"wink;
System.out.println("Enter 2 to stop and print all beers added"wink;

while(choice != 2){
String beerName;
int number;
String type;
double price;

String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);

if(choice == 1){
System.out.println("Enter beer Name"wink;
beerName = brConsoleReader.readLine();

System.out.println("Enter beer catalogue Number (four digits)"wink;
System.out.println("4500 = herb, 5500 = bitters, 6600 = pure alcohol"wink;
number = Integer.parseInt(brConsoleReader.readLine());

if(number == 4500){
type = "herb";
}else if(number == 5500){
type = "bitters";
}else if(number == 6600){
type = "pure alcohol";
}else{
System.out.println("Catalogue number incorrect"wink;
}


System.out.println("Enter Beer price (already in naria)"wink;
price = Double.parseDouble(brConsoleReader.readLine());

beerBeanRemote.addBeer(beerName, number, type, price);

}else if(choice == 2){
break;
}
}

List<String> beerList = beerBeanRemote.getBeers();
List<Integer> beerNumberList = beerBeanRemote.getBeerNumber();
List<String> beerTypeList = beerBeanRemote.getBeerType();
List<Double> beerPriceList = beerBeanRemote.getBeerPrice();

System.out.println("Number of Beers in the Inventory: " + beerList.size());

for(int i = 0; i < beerList.size(); i++){
System.out.println("Beer Name: "+ beerList.get(i) +" Catalogue Number: "+ beerNumberList.get(i) +" Type: "+ beerTypeList.get(i) +" Price: "+ beerPriceList.get(i));
}

}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}finally{
try{
if(brConsoleReader != null){
brConsoleReader.close();
}
}catch(IOException ex){
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}

}


before clean and build, add JBoss-client.jar to the project library and then run
Re: Learn Java EE With Me by ugwum007(m): 6:57pm On Sep 18, 2016
Next up is adding persistence functionality
Re: Learn Java EE With Me by NaijaTroops(m): 10:39am On Sep 19, 2016
well done sir, i'm really interest in following this tutorial, thank you for your effort
Re: Learn Java EE With Me by ugwum007(m): 11:29am On Sep 19, 2016
NaijaTroops:
well done sir, i'm really interest in following this tutorial, thank you for your effort

welcome
Re: Learn Java EE With Me by ugwum007(m): 7:47pm On Sep 19, 2016
Don't mind those funny faces in the code, I don't know where it came from.
Before starting this, first add datasource (postgres db) to the application server (wildfly).

-Download postgres db driver from maven repository or just google it.

-Copy the driver actually is a jar file to the wildfly/standalone/deployments/ folder.

-Restart the server.
-the driver have been deployed. To confirm, open the server dashboard on the browser (localhost/9900), under deployed applications, you will see the driver.
Re: Learn Java EE With Me by ugwum007(m): 10:47pm On Sep 20, 2016
- To add a new datasource to the wildfly server, open the localhost console (localhost:9990/console)
- click on the configuration tab
- Select subsystems > datasources > Non -XA > add new
- on the create datasource window, select postgresSQL datasource, click next
- Name = postgresDS
JNDI name = java:/jboss/datasources/PostgresDS
Click next
- switch to Detected driver tab and select the deployed postgres database driver (jar file)
- switch back to specfiy driver, the fields should be filled automatically, click next
- the next page should contain connection url to the created database if you have created a database, supply the database username and password, click next and finish
- return back to netbeans Services tab, and refresh the server.
- the datasource will apply under resources > datasource

and that is it.
Re: Learn Java EE With Me by Nobody: 6:30am On Sep 21, 2016
Keep at it bro even though it's spring on my mind, knowing this won't hurt because yday a small Server knowledge helped me out in Spring in which am totally a noob in fact I don't know it

1 Like

Re: Learn Java EE With Me by NaijaTroops(m): 10:47pm On Sep 23, 2016
pls i'm stuck, my server my failing to deploy. i downloaded and installed netbean and wildfly.
Re: Learn Java EE With Me by NaijaTroops(m): 10:48pm On Sep 23, 2016
pls i'm stuck, my server is failing to deploy. i downloaded and installed netbean and wildfly.
Re: Learn Java EE With Me by ugwum007(m): 11:15pm On Sep 23, 2016
NaijaTroops:
pls i'm stuck, my server my failing to deploy. i downloaded and installed netbean and wildfly.

add the wildfly server instance to your netbeans, then right-click and choose start server

go to your browser, and type localhost:9990 or localhost:8050

if you choose to use the later, click on administration console in the homepage

Re: Learn Java EE With Me by ugwum007(m): 11:22pm On Sep 23, 2016
you can also start it up through: C:\wildfly-10\bin\standalone.bat

in your own system context
Re: Learn Java EE With Me by ugwum007(m): 11:33pm On Sep 23, 2016
In this section:

- Create a new database with name postgresdb using Postgresql
- Create a table for database postgresdb
- Integrate the new database to Netbeans
- Create a three columns (id - primary key, name, price): CREATE TABLE beers (id integer PRIMARY KEY, name varchar(255), price varchar(250));

- Use Netbeans create new project wizard to create a new Java EE project (EJB Module) named Inventory-ejb, package name is inv
- Create another project, Java project named Inventory_Client with main class Inventory_Client, package name is client
Re: Learn Java EE With Me by ugwum007(m): 11:38pm On Sep 23, 2016
Inside Inventory-ejb project:

- Create an Entity class: Beer
- Create persistence unit: persistence.xml
- Create datasource file: jboss-ds.xml (create a new folder (setup) under the project folder and put it inside.
- Create the remote interface: InventoryRemote
- Create the persistent bean class: InventoryPersistBean
Re: Learn Java EE With Me by ugwum007(m): 11:57pm On Sep 23, 2016
Entity Class:

-highlight project folder, go to new > entity class from Database
-On the next window, choose datasource from the drop-down menu; in this case, mine is java:/jboss/datasources/postgresdb
-the database table (beers) should apply inside the available tables section, click on it and add it to Selected Tables, click next
-On the next window, choose your Entity class name, make sure your project and package names are correct
-For now for this context, uncheck the three options under and click next
-In the next window, choose Association Fetch: default, Collection Type: java.util.List
-Only check, the third option
-Click finish

Read up rules regarding creating of Entity classes but the profounds are setter and getters methods, each field name must match the name of the column name in the database and atleast one constructor.

ofcourse it must be serialized.



the final entity class after much editing:

@Entity //an entity class
@Table(name = "beers"wink //for the table
@NamedQueries({
@NamedQuery(name = "Beers.findAll", query = "SELECT b FROM Beers b"wink})
public class Beers implements Serializable {

private static final long serialVersionUID = 1L;

private int id; //column id from database

private String beername; //column beername from database

private String price; //column price from database

public Beers() { // constructor
}


@Id //denotes this field as the id column and primary key
//@GeneratedValue(strategy= GenerationType.AUTO) //will explain later
@Column(name="id"wink
public int getId() {
return id;
}

public void setId(int bid) {
this.id = bid;
}

public String getBeername() {
return beername;
}

public void setBeername(String beername) {
this.beername = beername;
}


public String getPrice() {
return price;
}

public void setPrice(String beerPrice) {
this.price = beerPrice;
}
}
Re: Learn Java EE With Me by ugwum007(m): 12:05am On Sep 24, 2016
ugwum007:

//@GeneratedValue(strategy= GenerationType.AUTO) //will explain later

This line of code kept me from updating for almost two days, like I said am still learning.

If you like hibernate to automatically insert the id column (primary key), use the above line either in AUTO or IDENTITY, but if you want to do it manually, remove or comment out the line.
Re: Learn Java EE With Me by ugwum007(m): 12:10am On Sep 24, 2016
InventoryRemote Interface:

@Remote //this is a remote interface
public interface InventoryRemote {

//business methods
void addBeer(Beers IE); //adds beers to db
List<Beers> getAllBeers(); //gets all beers from db


}
Re: Learn Java EE With Me by ugwum007(m): 12:13am On Sep 24, 2016
InventoryPersistBean class:


@Stateless
public class InventoryPersistBean implements InventoryRemote{

public InventoryPersistBean() {
}

@PersistenceContext(unitName="Inventory-ejbPU"wink //our persistence unit
private EntityManager em; //declare entitymanager

@Override
public void addBeer(Beers IE) {

em.persist(IE); //persist or add record to db

}

@Override
public List<Beers> getAllBeers() {
return em.createQuery("From Beers"wink.getResultList(); //gets the beers in the inventory by name
}


}
Re: Learn Java EE With Me by ugwum007(m): 12:20am On Sep 24, 2016
jboss-ds.xml:

<?xml version="1.0" encoding="UTF-8"?>

<datasources>
<local-tx-datasource>
<jndi-name>java:/jboss/datasources/PostgresDS</jndi-name>
<connection-url>jdbc:postgresql://localhost:5432/postgresdb</connection-url>
<driver-class>org.postgresql.driver</driver-class>
<user-name>postgres</user-name>
<password>nkenna007</password>
<min-pool-size>10</min-pool-size>
<max-pool-size>20</max-pool-size>
<idle-timeout-minutes>5</idle-timeout-minutes>
</local-tx-datasource>
</datasources>

These should be self explanatory, it contains credentials to the postgres database/datasource
Re: Learn Java EE With Me by ugwum007(m): 12:27am On Sep 24, 2016
Persistence.xml:

You can either use the GUI to build this file or just do it programatically. choose the source tab and enter this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Inventory-ejbPU" transaction-type="JTA">

<jta-data-source>java:/jboss/datasources/PostgresDS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
<persistence-unit name="Inventory-ejbPU2" transaction-type="JTA">
<jta-data-source>java:/jboss/datasources/PostgresDS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>ALL</shared-cache-mode>
</persistence-unit>
</persistence>


if you want to use the GUI, choose the design Tab, choose data source, persistence unit name, persistence provider (I am using Hibernate).

Note: the changes in the design tab is reflecting on the source tab
Re: Learn Java EE With Me by ugwum007(m): 12:34am On Sep 24, 2016
Now choose clean and build and finally deploy to the wildfly server.

next is the application client
Re: Learn Java EE With Me by ugwum007(m): 12:51am On Sep 24, 2016
Look out on the server log for these JNDI entries:

22:09:19,722 INFO [org.jboss.as.ejb3.deployment] (MSC service thread 1-1) WFLYEJB0473: JNDI bindings for session bean named 'InventoryPersistBean' in deployment unit 'deployment "Inventory-ejb.jar"' are as follows:

java:global/Inventory-ejb/InventoryPersistBean!inv.InventoryRemote
java:app/Inventory-ejb/InventoryPersistBean!inv.InventoryRemote
java:module/InventoryPersistBean!inv.InventoryRemote
java:jboss/exported/Inventory-ejb/InventoryPersistBean!inv.InventoryRemote
java:global/Inventory-ejb/InventoryPersistBean
java:app/Inventory-ejb/InventoryPersistBean
java:module/InventoryPersistBean


add these files/project to your library or as a library:
jboss-client.jar
jpa.jar
hibernate3.jar
postgresql driver (jar file)
Inventory-ejb.jar (add the project)

Some of these files are shipped with wildfly server but for some reasons which I am still looking for, they are missing from the classpath during compliling.


public class Inventory_Client {

BufferedReader brConsoleReader = null; // take input from console
Properties props;
InitialContext ctx;
Context remoteContext;


public static void main(String[] args) {
// TODO code application logic here
Inventory_Client IC = new Inventory_Client();
IC.tasteStatelessEJB();
}

private void tasteStatelessEJB(){
try{
props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"wink;
props.put(Context.PROVIDER_URL, "http-remoting://localhost:8050"wink;
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"wink;
props.put(Context.SECURITY_PRINCIPAL, "steinacoz"wink;
props.put(Context.SECURITY_CREDENTIALS, "nkenna007"wink;
props.put("jboss.naming.client.ejb.context", true);

ctx = new InitialContext(props);

}catch(NamingException ex){
ex.printStackTrace();
}

brConsoleReader = new BufferedReader(new InputStreamReader(System.in));

try{

int choice = 1;
int id = 1; //for manually inserting id

InventoryRemote inventoryBean = (InventoryRemote) ctx.lookup("Inventory-ejb/InventoryPersistBean!inv.InventoryRemote"wink; //JNDI LOOKUP
System.out.println("choose right option"wink;
while(choice != 2){
String beerName;
String beerPrice;


String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);

if(choice == 1){
System.out.println("Enter Beer Name: "wink;
beerName = brConsoleReader.readLine();

System.out.println("Enter Beer Price: "wink;
beerPrice = brConsoleReader.readLine();

Beers beer = new Beers();
beer.setBeername(beerName);
beer.setPrice(beerPrice);
beer.setId(id); //manually sets the id column

inventoryBean.addBeer(beer); //persists beer to the database

id++; //increments id column
}else if(choice == 2){
break;
}
}

List<Beers> beerList = inventoryBean.getAllBeers();
System.out.println("Beers entered so far: " + beerList.size());
int i = 0;
for(Beers ie:beerList){
System.out.println("Beer Name: " + "." + ie.getBeername() + "Price: " + ie.getPrice());
}




}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}finally{
try{
if(brConsoleReader != null){
brConsoleReader.close();
}
}catch(IOException ex){
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}

}


you can now run the client. If all goes well, netbeans console will look like these:

run:
Sep 23, 2016 10:22:30 PM org.xnio.Xnio <clinit>
INFO: XNIO version 3.4.0.Final
Sep 23, 2016 10:22:30 PM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.4.0.Final
Sep 23, 2016 10:22:30 PM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 4.0.21.Final
Sep 23, 2016 10:22:35 PM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
Sep 23, 2016 10:22:36 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@5ce81285, receiver=Remoting connection EJB receiver [connection=Remoting connection <70b9d7b9> on endpoint "config-based-naming-client-endpoint" <78c03f1f>,channel=jboss.ejb,nodename=steinacoz-pc]} on channel Channel ID cfeabde9 (outbound) of Remoting connection 4f970963 to localhost/127.0.0.1:8050 of endpoint "config-based-naming-client-endpoint" <78c03f1f>
Sep 23, 2016 10:22:36 PM org.jboss.ejb.client.EJBClient <clinit>
INFO: JBoss EJB Client version 2.1.4.Final
choose right option
1
Enter Beer Name:
hero
Enter Beer Price:
300
1
Enter Beer Name:
orijin
Enter Beer Price:
450
1
Enter Beer Name:
life
Enter Beer Price:
250
2
Beers entered so far: 3
Beer Name: .heroPrice: 300
Beer Name: .orijinPrice: 450
Beer Name: .lifePrice: 250
BUILD SUCCESSFUL (total time: 48 seconds)

Don't mind my spacing, feeling sleepy.

run a query on the database (SELECT * FROM beerswink to view the records
Re: Learn Java EE With Me by ugwum007(m): 1:06am On Sep 26, 2016
I tried inserting id manually in the id column for each record, it worked at first run but started getting exceptions on the second run. The manual id(s) were being duplicated which lead to errors.

I had to modify the entity and client class so that the Id will be inserted automatically.

[url]NOTE:[/url] Postgresql database doesn't support auto increment like mySQL, you have to create a sequence in the database.

How to create Sequence in postgresql:

- Make sure that the table has an Id column of type smallInt or bigInt.
- open postgressql GUI tool named pgAdmin 111
- expand the schema where the table is, right-click on sequence > Add new Sequence
- add the sequence name, if the table is empty leave the other fields like that
- finally, add the line nextval('your_sequence_name'::regclass) to the Default value in your primary key
use alter table beers alter column id set default nextval('sequence name'::regclass)

(1) (2) (3) (4) (Reply)

Net Salary For A Data Analyst Or Scientist Or Web Dev / Please Review My Personal Website / Where Can I Learn Programming In Ibadan?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 82
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.