20
May/10
0

Selecting MySQL rows within a Time Range for a Java Program

Earlier I discussed selecting rows from a table within a specified date range. What if you need to select rows from within a time range?

Again a SELECT statement with a WHERE clause is needed. The  method below will give the statement for the start and end times of the statement. Unlike the earlier example this has a DateFormat to supply the String required to generate the SQL statement.

GregorianCalendar calStart = new GregorianCalendar() ;
 calStart.set( Calendar.HOUR_OF_DAY, 18 ) ;
 calStart.set( Calendar.MINUTE, 1 ) ;
 String sql = "SELECT * FROM " + dbh.userTable.tableName +
          " WHERE status=1 AND "
          + SQLStrings.getWhereBetweenDates( "signup_time", calStart ) ;

.....

public static String getWhereBetweenDates( String columnName, GregorianCalendar calStart)
{
    GregorianCalendar now = new GregorianCalendar() ;
    StringBuffer sb = new StringBuffer( columnName + " BETWEEN " ) ;
    SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) ;
    String beforeDateString = df.format( calStart.getTime() ) ;
    String afterDateString = df.format( now.getTime() ) ;
    sb.append( beforeDateString + " AND " + afterDateString ) ;
    return sb.toString() ;
}

Note that the DateFormat String here was determined by what was present in the MySQL database for that particular column. YMMV – so if there are problems maybe check your database table to see what particular format that column takes.

Filed under: Java, MySQL
6
May/10
0

Selecting MySQL Rows with a Timestamp Column on a Single Day or Month from your Java Program

You have a MySQL database table with a Timestamp column, and you need to select entries for a single day, month or year. As indicated in previous posts, you can construct MySQL statements that allow you to access items from a certain date/month/year.

This can be achived using the following.

String sql =
“SELECT * FROM orders WHERE day(time_outgoing)=” + day ;
Statement st = con.createStatement() ;
ResultSet rs = st.executeQuery(sql) ;

Something similar can be done for months and years, and Java’s Calender class provides fields for moth access. However note that the java.util.Calender fields for months are indexed from 0, so the actual integer value of Calender.MAY is 4 and not 5.

int month = Calender.MAY ; //want entries for may

//increase the month by 1 to access values

String sql =
“SELECT * FROM orders WHERE month(time_outgoing)=” + (month+1) ;
Statement st = con.createStatement() ;
ResultSet rs = st.executeQuery(sql) ;

OR

SELECT *
FROM optout75308
WHERE logtime
BETWEEN “2010-05-21″
AND “2010-05-22″
AND (
contact_method = ‘ODWBPRESS1′
OR contact_method = ‘SERV2OWDBy’
)

6
May/10
0

Reading a TimeStamp Value from a MySQL Database in your Java Program

So, you’ve written your database table to have a Timestamp column in it. Often this can be used to log when an entry is created or updated. This post will detail a couple of things that may be useful in doing more than just having it logged.

Java provides a java.sql.Timestamp class that can be used to read and alter timestamp values.

So if your database column is called signup_time, it can be read out as follows

import java.sql.Timestamp;

        Statement st = con.createStatement() ;
        ResultSet rs = st.executeQuery(  “SELECT * FROM subscribers”  ) ;
        StandardSubscriptionUser users[] = new StandardSubscriptionUser[ count ] ;
        rs.beforeFirst() ;
        while( rs.next() && count>0 )
        {

               signupTime = rs.getTimestamp("signup_time") ;

        }

The Timestamp class is a subclass of java.util.Date. However there is not quite a match between how java.util.Date and java.sql.Timetamp store the time. This means that a Timestamp object should not be used where Date objects are required in functions. As the API says “The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance.”

However it does mean that the standard Date API methods can be used to do things with the Timestamp object, meaning there is a set of standard methods to manipulate and access the values with.

30
Apr/10
1

Selecting MySQL Rows with Timestamp Column in a date range from a Java Program

You have a MySQL database table with a Timestamp column, and you need to select a range of dates in which you need to select entries from.

As ever a SELECT statement with a WHERE clause is needed. The following will give you a range of dates from the first to the last. Note that without times specified it is the start of each day. So although 2008-08-31 is specifed no entries on that date will actually be included in the returned results.

String sqlWhereDate =
                " order_placed BETWEEN \”1995-05-01\” AND \”2008-08-31\” ” ;

String sql = “SELECT * FROM orders WHERE ” + sqlWhereDate ;

Tagged as:
4
Apr/10
0

Netbeans Editor Code Folds

Some times its handy to be able to hide sections of code in the Netbeans editor window.

// <editor-fold desc="This section of the code deals with the item.">
 Your code goes here...
// </editor-fold>

These are just the basics. More information is available from the Netbeans page.

30
Mar/10
0

jQuery / Java / Autocomplete

If you haven’t heard of jQuery, its a Javascript library that allows you to implement AJAX functionality in your web pages. You can find out more about it from the jQuery website.

My first attempt with jQuery involved using an autocomplete  plugin provided by Pengoworks. Autocomplete is where you start typing in a few letters and the text box will come up with some suggestions. There was a tutorial for this present on the jquery autocomplete page, but it didn’t quite meet my needs, and maybe it doesn’t meet yours.

There are two ways in which to use the auto complete functionality. If there are a few options (ie upto 50) the list of suggestions can be embedded in the web page itself, but if there are many options (1000s) the suggestions need to come back from a server. It was this second process that the tutorial did not cover, and hence the reason for providing a step by step guide to doing it here.

In partcular, I’m working from Java in Netbeans 6.8.

3
Aug/09
0

VisualWeb JSF being discontinued

One of the primary reasons for starting this blog was to log any nifty tricks I learnt whilst using Netbeans and VisualWeb JSF. However this has now been converted to a non-essential Sun item. I was also planning to document IceFACES, an AJAX support framework, which no longer supports a Visual developer setup with Netbeans 6.7 due to the removal of the ‘Woodstock’ components for the core IDE. Being new with IceFACES from within a text editor looses the productivity gains it should provide.

As I have some projects looming up and have to think about maintenance down the line, I have decided to make the switch to Eclipse – specifically I am using the MyEclipse platform. Its not free as in beer, but comes with lots of neat addons tied in with it, so I am putting this through its paces.

7
Jul/09
1

sun.misc.BASE64Encoder replacement required

The package sun.misc.BASE64Encoder that was used in some code I was referencing came up with a warning: sun.misc.BASE64Encoder is Sun proprietary API and may be removed in a future release

Not good for futureproofing!

Its replacement can be found using the relevant class from Apache Commons library. This is in the Codec suite of the Apache Commons library.

Filed under: Java
3
Jul/09
1

VisualWeb JSF: linking form elements to a database (Part I:Displaying)

One of the advantages of using the VisualWeb JSF framework in Netbeans is that it makes the process of storing user data submitted in a web page form easy to store into a database.

The Prep Work

The first thing to do is to ensure your database has been setup with the tables required, and a user that can add data to a table has also been created. This is something I will add in a future post at some point.

As a start point you should have the VisualWeb JSF plugin installed to your copy of Netbeans 6 and created a web application that uses the VisualWeb JSF framework.

Two pages need to created for this application. By default there is a ‘Page1.jsp’ and supporting files created with the application. Add a second page by right clicking on ‘Web Pages’ and choose ‘New -> Visual Web JSF Page’. Label this ‘Page2.jsp’.

Create the form on Page 1 by opening up Page1.jsp. Ensure you are in the Design View for the page. The Woodstock pallette contains the item to use. A full rundown of all the components is available here – we are going to use the input text field and a submit button.

Part I

The first fun thing to do would be to populate an element from your database.

  • In the Services Tab, ensure the database you have chosen is connected.
  • Drag a listbox over from the Woodstock Basic Panel across to the page. By default it will be filled with ‘Item 1, Item 2 and Item3′, which we want to change to something from a database.
  • Give it a label in the Appearance section of the Properties window.
  • In the Services tab, go to the table whose data you want to be included in the list box.
  • Drag this table into the page.
  • Right click on the ListBox Item and right click on it. Choose ‘Bind To Data’ from the menu that appears.
  • In the ‘Bind To Data Provider’.
  • Choose the table and column you wish to display in the table.
  • Now when you deploy and run the application in your browser the information should appear in the table