iEntry 10th Anniversary Java Faqs RSS Feed

Archive for the ‘Java Programming Tutorials’ Category

New Language Running on the JVM

Friday, September 18th, 2009

Google and several other companies have intorduced a new language experiment called Noop that runs on the JVM. It tried to include all the best practices in the programming world while discouraging the the worst ones. On their Google Code site the following reasons to use Noop over other languages.

Dependency Injection changed the way we write software. Spring overtook EJB’s in thoughtful enterprises, and Guice and PicoContainer are an important part of many well-written applications today.

Automated testing, especially Unit Testing, is also a crucial part of building reliable software that you can feel confident about supporting and changing over its lifetime. Any decent software shop should be writing some tests, the best ones are test-driven and have good code coverage.

Noop is a new language that will run on the Java Virtual Machine, and in source form will look similar to Java. The goal is to build dependency injection and testability into the language from the beginning, rather than rely on third-party libraries as other languages do.

Immutability and minimal variable scope are encouraged by making final/const behavior the default and providing easy access to a functional style. Testability is encouraged by providing Dependency Injection at the language level and a compact constructor injection syntax.

Their Google Code site also has these suggestions for the use of their Noop source files:

1. Java translator: produces Java source. Allows you to use Noop without converting your codebase, but not all runtime features of the language are provided.

2. Interpreter: reads and evaluates the Noop code through an interpreter. Slower, but will have a command-line interface

3. Compiled: to Java bytecode.

Noop is part of the Apache License 2.0 code Licencse. Their getting started page is very informatiove and tells you how to get Noop installed and configured easily. Noop has an extensive features list. I can not see this replacing Java, but I do see it being the main development source for the android in the future.

JavaFX

Monday, March 23rd, 2009

Sun has released a new video that shows some of the new features of JavaFX. They are demonstrated by Jacob Lehrbaum, Senior Product Line Manager for JavaFX Marketing.

JavaFX is a platform for creating and delivering Rich Internet Applications (RIAs) — web applications that use rich media types such as video, audio, graphics, and rich text — that can run in a wide variety of devices, anywhere from handsets-to-laptops-to desktops. In many cases, you can compile and run the same JavaFX application code in a mobile environment, a desktop environment, or in a browser.

Java Strings Tutorial

Tuesday, September 9th, 2008

WEB Application Tutorial

Tuesday, September 9th, 2008

Preface

I decided to write this tutorial because I didn’t find a simple tutorial that demonstrates how one could use JSP and Servlets to connect to a DBMS and store/retrieve data, which is one of the most common things a dynamic web site author would do. Such a task is very simple in PHP or ASP, and it is easy in JSP too, but most books written about JSP & servlets simply confuse the reader with the details of the language instead of giving useful use case examples.

In this tutorial I assume that you are familiar with the Java programming language and JDBC and that you have some knowledge of Relational Databases. For the example in this tutorial I ‘ll be using MySQL RDBMS, and the Jakarta Tomcat Servlet container which is also a Web Server. I ‘ll describe how to install each one of them in Windows and Linux operating systems, what to look after, and how to use them. You can also download the source code of the article and try it for yourself.

Installation

Jakarta Tomcat is the most famous and widely used servlet container or application server. It can also play the role of a standalone web server, or it can work in cooperation with other web servers such as Apache, IIS or Netscape. Above all, it’s free!

It can be downloaded from Jakarta’s web site http://jakarta.apache.org/tomcat. The most recent version, as of this writing, is 5.0.

Installation is similar for both Windows and Linux; once you download the tomcat5.zip or .exe file from the jakarta’s web site, simply unzip it to a folder (e.g. C:\Program Files\WebServers\Tomcat5.0 in Windows and \usr\local\Tomcat5.0 in Linux) or follow the instructions of the installation wizard.
Tomcat accepts requests in port 8080. If this port is not used by another application, then you can easily start Tomcat by executing [Tomcat installation]\bin\startup.bat or [Tomcat installation]/bin/startup.sh. If you see no errors while starting this script, then you can verify that Tomcat is running by opening your favorite web browser and typing the URL: http://localhost:8080. Tomcat’s starting web page should be loaded.

If for some reason the service fails to start, you can try to change the listening port. Open file [Tomcat installation]\conf\server.xml using your favorite text and/or xml editor, and search for the string “8080″. Change it to another value, e.g. “8082″. Save and exit, then try to run Tomcat again as we described previously.

To terminate the service simply execute [Tomcat installation]\bin\shutdown.bat or [Tomcat installation]/bin/shutdown.sh.

MySQL is one of the most famous RDBMSs and it is widely used for web applications. It can be downloaded from http://www.mysql.com. Even though version 5.0 is out, as of this writing, it is not stable yet, hence we ‘ll be using version 4.
Download the .zip file and unzip it to a temp folder. Execute Setup.exe in windows or follow the instructions on how to install in Linux (usually there is an install.sh in Linux; however please note that all modern Linux distributions come with mysql already installed; hence check if mysql service is already running or installed before trying to install another version of mysql). Follow the installation instructions. Download the JDBC/ODBC bridge .jar file too.

Once installation completes, execute winmysqladmin.exe in Windows, or winmysqladmin.sh in Linux. The first time you execute this service, it asks for a user name and password to act as administrator. After you provide them, the service normally starts. In Windows, however, it’s good practice to create a shortut and locate it under Start | Programs | Startup and reboot after that. This way, MySQL admin service will run every time you start Windows.
If everything has gone smoothly, then next time you reboot Windows you ‘ll see a new service running on the services on the lower right part of your screen, i.e. a traffic light with the green light active (if the red light is active then something has been wrong with the service and you should check with mysql troubleshooting to find out what’s wrong). A final word, please note that MySQL uses default port 3306.

We are almost ready to start. But before, we must notify Tomcat that we ‘ll be using MySQL. To do that, we must also download MySQL J/Connector from http://www.mysql.com and copy the downloaded jar (should be something like mysql-connector-java-3.x.x-stable-bin.jar) to [Tomcat installation]\shared\lib folder. This way, Tomcat will be able to find everything it needs to connect to MySQL database.

Example

I won’t go into details on how to use MySQL or how to create databases in general, but only what is required by the example we ‘ll be developing together. We ‘ll create a Web PhoneBook application, where a user will be able to complete a form with phone book details (Lastname, Firstname, phone number), and then click on a button to save the values. The web application will store these values to a table Phones_table inside MySQL and will return a JSP page that contains all the records in that table. Let’s begin.
Create a new database. (You can do that either from command line or by using one of the GUI interfaces to MySQL that exist, like Mascon from Scibit, or MyCC from MySQL, or MySQL Navigator. Otherwise, open a DOS (in Windows) or Command (in Linux) prompt and start mysql by issuing the command mysql (or mysql -u user); note that mysql must be in your path to be able to execute this command; if it is not, then go inside [MySQL installation]\bin and run the command mysql). Up to MySQL version 4, you can create a database without issuing a username or password if you prefix the database name with the word ‘test’ (this has changed from version 4.1 and on however). Go on and create the database test_phonebook_db.

 mysql> create database test_phonebook_db

Next, create a new table, phones_table, which contains only three fields: lastname, firstname, phone, the first two of type string and the last one of type integer (I know, phone should also be of type string, but leave it integer for this example in order to demonstrate how you can store a datatype other than string). You could also create another field id to behave as the key, but we ‘ll not complicate things for this simple tutorial. Hence, leave your table without any key.

mysql> create table phones_table
(
 lastname varchar(45) not null,
 firstname varchar(35) not null,
 phone integer

);
mysql> select * from phones_table;
+-----------+-----------+-------+
| lastname | firstname | phone |
+-----------+-----------+-------+
| | | |
+-----------+-----------+-------+
0 rows in set (0.00 sec)

Java Server Pages

Well, I think that the best way to describe JSP elements and how they can be used to access the data from the database, is to see how we can do this using a Java application first. Below, we list a Java application that does the job we want, i.e. it uses JDBC to retrieve all the entries from the phones_table and display them to a JTextArea.

/* PhoneBook.java -- Displays all phonebook entries from a MySQL database table.
 * @ John Kostaras 2/10/2004
 */
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class PhoneBook extends JFrame { 
 // Declarations
 private static final String dbURL = "jdbc:mysql://localhost:3306/test_phonebook_db";
 private static final String driver = "com.mysql.jdbc.Driver";
 private Connection conn = null;
 private Statement stat = null;
 private ResultSet rs = null;
 private Object rsValue = null;
 private int counter;
 private String output = new String();

  public PhoneBook() {
 this.getContentPane().setLayout(new BorderLayout());
	JTextArea txtaResults = new JTextArea();
	this.getContentPane().add(txtaResults, BorderLayout.CENTER);
	txtaResults.setText(getDBResults());
 addWindowListener(new WindowAdapter() {
 public void windowClosing(WindowEvent e) {
 System.exit(0);
 }
 });
 }

 private static String convertUTF8ToAnsi(String someString)
 throws java.io.UnsupportedEncodingException {
 byte b[] = getBytesFromString(someString);
 return new String(b, "UTF-8");
 }

 private static byte[] getBytesFromString(String someString) {
 byte b[] = new byte[someString.length()];
 for (int f = 0; f < someString.length(); f++)
 b[f] = (byte) (someString.charAt(f));
 return b;
 } 

 private String getDBResults() {
 try {
 //1. initialise the JDBC-ODBC bridge providing MySQL's driver as a parameter
Class.forName(driver).newInstance();
//2. initialise the connection to the database,
// providing the URL, username and password (if any)
conn = DriverManager.getConnection(dbURL,"", "");
//3 create an SQL statement and execute it
stat = conn.createStatement();
rs = stat.executeQuery("SELECT * FROM phones_table");
} catch (Exception e) {
System.out.print("Unable do make connection to phonebook");
System.out.print(e);
}
//4. process the results (stored in an instance of a ResultSet)
counter = 0;
try {
	while (rs.next()) {
	output += "Entry no. " + (++counter) ;
	output += "\nLastName: "+(((rsValue =
 rs.getObject("lastname"))==null || rs.wasNull()) ?
	"" : convertUTF8ToAnsi(rsValue.toString()));
	output += "\nFirstName: "+(((rsValue =
	rs.getObject("firstname"))==null || rs.wasNull()) ?
	"" : convertUTF8ToAnsi(rsValue.toString()));
	output += "\nPhone Number: "+(((rsValue =
	rs.getObject("phone"))==null || rs.wasNull()) ?
	"" : convertUTF8ToAnsi(rsValue.toString()));
	output += "\n\n";
	}
	//5. release any resources
	rs.close();
	stat.close();
	conn.close();
 } catch (SQLException sqle) {
	sqle.printStackTrace();
 } catch (UnsupportedEncodingException uee) {
	uee.printStackTrace();
 }
 return output;
 }

 public static void main(String[] args) {
	Frame f = new TestDBObject();
	f.setSize(450, 260);
	f.show();  
 }
}

The code is rather straightforward for those who have worked with JDBC before. The method of interest is getDBResults.The (standard for any database connection) steps are like so:

initialise the JDBC-ODBC bridge providing MySQL’s driver as a parameter

initialise the connection to the database, providing the URL, username and password (if any)

create an SQL statement and execute it

process the results (stored in an instance of a ResultSet)

release any resources

The two methods convertUTF8ToAnsi() and getBytesFromString() allow you to retrieve the data that are stored in the fields of a MySQL table in UTF-8 format. I believe that you ‘ll find these methods very useful in your own projects, especially if you deal with internationalized projects (i.e. with languages other than English). Now let’s see how the above listing is translated to JSP.

/* PhoneBook.jsp -- Displays all phonebook entries from a MySQL database table.
 * @ John Kostaras 2/10/2004
 */
<%@page language="java"
	import="java.sql.*" %>
<!-- Declarations -->
<%!
	final static String dbURL = "jdbc:mysql://localhost:3306/test";
 final static String driver = "com.mysql.jdbc.Driver";
	Connection conn = null;
	Statement stat = null;
	ResultSet rs = null;
	Object rsValue = null;
	int counter;
%>  
<%!
 private static String convertUTF8ToAnsi(String someString)
 throws java.io.UnsupportedEncodingException {
 byte b[] = getBytesFromString(someString);
 return new String(b, "UTF-8");
 }

 private static byte[] getBytesFromString(String someString) {
 byte b[] = new byte[someString.length()];
 for (int f = 0; f < someString.length(); f++)
 b[f] = (byte) (someString.charAt(f));
 return b;
 }
%>
<!-- Scriplets -->
<%
	try {
	Class.forName(driver).newInstance();
	conn = DriverManager.getConnection(dbURL,"", "");
	stat = conn.createStatement();
	rs = stat.executeQuery("SELECT * FROM phones");
	} catch (Exception e) {
	out.print("Unable do make connection to guestbook");
	out.print(e);
	}
%>
<html>
 <head>
 <title>PhoneBook Entries</title>
 </head>
 <body>
 <body bgcolor="#FFFFFF" text="#000000">
 <h3>Phonebook Entries <br>
 </h3>
 <%@ page contentType="text/html; charset=utf-8" %>
 <br>
 <%
 counter = 0;
 while (rs.next()) { %>
 <h4>Entry no. <%= ++counter %> </h4>
 <table width="75%" border="1">
 <tr>
 <td width="21%"><b>LastName:</b></td>
 <td width="79%"><%=(((rsValue = rs.getObject("LASTNAME"))==null || rs.wasNull()) ?
 "" : convertUTF8ToAnsi(rsValue.toString()))%>
 </td>
 </tr>
 <tr>
 <td width="21%"><b>FirstName:</b></td>
 <td width="79%"><%=(((rsValue = rs.getObject("FIRSTNAME"))==null || rs.wasNull()) ?
 "" : convertUTF8ToAnsi(rsValue.toString()))%>
 </td>
 </tr>
<td width="21%"><b>Phone:</b></td>
 <td width="79%"><%=(((rsValue = rs.getObject("PHONE"))==null || rs.wasNull()) ?
 "" : convertUTF8ToAnsi(rsValue.toString()))%>
</td>
 </tr>
 </table>
 <br>
 <br>
 <% } %>
</body>
 </html>
 <%
 rs.close();
 stat.close();
 conn.close();
 %>
  

You can notice the similarities quite easily. As in the .java file, you start by declaring any import statements that you ‘ll need. In a similar manner, you declare a web page as .jsp by adding the following lines on top, using the directive <%@ %>:

<%@page language="java"
 import="java.sql.*" %>

JSP directives provide global information about a JSP page. The syntax of a JSP directive is as follows:
<%@ directive {attribute=”value”} %>
Three possible directives are currently defined by the JSP specification: page, include, and taglib.
You can also meet this tag in the declaration of the following statement where we tell the server that the resulting web page should be of type html with character set UTF-8. Don’t forget to set this if you want to see your web page correctly.

<%@page contentType="text/html; charset=utf-8" %>

JSP code is defined by the <% %> tag. As we ‘ll see below, this is called scriplet tag.

Then, there is the declaration part, where you declare your attributes of the class. You normally declare attributes (and/or methods that are used by the jsp or the resulting servlet) using the declaration tag: <%! %>.Then follows the main part of the java class. You can write normal Java code in a JSP file by including it inside the scriplet tag: <% %> and mix it up with HTML tags. Finally, you can use a third type of tag, the expression tag <%= %> to return the value of a java method or statement in the position specified by the tag. Note, that you normally can do the same things with both the scriplet and expression tags, hence you can see them to be used interchangeably in JSP pages.
See! It was not difficult after all; rather straightforward I ‘d say for someone who knows java and JDBC. S/he can the easily convert his/her code to JSP.

Servlets

Servlets contain the code that is executed in the server side. They normally contain all the business logic of the web application. Servlets have a three-part life cycle: initialisation – via the init() method, service – via the service() method and destruction – via the destroy() method. A servlet that handles HTTP requests extends from javax.servlet.http.HttpServlet.
The code for the servlet is shown in the following listing.

/* PhoneBookServlet.java -- Displays all phonebook entries from a MySQL database table.
 * @ John Kostaras 15/10/2004
 */ 
package phonebook;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class PhoneBookServlet extends HttpServlet {
 final String dbURL = "jdbc:mysql://localhost:3306/test";
 final String driver = "com.mysql.jdbc.Driver";
 private Connection conn = null;
 private Statement stat = null;
 private ResultSet rs = null;
 private int counter;
 private Object rsValue;
 private static String convertUTF8ToAnsi(String someString)
 throws java.io.UnsupportedEncodingException {
 byte b[] = getBytesFromString(someString);
 return new String(b, "UTF-8");
 }

 private static byte[] getBytesFromString(String someString) {
 byte b[] = new byte[someString.length()];
 for (int f = 0; f < someString.length(); f++)
 b[f] = (byte) (someString.charAt(f));
 return b;
 }
 public void init() {
 try {
 Class.forName(driver).newInstance();
 conn = DriverManager.getConnection(dbURL,"", "");
 stat = conn.createStatement();
 } catch (Exception e) {
 System.err.print("Unable to make connection ");
 System.err.print(e);
 }
 }
 public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 try {
 rs = stat.executeQuery("SELECT * FROM phones");
 } catch (SQLException sqle) {
 sqle.printStackTrace();
 }
 response.setContentType("text/html; charset=utf-8");
 PrintWriter out = response.getWriter();
 String docType =
 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
 "Transitional//EN\">\n";
 out.println(docType +
 "<HTML>\n" +
 "<HEAD><TITLE>PhoneBook entries</TITLE></HEAD>\n");
 counter = 0;
 try {
 while (rs.next()) {
 out.println("<h4>Entry no. " + (++counter) + "</h4>");
 out.println("<table width=\"75%\" border=\"1\">");
 out.println("<tr>\n"+
 "<td width=\21%\"><b>LastName:</b></td>\n"+
 "<td width=\"79%\">");
 out.println((((rsValue = rs.getObject("LASTNAME"))==null || rs.wasNull())?
 "":convertUTF8ToAnsi(rsValue.toString())));
 out.println("</td>\n</tr>\n" +
 "<tr>\n"+
 "<td width=\21%\"><b>FirstName:</b></td>\n"+
 "<td width=\"79%\">");
 out.println((((rsValue = rs.getObject("FIRSTNAME"))==null || rs.wasNull())?
 "":convertUTF8ToAnsi(rsValue.toString())));
 out.println("</td>\n"+
 "</tr>\n"+
 "<td width=\21%\"><b>Phone:</b></td>\n"+
 "<td width=\"79%\">");
 out.println((((rsValue = rs.getObject("PHONE"))==null || rs.wasNull())?
 "":convertUTF8ToAnsi(rsValue.toString())));
 out.println("</td>\n" +
 "</tr>\n" +
 "</table>\n"+
 "<br>\n<br>");
 }
 } catch (SQLException sqle) {
 sqle.printStackTrace();
 }
 out.println("</body>\n"+
 "</html>");
 }
 public void destroy() {
 try {
 rs.close();
 stat.close();
 conn.close();
 } catch (SQLException sqle) {
 sqle.printStackTrace();
 }
 }
}

Pay attention on the init() and destroy() methods which are used to allocate and release resources (in our case, database resources). The most important method is doGet(), which accepts an HttpServletRequest and an HttpServletResponse as parameters. These two classes simply represent a request by the client and the response by the server (the result of the servlet) to this request. In other words, the request is what comes as input to the servlet from the client (e.g. form data) and the response is what the servlet returns back to the client.

This method gets the Writer from the response (i.e. the resulting page where it will print the result) in the variable out, and constructs the resulting html page using out.println() statements. You can see how error prone it is to produce an html page from a servlet, hence the usefulness of JSPs.

Deployment

By itself a Servlet is not a full Java application anymore. Since Servlet specification 2.3, servlets are part of a web application module that a container manages, most often packaged as a web archive or .war file. Using a Servlet to generate dynamic respones involves both creating the Servlet and deploying it for use in the Web Application. Each web application has its own configuration files and resources. The most important configuration file is called the deployment descriptor and it must be named web.xml. To deploy your servlet as part of a web application you have to do the following:
Create a new directory called e.g. phonebook inside Tomcat’s webapps directory. Inside it create a new directory called WEB-INF. Create two more folders called classes and lib inside WEB-INF. classes contains all the servlets you create for your web app. It is good practice to put your servlet classes inside packages. We have declared package phonebook for our servlet (see previous listing). So create a new folder phonebook inside classes and put your PhoneBook.class[*] servlet inside it. lib contains any jar files that your web application depends upon. If you haven’t copied the MySQL Connector/J jar file inside /shared/lib, then you have to copy it inside the WEB-INF/lib folder. Note that whatever files you put inside WEB-INF are not visible by any HTTP client. Finally, copy PhoneBook.jsp inside /phonebook folder (not the one inside WEB-INF/classes) in order to be visible by the clients.
We are not done yet. You need to create the deployment descriptor web.xml file and save it inside WEB-INF.Add the following code inside it:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <servlet>
 <servlet-name>PhoneBook</servlet-name>
 <servlet-class>phonebook.PhoneBookServlet</servlet-class>
 </servlet>

 <servlet-mapping>
 <servlet-name>PhoneBook</servlet-name>
 <url-pattern>/PhoneBookServlet</url-pattern>
 </servlet-mapping>
</web-app>

The main tag <web-app> denotes that this is the deployment descriptor for a web application. You define the web app’s servlets inside such a deployment descriptor. For each servlet, you need to add two tags, <servlet> and <servlet-mapping>. The first tag (<servlet>) links the servlet class to a name (you can choose anything you like for the value of the <servlet-name> tag). The second tag (<servlet-mapping>) connects the previously defined name with a URL.
You are ready now. Restart your Tomcat, open a web browser and try what we ‘ve done so far. Type:

http://localhost:8080/phonebook/phonebook.jsp to test your JSP and then
http://localhost:8080/phonebook/PhoneBookServlet to test your servlet.

If this doesn’t work, then you need to do one more thing; you need to inform the Tomcat container that a new Web application exists. Usually, if you add a new web application (like phonebook we just described) under /webapps, Tomcat 5 automatically recognizes it as a new web app and there is nothing more to do. However, other times it does not, hence you need to do one more thing (especially if your web app has other folders inside it than the WEB-INF folder; e.g. /phonebook/entries). You can inform the Tomcat container using either of the following two methods:

1. Add the following line under <host> tag in /conf/server.xml file:

<Context path=”/phonebook” docBase=”phonebook” debug=”0″ reloadable=”true” />

2. Create a new phonebook.xml file, that contains the above line, under /webapps (see [4] p.21).

Restart Tomcat and try again.
You can download the source code of the article, save it into your Tomcat’s installation webapps folder, restart Tomcat and try it for yourself. Good luck with your web applications!

Java Numbers Tutorial

Tuesday, September 9th, 2008

Checking Whether a String Is a Valid Number

//----------------------------------------------------------------------------------
String s;
NumberFormat nf = NumberFormat.getInstance(locale);

Number nb;
try {
    nb = nf.parse(s);
} catch (ParseException ex) {

    // not a number
}
//----------------------------------------------------------------------------------
String s1;
try {
    int i = Integer.parseInt(s1);

} catch (NumberFormatException ex) {
    System.out.println(s1 + " is not an integer");

}

String s2;
try {
    float f = Float.parseFloat(s);

} catch (NumberFormatException ex) {
    System.out.println(s2 + " is not a float");

}
//----------------------------------------------------------------------------------
NumberFormat nf = NumberFormat.getInstance(locale);

boolean isNumeric(String string) {

    try {
       nf.parse(string);
       return true;

   } catch (ParseException ex) {
       return false;

   }
}
//----------------------------------------------------------------------------------

Comparing Floating-Point Numbers

//----------------------------------------------------------------------------------
/**
 * Returns true if given values are equal to accuracy number of decimal places
 */
boolean equal(double num1, double num2, int accuracy) {

    BigDecimal bigNum1 = new BigDecimal(num1).setScale(accuracy, BigDecimal.ROUND_DOWN);

    BigDecimal bigNum2 = new BigDecimal(num2).setScale(accuracy, BigDecimal.ROUND_DOWN);

    return (bigNum1.compareTo(bigNum2) == 0);
  }
//----------------------------------------------------------------------------------
// with a scaling factor

int wage = 536;       // $5.36/hour
int week = 40 * wage; // $214.40

System.out.println("One week's wage is: $" + (double) week/100);
//=> One week's wage is: $214.40
//----------------------------------------------------------------------------------
// with BigDecimal
BigDecimal wage = new BigDecimal("5.36"); // $5.36/hour

BigDecimal hours = new BigDecimal("40");  // 40 hours/week
BigDecimal week = wage.multiply(hours);   // $214.40

System.out.println("One week's wage is: $" + week);
//=> One week's wage is: $214.40
//----------------------------------------------------------------------------------

Rounding Floating-Point Numbers

//----------------------------------------------------------------------------------
BigDecimal rounded = unrounded.setScale(scale, roundingMode);

//----------------------------------------------------------------------------------
BigDecimal a = new BigDecimal("0.255");
BigDecimal b = a.setScale(2, BigDecimal.ROUND_HALF_UP);

System.out.println("Unrounded: " + a);
System.out.println("Rounded: " + b);
//=> Unrounded: 0.255
//=> Rounded: 0.26
//----------------------------------------------------------------------------------
// caution, Math.rint() rounds to the nearest integer !

double[] a = new double[] {3.3 , 3.5 , 3.7, -3.3};

System.out.println("number\tint\tfloor\tceil");
for (int i=0; i<a.length; i++)

    System.out.println(Math.rint(a[i]) + "\t" + Math.floor(a[i]) + "\t" + Math.ceil(a[i]));

//=> number  int     floor   ceil
//=>
//=>  3.3     3.0     3.0     4.0
//=>
//=>  3.5     3.0     3.0     4.0
//=>
//=>  3.7     3.0     3.0     4.0
//=> 

//=> -3.3    -3.0    -4.0    -3.0
//----------------------------------------------------------------------------------

Converting Between Binary and Decimal

//----------------------------------------------------------------------------------
int i = Integer.parseInt(s, 2);
//----------------------------------------------------------------------------------
string s = Integer.toString(i, 2);

//----------------------------------------------------------------------------------
int i = Integer.parseInt("0110110", 2); // i = 54
String s = Integer.toString(54, 2);     // s = 110110
//----------------------------------------------------------------------------------

Operating on a Series of Integers

//----------------------------------------------------------------------------------
for (int i=X, i<=Y, i++) {

    i is set to every integer from X to Y, inclusive
}

for (int i=X, i<=Y, i+=7) {

    i is set to every integer from X to Y, stepsize = 7
}
//----------------------------------------------------------------------------------
System.out.print("Childhood is: ");
for (int i=5; i<=12; i++) {

    System.out.print(i + " ");
}
System.out.println();

//=> Childhood is: 5 6 7 8 9 10 11 12
//----------------------------------------------------------------------------------

Working with Roman Numerals

//----------------------------------------------------------------------------------
// no roman module available
//----------------------------------------------------------------------------------

Generating Random Numbers

//----------------------------------------------------------------------------------
Random random = new Random();
int i = random.nextInt(Y-X+1) + X;

//----------------------------------------------------------------------------------
int i = random.nextInt(51) + 25;
System.out.println(i);

//----------------------------------------------------------------------------------
String elt = array[random.nextInt(array.length)];
//----------------------------------------------------------------------------------
StringBuffer buffer = new StringBuffer();

for (int i=0; i<8; i++)

    buffer.append((char) (random.nextInt(43) + 48));
String password = buffer.toString();

//----------------------------------------------------------------------------------

Generating Different Random Numbers

//----------------------------------------------------------------------------------
long seed;
Random random = new Random(seed);

//----------------------------------------------------------------------------------
try {
    DataInputStream input = new DataInputStream(System.in);

    Random random = new Random(input.readLong());
} catch (IOException ex) {

    System.err.println("Exception occured while initialising random seed");
}
//----------------------------------------------------------------------------------

Making Numbers Even More Random

//----------------------------------------------------------------------------------
// java.util.Random class already uses a 48-bit seed
//----------------------------------------------------------------------------------

Generating Biased Random Numbers

//----------------------------------------------------------------------------------
// use Random.nextGaussian() method
//----------------------------------------------------------------------------------
/*
 * WeightedDistribution.java
 */
package pleac.ch02.part10;

import java.util.Random;
/**
 * A weighted distribution of values
 */
public class WeightedDistribution {

    private Random random;
    private Object[] values;

    private int[] limits;
    private int total;

    /**
     * Creates a new weighted distribution with given values and given weights
     */
    public WeightedDistribution(Object[] values, int[] weights) {

        if (values.length != weights.length)
            throw new IllegalArgumentException(
                    "values and weights array size don't match");

        this.random = new Random();
        this.values = new Object[values.length];

        this.limits = new int[weights.length];
        for (int i=0; i<values.length; i++) {

            this.values[i] = values[i];
            total += weights[i];

            this.limits[i] = total;
        }
    }

    /**
     * Draws a random value
     */
    public Object draw() {
        int token = random.nextInt(total + 1);

        for (int i=0; i<limits.length; i++)

            if (token <= limits[i])
                return values[i];

        return null;
    }

}

Object values[];

int weights[];

// creates distribution
WeightedDistribution distribution = new WeightedDistribution(values, weights);

// draws a value from it
Object value = distribution.draw();
//----------------------------------------------------------------------------------
Random random = new Random();

int mean = 25;
int sdev = 2;
double salary = random.nextGaussian() * sdev + mean;

System.out.println("You have been hired at " + salary);
//----------------------------------------------------------------------------------

Doing Trigonometry in Degrees, not Radians

//----------------------------------------------------------------------------------
// use Math.toDegrees() and Math.toRadians() methods
//----------------------------------------------------------------------------------
double radians = Math.toRadians(degrees);

double degrees = Math.toDegrees(radians);
//----------------------------------------------------------------------------------
/**
 * Returns sine of an angle expressed in degrees
 */
double degreeSine(double a) {

    return Math.sin(Math.toRadians(a));
}
//----------------------------------------------------------------------------------

Calculating More Trigonometric Functions

//----------------------------------------------------------------------------------
// use Math.tan() method
//----------------------------------------------------------------------------------
double y = Math.acos(3.7);

//----------------------------------------------------------------------------------

Taking Logarithms

//----------------------------------------------------------------------------------
double log = Math.log(10000.00);
//----------------------------------------------------------------------------------
/**
 * Returns log in given base of given value
 */
double logBase(int base, double value) {

    return Math.log(value)/Math.log(base);
}
//----------------------------------------------------------------------------------
double answer = logBase(10, 10000.00);

System.out.println("log10(10000) = " + answer);
log10(10,000) = 4

//----------------------------------------------------------------------------------

Multiplying Matrices

//----------------------------------------------------------------------------------
/**
 * Compute multiplication of two given matrices
 */
int[][] multiplyMatrix(int[][] matrix1, int[][] matrix2) {

    if (matrix1 == null)
        throw new IllegalArgumentException("Null height matrix1");

    int rows1 = matrix1.length;

    if (matrix1[0] == null)

        throw new IllegalArgumentException("Null width matrix1");
    int cols1 = matrix1[0].length;

    if (matrix2 == null)
        throw new IllegalArgumentException("Null height matrix2");

    int rows2 = matrix2.length;

    if (matrix2[0] == null)

        throw new IllegalArgumentException("Null width matrix2");
    int cols2 = matrix2[0].length;

    if (cols1 != rows2)
        throw new IllegalArgumentException("Matrices don't match: " + cols1 + " != " + rows2);

    int[][] result = new int[rows1][cols2];

    for (int i=0; i<rows1; i++)
        for (int j=0; j<cols2; j++)

	    for (int k=0; k<cols1; k++)

                result[i][j] += matrix1[i][k] * matrix2[k][j];

    return result;
}
//----------------------------------------------------------------------------------
int[][] matrix1 = new int[][] {

   {3, 2, 3},
   {5, 9, 8}

};

int[][] matrix2 = new int[][] {
   {4, 7},
   {9, 3},
   {8, 1}

};

int[][] product = multiplyMatrix(matrix1, matrix2);
//----------------------------------------------------------------------------------

Using Complex Numbers

//----------------------------------------------------------------------------------
/**
 * A basic complex number implementation
 */

public class Complex {

    private int real;
    private int imaginary;

    public Complex(int real, int imaginary) {

	this.real = real;
	this.imaginary = imaginary;
    }

    public int getReal() {

        return real;
    }

    public int getImaginary() {

        return imaginary;
    }

    public Complex multiply(Complex other) {

	return new Complex(
           (this.real * other.getReal()) - (this.imaginary * other.getImaginary()),
           (this.real * other.getImaginary()) + (this.imaginary * other.getReal())

       );
    }

    public String toString() {

        if (imaginary > 0)
	    return real + "+" + Math.abs(imaginary) + "i";

        if (imaginary < 0)
	    return real + "-" + Math.abs(imaginary) + "i";

	return real;
    }

}
//----------------------------------------------------------------------------------
Complex a = new Complex(3, 5);  // 3 + 5i

Complex b = new Complex(2, -2); // 2 - 2i
Complex c = a.multiply(b);

System.out.println("c = " + c);
//=> c = 16+4i
//----------------------------------------------------------------------------------

Converting Between Octal and Hexadecimal

//----------------------------------------------------------------------------------
int number = Integer.parseInt(hexadecimal, 16); // hexadecimal

int number = Integer.parseInt(octal, 8);        // octal
//----------------------------------------------------------------------------------
System.out.println("Gimme a number in decimal, octal, or hex: ");
try {

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

    String num = input.readLine().trim();
    int number;
    if (num.startsWith("0x"))

	number = Integer.parseInt(num.substring(2), 16);
	else if (num.startsWith("0"))

	    number = Integer.parseInt(num.substring(1), 8);
	    else
		number = Integer.parseInt(num);

    System.out.println("Decimal value: " + number);
} catch (IOException ex) {

    System.err.println("Exception occured while getting input");
} catch (NumberFormatException ex) {

    System.err.println("Exception occured while parsing input");
}
//----------------------------------------------------------------------------------
System.out.println("Enter file permission in octal: ");
try {

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

    String num = input.readLine().trim();
    int permission = Integer.parseInt(num, 8);

    System.out.println("Decimal value: " + permission);
} catch (IOException ex) {

    System.err.println("Exception occured while getting input");
} catch (NumberFormatException ex) {

    System.err.println("Exception occured while parsing input");
}
//----------------------------------------------------------------------------------

Putting Commas in Numbers

//----------------------------------------------------------------------------------
// using a localized number format
NumberFormat format = NumberFormat.getInstance(locale);

format.format(value);
//----------------------------------------------------------------------------------
// using a customized number format
DecimalFormatSymbols symbols = new DecimalFormatSymbols();

symbols.setGroupingSeparator(',');
DecimalFormat format = new DecimalFormat();
format.setDecimalFormatSymbols(symbols);

format.format(value);
//----------------------------------------------------------------------------------
int hits = truly_random_value(); // negative hits!
System.out.println("Your web page received " + format.format(hits) + " accesses last month.");

//=> Your web page received -1,740,525,205 accesses last month.
//----------------------------------------------------------------------------------

Printing Correct Plurals

//----------------------------------------------------------------------------------
System.out.println("It took " + time + " hour" + ((time == 1) ? "" : "s"));

System.out.println(time + " hour" + ((time == 1) ? " is" : "s are") + " enough");  

//----------------------------------------------------------------------------------
double[] limits = new double[] {1, ChoiceFormat.nextDouble(1)};

String[] names = new String[] {"century", "centuries"};

ChoiceFormat format = new ChoiceFormat(limits, names);
System.out.println("It took " + time + " " + format.format(time));

//----------------------------------------------------------------------------------
Perl5Util util = new Perl5Util();
/**
 * Compute plural of given noun
 */
String nounPlural(String noun) {

    // order really matters here!
    if (util.match("/ss$/", noun))
	return util.substitute("s/ss$/sses", noun);

    if (util.match("/([psc]h)$/", noun))
	return util.substitute("s/([psc]h)$/${1}es/", noun);

    if (util.match("/z$/", noun))
	return util.substitute("s/z$/zes/", noun);

    if (util.match("/ff$/", noun))
	return util.substitute("s/ff$/ffs/", noun);

    if (util.match("/f$/", noun))
	return util.substitute("s/f$/ves/", noun);

    if (util.match("/ey$/", noun))
	return util.substitute("s/ey$/eys/", noun);

    if (util.match("/y$/", noun))
	return util.substitute("s/y$/ies/", noun);

    if (util.match("/ix$/", noun))
	return util.substitute("s/ix$/ices/", noun);

    if (util.match("/([sx])$/", noun))
	return util.substitute("s/([sx])$/$1es/", noun);

    return util.substitute("s/$/s/", noun);
}
//----------------------------------------------------------------------------------

Program: Calculating Prime Factors

//----------------------------------------------------------------------------------
//=> # java pleac.ch02.part19.BigFact 8 9 96 2178
//=> 8: 2^3
//=> 9: 3^2
//=> 96: 2^5 3
//=> 2178: 2 3^2 11^2
//----------------------------------------------------------------------------------
//=> # java pleac.ch02.part19.BigFact 25000000000000000000000000
//=> 25000000000000000000000000: 2^24 5^26
//=> # java pleac.ch02.part19.BigFact 239322000000000000000000
//=> 239322000000000000000000: 2^19 3 5^18 39887
//----------------------------------------------------------------------------------
/*
 * BigFact.java
 */

package pleac.ch02.part19;

import java.math.BigInteger;

import java.util.*;
/**
 * Calculate prime factors
 * Usage: java pleac.ch02.part19.BigFact [-b] <number>
 */
public class BigFact {

    private static final Integer ONE = new Integer(1);

    private static TreeMap getFactors(int n) {

        int orig = n;
        TreeMap factors = new TreeMap();

        int i, sqi;
        for (i = 2, sqi = 4; sqi <= n; sqi += 2 * i++ + 1) {

            while (n % i == 0) {
                n /= i;
                Integer factor = new Integer(i);

                if (! factors.containsKey(factor))
                    factors.put(factor, ONE);
                else

                    factors.put(factor, new Integer(((Integer) factors.get(factor)).intValue() + 1));

            }
        }
        if ((n != 1) && (n != orig))

            factors.put(new Integer(n), ONE);
        return factors;

    }

    private static TreeMap getFactors(BigInteger n) {

        BigInteger orig = n;
        TreeMap factors = new TreeMap();

        BigInteger i = new BigInteger("2");
        BigInteger sqi = new BigInteger("4");

        BigInteger step = new BigInteger("2");
        while (sqi.compareTo(n) <= 0) {

            while (n.mod(i).equals(BigInteger.ZERO)) {
                n = n.divide(i);

                if (! factors.containsKey(i))
                    factors.put(i, BigInteger.ONE);
                else

                    factors.put(i, ((BigInteger) factors.get(i)).add(BigInteger.ONE));

            }
            sqi = sqi.add((i.multiply(step)).add(BigInteger.ONE));
            i = i.add(BigInteger.ONE);

        }
        if ((! n.equals(BigInteger.ONE)) && (! n.equals(orig)))

            factors.put(n, BigInteger.ONE);
        return factors;
    }

    private static void displayFactors(int n, TreeMap factors) {

        System.out.print(n + ": ");
        if (factors.isEmpty())
            System.out.println("PRIME");

        else {
            Iterator it = factors.keySet().iterator();
            while (it.hasNext()) {

                Object factor = it.next();
                Object power = factors.get(factor);

                System.out.print(factor + ((power.equals(ONE))? "": "^" + power) + " ");

            }
            System.out.println();
        }
    }

    private static void displayFactors(BigInteger n, TreeMap factors) {

        System.out.print(n + ": ");
        if (factors.isEmpty())
            System.out.println("PRIME");

        else {
            Iterator it = factors.keySet().iterator();
            while (it.hasNext()) {

                Object factor = it.next();
                Object power = factors.get(factor);

                System.out.print(factor + ((power.equals(BigInteger.ONE))? "": "^" + power) + " ");

            }
            System.out.println();
        }
    }

    public static void main(String args[]) {

        if (args.length < 1) {
            System.err.println("Usage: java pleac.ch02.part19.BigFact [-b] <number>...");

            System.exit(1);
        }

        boolean useBigInteger = false;

        int index = 0;

        if (args[0].equals("-b")) {

            useBigInteger = true;
            index = 1;
        }

        for (int i=index; i<args.length;i++) {

            if (useBigInteger) {
                BigInteger n = new BigInteger(args[i]);

                displayFactors(n, getFactors(n));
            } else {
            try {

                int n = Integer.parseInt(args[i]);
                displayFactors(n, getFactors(n));

                } catch (NumberFormatException ex) {
                    BigInteger n = new BigInteger(args[i]);

                    displayFactors(n, getFactors(n));
                }
            }
        }

    }

}
//----------------------------------------------------------------------------------

Connecting to a mysql database with JDBC

Tuesday, September 9th, 2008

This is a crucial step in designing a Java application, but many people over look a few important things though. There are different way to make the connection, but not all are created equal. Some are more efficient than others.

First, you need to import your classes. Here is where many people make their first mistake. They will say import java.sql.*. This will work but is very inefficient. This will import all classes that have the the prefix of java.sql, it will not include many redundant classes but it will make your program run slower. You should only import the classes you need. In this case you need three.

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

Second, I like to always set my variables when I initialize, that way I know what the value of it from the beginning. Many think this is a redundant step but it has saved me a great deal of headache in the past.

Connection connect = null;

Third, it is not required but when you load the driver for the connection use the newInstance() argument with it. It will let you create a dynamically loaded class for it.

Class.forName(“com.mysql.jdbc.Driver”).newInstance();

Now it is time for us to make our connection to the database. This is the simplest part of the process. You just specify the url, username, and password of the database. For security reasons, it is good to never use your root account in a program. If someone does get the user name and password, they will only have access to just one of the databases instead of all of them.

connect = DriverManager.getConnection(“url”, “user”, “password”);