JDBC Statement – Insert a row

A JDBC Statement example to insert a row into the database. RowInsert.java package com.mkyong.jdbc.statement.row; import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.time.LocalDateTime; public class RowInsert { public static void main(String[] args) { // auto close connection and statement try (Connection conn = DriverManager.getConnection( "jdbc:postgresql://127.0.0.1:5432/test", "postgres", "password"); Statement statement = conn.createStatement()) { …

Read more

JDBC Statement – Create a table

A JDBC Statement example to issue a create table SQL to the database. CREATE TABLE EMPLOYEE ( ID serial, NAME varchar(100) NOT NULL, SALARY numeric(15, 2) NOT NULL, CREATED_DATE timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP PRIMARY KEY (ID) ); TableCreate.java package com.mkyong.jdbc.statement.table; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class TableCreate …

Read more

Hibernate Error – Collection has neither generic type or OneToMany.targetEntity()

Problem In Hibernate development, defined one to many relationship via annotation. package com.mkyong.user.model; @Entity @Table(name = "USER", schema = "MKYONG") public class User implements java.io.Serializable { private Set address = new HashSet(0); //… @OneToMany(orphanRemoval=true, cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user") public Set getAddress() { return this.address; } } But it hits following exception : …

Read more

BasicDataSource causing java.util.ConcurrentModificationException in WebSphere

Problem With Spring, declares data source as “org.apache.commons.dbcp.BasicDataSource“. When deployed to WebSphere, everything work perfectly. File : spring-datasource.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:config/database/database.properties</value> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> …

Read more

JAX-WS + Spring integration example

Here’s a guide to show you how to integrate Spring with JAX-WS, as mention in this link : http://jax-ws-commons.java.net/spring/. Upon finishing this tutorial, you will create a simple HelloWorld web service (JAX-WS), and DI a bean into the web service via Spring. 1. Project Folder See the final project folder structure. 2. Project Dependencies Use …

Read more

How to enable Subversion (SVN) in Eclipse IDE

Eclipse IDE has build-in integration with Concurrent Versions System (CVS), but not Subversion (SVN). Here’s a guide to show you how to make Eclipse IDE support Subversion (SVN) via Subclipse plugin. 1. Subclipse Plugin Visit this link : http://subclipse.tigris.org/ , click on the “Download and Install” tab. Get the “Eclipse update site URL” for Subclipse …

Read more

Unable to locate Spring NamespaceHandler for XML schema namespace [http://jax-ws.dev.java.net/spring/servlet]

Problem Integrate Spring with JAX-WS, according to this link: http://jax-ws-commons.java.net/spring/ . When start the web application, get this exception : org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://jax-ws.dev.java.net/spring/servlet] Here’s the Spring + JAX-WS configuration file. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd" > …

Read more

Maven : generics are not supported in -source 1.3

Problem Maven as project build tool. Using Java generic function, when build with Maven, get this error message generics are not supported in -source 1.3 (use -source 5 or higher to enable generics) Solution You need to tell Maven to use JDK 1.5 to compile your source code explicitly. Declare Maven compiler plugin (maven-compiler-plugin) in …

Read more

JAX-WS + Java Web Application Integration Example

Often times, JAX-WS always be part of your Java web application. Here we show you how to integrate JAX-WS into Java web application easily. 1. Project Folder First, review this project folder structure. 2. Web Service A super simple web service. Code is self-explanatory. File : HelloWorld.java package com.mkyong.ws; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public …

Read more

How to increase WebSphere JVM memory

Problem When a J2EE web application is deployed into the WebSphere Application Server 7 (WAS), the WAS started to load very slow and keep hanging for whatever actions selected, end with a “java.lang.OutOfMemoryError” error message in the WebSphere’s log file. Solution The default WebSphere’s Java Virtual Machine memory is not enough; you should adjust more …

Read more

WebSphere 7 & javax/persistence/OneToMany.orphanRemoval() error

Problem In Hibernate development, contains a model class with JPA @OneToMany annotation : @OneToMany( cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user") public Set<Debit> getDebits() { return this.debits; } When web application is deployed on WebSphere 7, it hit following error message : Caused by: java.lang.NoSuchMethodError: javax/persistence/OneToMany.orphanRemoval()Z at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1912) at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:707) … 118 more P.S hibernate-jpa-2.0-api-1.0.0.Final.jar …

Read more

org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity()

Problem See following Hibernate mapping via annotation, which is generated by NetBean’s Hibernate Tool. File : Stock.java package com.mkyong.stock.model; //… @Entity @Table(name = "stock", catalog = "mkyong"); public class Stock implements java.io.Serializable { //… private Set stockCategories = new HashSet(0); @OneToMany(fetch = FetchType.LAZY, mappedBy = "stock") public Set getStockCategories() { return this.stockCategories; } public void …

Read more

How to unzip a WAR file in Java

In J2EE web development, Web Application Archive(WAR) file is just a normal JAR file, which consists all of your web application components like, servlets, Java classes, libraries, resources and etc. Read Wiki for detail. Problem Current web application WAR file is generated via Ant or Maven tool, copy to *nix environment for deployment, but no …

Read more

java.lang.ClassNotFoundException: com.sun.mail.util.MessageRemovedIOException

Problem Use JavaMail API to send Email via GMail smtp server, but hit the following error message : Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MessageRemovedIOException at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) … 1 more P.S The javaee.jar library is included. Solution To solve it, you need to include the …

Read more

Metro on WebSphere 7 – com.ibm.xml.xlxp2.jaxb.JAXBContextImpl incompatible exception

Problem Developed a SOAP web service via Metro 2.0.1 (webservices-rt.jar), integrate with Spring via jaxws-spring-1.8.jar and deployed on WebSphere Application Server (WAS) version 7.0.0.13 . See web service below : File : UserWS.java package com.mkyong.user.ws; //import… @WebService() public class UserWS { private UserBo userBo; @WebMethod(exclude = true) public UserBo getUserBo() { return userBo; } @WebMethod(exclude …

Read more

Spring + JAX-WS : ‘xxx’ is an interface, and JAXB can’t handle interfaces

Problem Integrate Spring + JAX-WS, see web service below : package com.mkyong.user.ws; //imports… @WebService() public class PGUserWS { //DI via Spring private UserBo userBo; public UserBo getUserBo() { return userBo; } public void setUserBo(UserBo userBo) { this.userBo = userBo; } @WebMethod(operationName = "addUser") public boolean addUser(@WebParam(name = "userId") String userId, @WebParam(name = "User") User user) …

Read more

Hibernate Error : JavaReflectionManager cannot be cast to MetadataProviderInjector

Problem Using Hibernate annotation, after upgraded Hibernate version from v3.2.7 to v3.6, it hits following error message : Caused by: java.lang.ClassCastException: org.hibernate.annotations.common.reflection.java.JavaReflectionManager cannot be cast to org.hibernate.annotations.common.reflection.MetadataProviderInjector Here’s the list of the Hibernate annotation libraries : hibernate3-3.6.0.Final.jar hibernate-annotations-3.4.0.GA.jar hibernate-commons-annotations-3.0.0.GA.jar Solution Hibernate annotation module is merged into Hibernate core module since v3.5 (if not mistake). In …

Read more

java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.(Z)V

Problem Ant project, doing Spring + Hibernate (annotation) development, but hits following error message : Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(Z)V Here’s the list of the main jar files, included asm-3.3.1.jar, but still hits the above error message? hibernate-3.2.7.ga.jar hibernate-annotations-3.4.0.GA.jar hibernate-commons-annotations-3.0.0.GA.jar spring-2.5.6.jar asm-3.3.1.jar Solution A classic problem in Ant project, you have to manage dependency library manually, …

Read more

Spring + jax-ws : ‘#xxx’ is not a valid value for ‘NCName’

Problem Here’s the Spring + JAX-WS configuration file … <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd" > <wss:binding url="/ws/OrderWs"> <wss:service> <ws:service bean="#orderWs"/> </wss:service> </wss:binding> <!– this bean implements web service methods –> <bean id="#orderWs" class="com.mkyong.order.ws.OrderWS"> <property name="orderBo" ref="OrderBo" /> </bean> </beans> During server start up, it hits following …

Read more

Create ERD with Visio 2003 + Oracle 11g, reverse engineer failed?

Problem In Office Visio 2003, trying to use reverse engineer, “Oracle server” as Visio driver to create entity-relationship diagram or ERD from Oracle 11G. But, when Visio “Extracting Oracle Object, type and body ?XDB$Complex_derivation….” A “Encountered a fatal error during reverse engineer of information from database” message is prompt and the process is stopped. Solution …

Read more

Connect to Oracle DB via JDBC driver

A JDBC example to show you how to connect to a Oracle database with a JDBC driver. Tested with: Java 8 Oracle database 19c Oracle JDBC driver for Java 8, ojdbc8.jar 1. Download Oracle JDBC Driver Visit Oracle database website and download the Oracle JDBC Driver. 2. JDBC Connection Note Find your Oracle SID in …

Read more

java.lang.ClassNotFoundException: org.apache.xbean.spring.context.v2.XBeanNamespaceHandler

Problem Developing jax-ws with Spring, using jdk1.6 + jaxws-spring-1.8.jar + Spring-2.5.6.jar. See following Spring XML configuration file : <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd" > <wss:binding url="/ws"> <wss:service> <ws:service bean="#UserWs"/> </wss:service> </wss:binding> <!– this bean implements web service methods –> <bean id="UserWs" class="com.mkyong.user.ws.UserWS"> <property name="UserBo" ref="com.mkyong.user.bo.UserBo" /> …

Read more

java.lang.ClassNotFoundException: org.springframework.beans.factory.support.ReaderContext

Problem Integrating jax-ws with Spring, using xbean-spring-2.8.jar + Spring-2.5.6.jar. While server is starting up, it hits java.lang.ClassNotFoundException: org.springframework.beans.factory.support.ReaderContext Solution The “org.springframework.beans.factory.support.ReaderContext” is no longer exist in Spring-2.5.x, it’s only exist in the old Spring version < 2.5.x. The solution is upgrade your xbean-spring to latest version , for example, v3.7. You can get xbean-spring.jar from ...

Read more

java.lang.ClassNotFoundException: org.objectweb.asm.Type

Problem In Hibernate development, it hits… java.lang.ClassNotFoundException: org.objectweb.asm.Type Solution This is caused by the missing of asm.jar, you can get it from asm official website, or get it via Maven <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3.1</version> </dependency>

JAX-WS : SOAP handler in server side

SOAP handler is a SOAP message interceptor, which is able to intercept incoming or outgoing SOAP message and manipulate its values. For example, attach a SOAP handler in client side, which will inject client’s computer MAC address into the SOAP header block for every outgoing SOAP message that is send by the client. In server …

Read more

javax.xml.stream.XMLStreamException : ParseError at [row,col]:[x,xx]

Problem Created a JAX-WS handler to inject a mac address into the client side SOAP request header automatically : File : MacAddressInjectHandler.java public class MacAddressInjectHandler implements SOAPHandler<SOAPMessageContext>{ @Override public boolean handleMessage(SOAPMessageContext context) { //…… //get mac address String mac = getMACAddress(); //add a soap header, name as "mac address" QName qname = new QName("http://ws.mkyong.com/", "mac …

Read more

JAX-WS : wsgen tool example

The wsgen tool is used to parse an existing web service implementation class and generates required files (JAX-WS portable artifacts) for web service deployment. This wsgen tool is available in $JDK/bin folder. Use cases 2 common use cases for wsgen tool : Generates JAX-WS portable artifacts (Java files) for web service deployment. Generates WSDL and …

Read more

JAX-WS : wsimport tool example

The wsimport tool is used to parse an existing Web Services Description Language (WSDL) file and generate required files (JAX-WS portable artifacts) for web service client to access the published web services. This wsimport tool is available in the $JDK/bin folder. Use Case An common use case of this wsimport tool. 1. Server – Published …

Read more

How to get MAC address in Java

Since JDK 1.6, Java developers are able to access network card detail via NetworkInterface class. In this example, we show you how to get the localhost MAC address in Java. App.java – Get MAC Address via NetworkInterface.getByInetAddress() package com.mkyong; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; public class App{ public static void main(String[] args){ …

Read more

How to get Server IP address in Java

In Java, you can use InetAddress.getLocalHost() to get the Ip Address of the current Server running the Java app. package com.mkyong; import java.net.InetAddress; import java.net.UnknownHostException; public class Test { public static void main(String[] args) { InetAddress ip; try { ip = InetAddress.getLocalHost(); System.out.println("Current IP address : " + ip.getHostAddress()); } catch (UnknownHostException e) { e.e.printStackTrace(); …

Read more

How to convert negative number to positive in Java

To convert negative number to positive number (this is called absolute value), uses Math.abs(). This Math.abs() method is work like this “number = (number < 0 ? -number : number);". See a complete example : package com.mkyong; public class app{ public static void main(String[] args) { int total = 1 + 1 + 1 + ...

Read more

Container Authentication with JAX-WS – (Tomcat version)

In this article, we show you how to implement container authentication with JAX-WS, under Tomcat 6.0. In this way, the authentication is declarative rather than programmatic like this – application authentication in JAX-WS. And Tomcat implement the container authentication via security realm. At the end of this article, the deployed web service will authenticate user …

Read more

Application Authentication with JAX-WS

One of the common way to handle authentication in JAX-WS is client provides “username” and “password”, attached it in SOAP request header and send to server, server parse the SOAP document and retrieve the provided “username” and “password” from request header and do validation from database, or whatever method prefer. In this article, we show …

Read more

How to bypass certificate checking in a Java web service client

In Java web service development environment, developer are always generate a test certificate using keytool. While doing client testing, often times, the web service test client will hits following error messages : java.security.cert.CertificateException: No name matching localhost found SunCertPathBuilderException: unable to find valid certification path to requested target Here’s a source code, that i copied …

Read more

SunCertPathBuilderException: unable to find valid certification path to requested target

1. Problem Set up a localhost Tomcat to support SSL and deployed this web service for testing. While connecting to the deployed web service over SSL connection via this URL : https://localhost:8443/HelloWorld/hello?wsdl, it hits Terminal javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target Caused by: sun.security.validator.ValidatorException: PKIX …

Read more