JDev

Code, javaJune 29, 2007 4:16 pm

The String.replaceAll(String regex, String replacement) method can be used to escape single quotes in Java.

The correct usage of this method is not quite intuitive. If you construct sql statements in Java, single quotes in a string must be preceded with a backslash(\). However, if you put a single backslash before the quote, the method would still return an unescaped quote.  I tried putting a double backslash with the same result. It turns out that the correct number of backslashes is four, two for the regex engine and two for the java compiler:

    str = str.replaceAll("’","\\\\’");

The solution was found on the JavaRanch forum

 

Code, javaJune 5, 2007 12:49 pm

Add an empty border to JTextArea: 

    myTextArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

javaJune 4, 2007 1:06 pm

Use the setMnemonic(char) method to set a hot key associated with a button in Java Swing, e.g.:

jButton.setMnemonic(’a'); 

Code, php, javaMay 3, 2007 6:00 pm

I always have trouble remembering the correct arguments to the substring() method in order to delete the last character in a String.

Here is how you do it in Java: 

str =  str.substring(0, str.length()-1);

A similar statement in PHP:

$str = substr($str,0, strlen($str)-1); 

 

 More Java tips at http://www.ekcsoft.com/coding/java

Code, java 5:15 pm

Apparently, you cannot continue a long string on multiple lines with a \ separator like you do it in C or JavaScript.

Can’t be done. Gotta use the plus sign or append() to a StringBuffer. So the post title is actually misleading.

 

javaFebruary 15, 2007 5:22 pm

The following is a list of most frequent J2EE interview questions based on real interview experience :

Project development lifecycle
- Senior dev roles and responsibilities
- Using version control systems. Branching souce codes, ets

- OOP concepts
- Inheritance vs composition
- Design patterns. Almost everybody asked about two ways of implementing Singleton.

- Java interfaces vs abstract classes.
- Java collections framework. Differences between Hashtable and Hashmap, differences between ArrayList and LinkedList. Concurrent access and modification of the List.
- Java threads. Multi-treading. Differences between Runnable interface and Thread class.
- SAX vs DOM
- JDBC: Difference between Statement, PreparedStatement.
- Java Sockets
- RMI
- JMS
- EJB: Very high level questions: types of EJB, when to use
- Applicabilty of EJB
- Struts
- Applicability of Struts
- Spring framework and Hibernate.
- Ant

- What is a transaction?
- What is an outher join?
- What is normalization and why it may be helpful to denormalize?
- Using of Group clause in SQL SELECT statements

Visit http://www.ekcsoft.com/coding/java.php for more Java tips.

Code, javaDecember 13, 2006 5:48 pm

Here are some Java books recommended for beginners:

O’Reilly’s Learning Java

Java: Learning to Program with Robots

Code, java 5:38 pm
Calendar cal = GregorianCalendar.getInstance(); 
SimpleDateFormat df = new SimpleDateFormat("MMM yyyy");
Date currentMonth = new Date();
cal.setTime(currentMonth);
cal.add(Calendar.MONTH, 1);
String nextMonth = df.format(cal.getTime());
 http://www.sitepoint.com/forums/showthread.php?t=434129
I posted more examples at http://www.ekcsoft.com/coding/java.php
 
 
 
 
javaOctober 13, 2006 2:21 pm

Go to Windows->Preferences

In the preferences window, expand Java->Editor.

Make sure the Editor is selected.

On the right side panel, under the Appearance tab, make sure the check-box Show line numbers is checked.

Click Ok to save the new setting. 

 

java, strutsSeptember 29, 2006 2:19 pm

    It’s very easy to read request parameters with Struts Tag Library, although it took me a while to find this information:

<bean:parameter id=’project’ name="projectNumber"/> 

This tag reads the request parameter named projectNumber and saves it in a bean whose id is project.

Use a scriptlet to show the value of the request parameter on the page:

Project Number : <%= project %> 

Use the bean:write tag to display the value of session or request  attributes on the page:

<bean:write name="attributeName"/> 

 

jstl, javaAugust 31, 2006 7:50 pm

There is a problem with using JSTL to show text data that was previously saved from a textarea. If you show such text data in a table, the line-breaks from the textarea will be ignored and the text will be hard to read. A possible solution is to use a Java bean to process such data and replace line-breaks with BR tags before showing it on the page. Such a bean may look as follows:

package beans;
public class HTMLBreaker {
   
    private String text;  
    public HTMLBreaker() {               
    } 
   
    private  String insertBreaks(String text){      
        int len = text.length();      
        StringBuffer sb = new StringBuffer(len+16);          
        for(int i=0; i<text.length();i++){       
            if(text.charAt(i)==’\n’)           
                sb.append("<br>");           
            else       
                sb.append(text.charAt(i));   
        }           
        return sb.toString();       
    }   
   
   
    public String getText() {
        return text;
    }

    public void setText(String string) {
         text="";
        if(string != null)
            text = insertBreaks(string);         
    }
}

 

Then, on a JSP page create  the bean instance:

    <jsp:useBean id=’hbreaker’ class=’beans.HTMLBreaker’>

Set the bean property with the value of a database field: 

     <c:set target=’${hbreaker}’ property=’text’ value=’${row.text}’/>

Display the modified value of the bean property on the page:  

   <jsp:getProperty name=’hbreaker’ property=’text’/>          

If you use the JSTL c:out tag:

<c:out value=’${hbreaker.text}’/>

the BR tags surrounding symbols will be replaced with HTML characters, and show up as such on the screen.                                       

Don’t forget to close the bean tag:

  </jsp:useBean> 

A couple of other solutions using the JSTL fn:replace function and the Jakarta String Tag Library are offered at this weblog.

 

javaAugust 22, 2006 12:26 pm

Instance variables in a class are always initialized, even if no constructor exists. The compiler automatically initializes them when it creates an instance. If you have an initial value specified the complier will use that for the initialization. If you don’t have an initial value, it will use defaults as listed below.

  • Numerical values — 0
  • Boolean values — false
  • Object references — null
java 12:24 pm

Here are some navigation shortcuts for Websphere Development Application Studio:

Ctrl+L

Go to line number.

Ctrl+I

Indent the highlighted text.

Ctrl+Q Goto last modified editor position

Ctrl+K

Go to next occurrence of the selected text

Ctrl+Shift+K

Go to previous occurrence of the selected text

Ctrl+F6

Navigate between open Editors (or, files in an editor)

Ctrl+F7

Navigate between open Views

Ctrl+F8

Navigate between open Perspectives

Ctrl+Shift+P Navigating to Matching braces

F12

Jump to the open Editor

Ctrl+Shift+W

List all files open in an editor

Alt+left arrow

Back (last editing position)

Alt+right arrow

Forward (next editing position)