Rob Bamforth's Blog
Java, SQL, MySQL, Networking, SQL Server, PHP, COM, DBA + ODBC

Oct
14

Here is a simple method to calculate seconds since midnight for the current time.
There are a few methods to achieve the same result. This is a quick and easy way.

public getSecondsSinceMidnight() {         

    DateFormat dateFormat = new SimpleDateFormat();      
    java.util.Date date = new java.util.Date();

    dateFormat = new SimpleDateFormat(“HH”);
    date = new java.util.Date();
    int hour = Integer.parseInt(dateFormat.format(date));         

    dateFormat = new SimpleDateFormat(“mm”);
    date = new java.util.Date();
    int minute = Integer.parseInt(dateFormat.format(date));

    dateFormat = new SimpleDateFormat(“ss”);
    date = new java.util.Date();
    int second = Integer.parseInt(dateFormat.format(date));   

    int secondsSinceMidnight = (hour* 3600) + (minute * 60) + second;      

    System.out.println(“Current Time:” + hour + “:” + minute + “:” + second);
    System.out.print(“Seconds Since Midnight: ” + secondsSinceMidnight);                       

 }

Amongst other things, seconds since midnight can be used in calculations to establish accurate differences between 2 times.

Aug
20

Here is a very simple, but effective way to stop web users being able to see the contents of a folder on a web host.

put index.htm file in the folder.

When the web user navigates to the folder, the index.htm file will display,  rather than the contents of the folder.
The htm file can be empty, in which case the browser ill display a blank page.

Simple.. but effective!

Jul
31

To Enable a Job: 

 

 

UPDATE
 MSDB.dbo.sysjobs
SET
    Enabled = 1
WHERE Name = ‘JAB_NAME’;

 

To Disable a Job: 

 

 

UPDATE
MSDB.dbo.sysjobs
SET
    Enabled = 0
WHERE Name = ‘JAB_NAME’;
Jul
28

The CHARINDEX() function returns the location of a search string within another string.

For Example: 

 

  Will return 8, which equates to the position of ’sentance’ within the string ‘This is a sentance’.

 

select charindex(’sentance’,‘This is a sentance’) 

 

 

This can be used to check if string A is contained in String B by using the following command: 

 

  The above command will return records where the field NAME contains the string ‘ROB’.

 

select * from TABLE 
where 
charindex(‘ROB’,NAME) != 0 

 

Jun
15

The following code illustrates how to derive an age from a given DOB.

This example uses the DOB format YYYY-MM-DD.

This can be altered to any format by changing the substrings taken from the original DOB input.

String dob = “1984-09-20″;

//TAKE SUBSTRINGS OF THE DOB SO SPLIT OUT YEAR, MONTH AND DAY
//INTO SEPERATE VARIABLES
int yearDOB = Integer.parseInt(dob.substring(0, 4));
int monthDOB = Integer.parseInt(dob.substring(5, 7));
int dayDOB = Integer.parseInt(dob.substring(8, 10));

//CALCULATE THE CURRENT YEAR, MONTH AND DAY
//INTO SEPERATE VARIABLES
DateFormat dateFormat = new SimpleDateFormat(“yyyy”);
java.util.Date date = new java.util.Date();
int thisYear = Integer.parseInt(dateFormat.format(date));

dateFormat = new SimpleDateFormat(“MM”);
date = new java.util.Date();
int thisMonth = Integer.parseInt(dateFormat.format(date));

dateFormat = new SimpleDateFormat(“dd”);
date = new java.util.Date();
int thisDay = Integer.parseInt(dateFormat.format(date));

//CREATE AN AGE VARIABLE TO HOLD THE CALCULATED AGE
//TO START WILL – SET THE AGE EQUEL TO THE CURRENT YEAR MINUS THE YEAR
//OF THE DOB
int age = thisYear – yearDOB;

//IF THE CURRENT MONTH IS LESS THAN THE DOB MONTH
//THEN REDUCE THE DOB BY 1 AS THEY HAVE NOT HAD THEIR
//BIRTHDAY YET THIS YEAR
if(thisMonth < monthDOB){
age = age – 1;
}

//IF THE MONTH IN THE DOB IS EQUEL TO THE CURRENT MONTH
//THEN CHECK THE DAY TO FIND OUT IF THEY HAVE HAD THEIR
//BIRTHDAY YET. IF THE CURRENT DAY IS LESS THAN THE DAY OF THE DOB
//THEN REDUCE THE DOB BY 1 AS THEY HAVE NOT HAD THEIR
//BIRTHDAY YET THIS YEAR
if(thisMonth == monthDOB && thisDay < dayDOB){
age = age – 1;
}

//THE AGE VARIBALE WILL NOW CONTAIN THE CORRECT AGE
//DERIVED FROMTHE GIVEN DOB
System.out.println(age);



This process calculates the current year, month and date to work out if the DOB has already passed this year in order to accuratly establish the age.

Jun
09

This is a simple method for generating a random number between zero (0) and a given number.

The following code example with generate a random number between 0 and 49.

Although the value of maxNumber is set to 50, Java will start the count at 0.

If you want a number between 0 and 50, set the value of maxNymber to 51.

int maxNumber = 50;
int randomNumber = (int)Math.floor(Math.random() * maxNumber);


Jun
04



The following codes assumes that you have created a JScrollPane() called scrollPane


int x = 0;
int y = 10;

scrollPane.getViewport().setViewPosition(new java.awt.Point(x, y));





In this example x sets the horizontal position of a horizontal scroll bar.
y sets the vertical position of a vertical scroll bar – in this case the vertical scroll bar will appear 10 pixels down from the top.

Jun
04


Data transfer speed table

USB 1.0: > 2 MPPS
USB 2.0 FULL SPEED: 12 MBPS
USB 2.0 HIGH SPEED: 180 MBPS
FIREWIRE-400: 400 MBPS
FIREWIRE-800: 800 MBPS
ENTERNET [10BASE-T]: 10 MBPS
FAST ETHERNET [100BASE-T]: 100 MBPS
GIGABIT ETHERNET [1000BASE-X]: 1000 MBPS
ATA-133: 1064 MBPS
SATA-150: 1200 MBPS





NOTE: these are rough speeds that may be possible.

The typical sustained transfer rate will be lower. Data rate is also dependent on the system architecture, the PCI bus and the attached USB devices

Jun
03


A very simple method to force case sensitivity in a select statement:

SELECT * FROM DBusers WHERE username = ‘AGENT1′ AND password = ‘paSSworD’ COLLATE SQL_Latin1_General_Cp1_CS_AS


The above statement will only return rows where the password value is exactly paSSworD, case specific.


May
05

The xp_cmdshell option is a server configuration option that enables system administrators to control whether the xp_cmdshell extended stored procedure can be executed on a system. By default, the xp_cmdshell option is disabled on new installations and can be enabled by using the Policy-Based Management or by running the sp_configure system stored procedure as shown in the following code example:

– To allow advanced options to be changed.
EXEC sp_configure ’show advanced options’, 1
GO
– To update the currently configured value for advanced options.
RECONFIGURE
GO
– To enable the feature.
EXEC sp_configure ‘xp_cmdshell’, 1
GO
– To update the currently configured value for this feature.
RECONFIGURE
GO

 

The following link illustrates how to use the xp_cmdshell command  to export data from a table in the database to a flat file:

http://robbamforth.wordpress.com/2008/11/03/sql-outout-data-from-sql-server-to-file-bcp/