Wednesday, August 30, 2017
Debug mode in WAS
1) Go to http://localhost:9060/ibm/console
2) Servers --> Server Types --> Web Application Servers --> server1
3) Click on server1. Click on the 'Configuration' tab. Under Additional Services --> Click on Debugging Service.
4) Ensure 'Enable service at server startup' ic clicked. The remote debug port would be port number as in 'JVM debug port'.
Saturday, August 26, 2017
DB2 Nitty Gritty Stuff
To change a column name of a table:
ALTER TABLE SCHEMANAME.TABLENAME RENAME COLUMN OLDCOLUMNNAME TO NEWCOLUMNNAME;
To take a DB backup:
db2stop force
db2start
db2 CONNECT TO DATABASENAME USER username USING password
db2 QUIESCE DATABASE IMMEDIATE
FORCE CONNECTIONS
db2 CONNECT RESET
db2 BACKUP DATABASE DATABASENAME COMPRESS
Tip: SQL1035N The operation failed because the specified
database cannot be
connected to in the mode
requested. SQLSTATE=57019
If during database backup, if the above error comes up, check if any application server is accessing database or shut down any application server that can use database.
To restore a database backup:
db2> restore database DBNAME FROM D:\db_dump ON D:\ into DBNAME without prompting
Note: dbadminuserid must have been added in Windows user accounts [Control panel --> User Accounts]
To restore a database backup:
db2> restore database DBNAME FROM D:\db_dump ON D:\ into DBNAME without prompting
D:\db_dump\DBNAME.0.DB2.DBPART000.20160610133049.001
DBNAME in bold is used in the above query
If the above command doesn't work.
1. Open
Command as Administrator
2. Run the
command runas /profile /user:machinename\dbadminuserid cmd
3. Enter
the database password: dbadminpassword
4. Change
to DB2 install location: [C:\Program Files\IBM\SQLLIB\BIN]
5. Run
db2cwadmin.bat
6. Change
directory to db_dump
7. Execute the above restore command.
Note: dbadminuserid must have been added in Windows user accounts [Control panel --> User Accounts]
To find database version:
db2> db2level
To verify if DB2 license is expired or not:
db2> db2licm.exe –l
To renew DB2 license,
Open command prompt and change directory to the file location, where db2 license is stored. [db2ese_u.lic is the license file]
db2> db2licm -a db2ese_u.lic
To drop a database
db2> drop database DATABASENAME
To list all the databases
db2>list database directory
To drop a column from the table
db2> ALTER TABLE TABLENAME DROP COLUMNNAME
If after an alter script, the following error comes up
DB2 SQL
Error: SQLCODE=-668, SQLSTATE=57016, SQLERRMC=7;xxx.xxx,
DRIVER=3.69.24 [SQL State=57016, DB Errorcode=-668]
This error means access to the database is restricted.
Execute the following query to resolve the error:
db2> REORG TABLE TABLENAME
To create a new database
db2> create database NEWDATABASENAME
To disconnect from the current database
db2> disconnect DATABASENAME
To find
the current database connected to the database.
db2> SELECT CURRENT SERVER FROM SYSIBM.SYSDUMMY1
Tuesday, July 11, 2017
When Eclipse doesn't startup...
On clicking on Eclipse.exe, if it doesn't startup, then delete the file "workbench.xmi" under \.metadata\.plugins\org.eclipse.e4.workbench.
Then, try to restart, Eclipse usually starts up.
Monday, April 4, 2016
Date Formats and excetera
Using SimpleDateFormat to convert a date in string into a desired format
SimpleDateFormat sdfYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd"); // The input date string will be in yyyy-MM-dd format
SimpleDateFormat sdfDDMMMYYYY = new SimpleDateFormat("dd-MMM-yyyy"); //The output date string will be in dd-MMM-yyyy format
Date date;
try {
date = sdfYYYYMMDD.parse("2012-12-09"); // Parse the input string into a date object. parse method throws a ParseException
System.out.println(date); // Prints as Sun Dec 09 00:00:00 IST 2012
String formattedDate = sdfDDMMMYYYY.format(date); // Format the date object to a string in the dd-MMM-yyyy (desired format).
System.out.println(formattedDate); // Prints as 09-Dez-2012
} catch (ParseException e) {
e.printStackTrace();
}
If we need to use sorting on dates, say for using Collections.sort(list), we implement Comparable interface and override the compareTo() method.
@Override
public int compareTo(EmpTable o) {
String dateinstr1 = this.getDate();
String dateinstr2 = o.getDate();
try{
SimpleDateFormat sdfyyyymmdd = new SimpleDateFormat("dd-MMM-yyyy");
Date date1 = sdfyyyymmdd.parse(dateinstr1);
Date date2 = sdfyyyymmdd.parse(dateinstr2);
return date1.compareTo(date2);
}catch(ParseException e){
e.printStackTrace();
}
return 0;
}
If dates are as Date objects, the above piece of code can be used for displaying dates in ascending format.
If they need to be in descending format, the highlighted text above needs to be written as
return - date1.compareTo(date2);
i.e., prefix with a minus sign.
SimpleDateFormat sdfYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd"); // The input date string will be in yyyy-MM-dd format
SimpleDateFormat sdfDDMMMYYYY = new SimpleDateFormat("dd-MMM-yyyy"); //The output date string will be in dd-MMM-yyyy format
Date date;
try {
date = sdfYYYYMMDD.parse("2012-12-09"); // Parse the input string into a date object. parse method throws a ParseException
System.out.println(date); // Prints as Sun Dec 09 00:00:00 IST 2012
String formattedDate = sdfDDMMMYYYY.format(date); // Format the date object to a string in the dd-MMM-yyyy (desired format).
System.out.println(formattedDate); // Prints as 09-Dez-2012
} catch (ParseException e) {
e.printStackTrace();
}
If we need to use sorting on dates, say for using Collections.sort(list), we implement Comparable interface and override the compareTo() method.
@Override
public int compareTo(EmpTable o) {
String dateinstr1 = this.getDate();
String dateinstr2 = o.getDate();
try{
SimpleDateFormat sdfyyyymmdd = new SimpleDateFormat("dd-MMM-yyyy");
Date date1 = sdfyyyymmdd.parse(dateinstr1);
Date date2 = sdfyyyymmdd.parse(dateinstr2);
return date1.compareTo(date2);
}catch(ParseException e){
e.printStackTrace();
}
return 0;
}
If dates are as Date objects, the above piece of code can be used for displaying dates in ascending format.
If they need to be in descending format, the highlighted text above needs to be written as
return - date1.compareTo(date2);
i.e., prefix with a minus sign.
Thursday, March 24, 2016
Design Pattern - 7 Adapter and Facade Pattern
Giving a slip to Design pattern 6, the command pattern
Adapter pattern - Converts the interface of a class into another interface clients expect. Lets classes work together that couldn't otherwise because of incompatible interfaces.
Facade pattern - Provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher level interface that makes the subsystem easier to use.
Adapter pattern - Typical example is using a adapter class to adapt a Enumeration class of Java to that of Iterator class.
Adapter pattern has three interfaces -
- The Target interface
- The Adapter interface
- The Adaptee interface
Example: Lets have a Duck interface
interface Duck{
void quack();
void fly();
}
interface Turkey {
void gobble();
void shortFly();
}
class DuckAdapter implements Duck{
Turkey turkey;
DuckAdapter(Turkey turkey){
this.turkey = turkey;
}
void quack(){
turkey.gobble();
}
void fly(){
turkey.shortFly();
}
}
In the above example, the DuckAdapter adapts to the Turkey(Adaptee). The client is not aware that the turkey's methods are getting executed.
Adapter Patterns are of two forms - Class Adapters and Object Adapter. Class Adapter require multiple inheritance.
Facade Pattern - Provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. A facade decouples a client from a complex subsystem.
For example, taking the example of home theatre system. It comprises of many subsystems such as a projector system, sound system, etcetra. If we were to do a simple job of watching a movie using the dvd player, and we take the subsystems into account, we end up creating multiple classes in a single class. Implementing Facade requires we compose the facade with its subsystem and use delegation to perform the work of the facade.
Use an Adapter, when you need to use an existing class and its interface is not the one you need.
Use a facade, when you need to simplify and unify a large interface or complex set of interfaces.
Adapter pattern - Converts the interface of a class into another interface clients expect. Lets classes work together that couldn't otherwise because of incompatible interfaces.
Facade pattern - Provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher level interface that makes the subsystem easier to use.
Adapter pattern - Typical example is using a adapter class to adapt a Enumeration class of Java to that of Iterator class.
Adapter pattern has three interfaces -
- The Target interface
- The Adapter interface
- The Adaptee interface
Example: Lets have a Duck interface
interface Duck{
void quack();
void fly();
}
interface Turkey {
void gobble();
void shortFly();
}
class DuckAdapter implements Duck{
Turkey turkey;
DuckAdapter(Turkey turkey){
this.turkey = turkey;
}
void quack(){
turkey.gobble();
}
void fly(){
turkey.shortFly();
}
}
In the above example, the DuckAdapter adapts to the Turkey(Adaptee). The client is not aware that the turkey's methods are getting executed.
Adapter Patterns are of two forms - Class Adapters and Object Adapter. Class Adapter require multiple inheritance.
Facade Pattern - Provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. A facade decouples a client from a complex subsystem.
For example, taking the example of home theatre system. It comprises of many subsystems such as a projector system, sound system, etcetra. If we were to do a simple job of watching a movie using the dvd player, and we take the subsystems into account, we end up creating multiple classes in a single class. Implementing Facade requires we compose the facade with its subsystem and use delegation to perform the work of the facade.
Use an Adapter, when you need to use an existing class and its interface is not the one you need.
Use a facade, when you need to simplify and unify a large interface or complex set of interfaces.
Tuesday, March 22, 2016
How to create a JPA project in RAD
The tutorials at the below link had excellent tutorial to create a JPA project in RAD. Useful to create in Eclipse as well.
http://www.ibm.com/support/knowledgecenter/SSRTLW_8.0.4/com.ibm.rad.samptut.doc/tutorials/javaee/topics/jpa_create_abst.html
http://www.ibm.com/support/knowledgecenter/SSRTLW_8.0.4/com.ibm.rad.samptut.doc/tutorials/javaee/topics/jpa_create_abst.html
Friday, March 18, 2016
Design Pattern - 5 Singleton Pattern.
One of the simplest patterns - Singleton patterns - Ensures a class has only one instance and provides gloabl access to it.
Steps:
Method 1:
1) Create a private constructor.
2) Create a static class variable.
3) Create a public method to return an object of the class.
Class A{
private static A singleinstance;
private A() {}
public static A getInstance() {
if(singleinstance==null) { //lazy initialization
singleinstance = new A();
}
return singleinstance;
}
}
Method 2:
Class A{
private static A singleinstance= new A();
private A() {}
public static A getInstance() { // Eager initialization
return singleinstance;
}
}
Method 3: To avoid synchronization issues while multithreading,
Class A{
private static A singleinstance= new A();
private A() {}
public static synchronized A getInstance() { // Ensures only one instance is created while multi threading
if(singleinstance==null) {
singleinstance = new A();
}
return singleinstance;
}
}
Method 4: Double checked locking -
Using synchronized keyword over a method is a performance overhead. So,
Class A{
private static A singleinstance;
private A() {}
public static A getInstance() { // Ensures only one instance is created while multi threading
if(singleinstance==null) {
synchronized(this){
if(singleinstance==null){
singleinstance = new A();
}
}
return singleinstance;
}
}
Only for the first time, only then an entry to synchronized block is made.
So, in stead of Method 3, the above approach can be followed in case of multiple threads are used. This way, performance overhead issue can be resolved.
Design Principles so far:
1) Encapsulate what varies
2) Favor composition over inheritance.
3) Program to interfaces, not implementations.
4) Strive for loosely coupled designs between objects that interact.
5) Classes should be open for extension, but closed for modification.
6) Depend on abstractions. Do not depend on concrete classes.
Used: Logging, Preferences and Registry settings, Thread pools, caches, dialog boxes.
Subscribe to:
Posts (Atom)