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.

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);




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.


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


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.


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/

 

This example shows how to copy a scheduled backup job from one instance of SQL server 2005 to another.

In Microsoft SQL Server Management Studio, expand the SQL Server Agent tree.

Expand the jobs tree.

Here you will see a list of all the jobs currently stored on the server.

 

Right-click on the job that you want to copy over to a second server.
Select Script Job as –> Create To –> New Query Editor Window.

 

This will open a new window in the management studio containing auto-generated code.

Connect to the Second server, which will be the destination for the copied job.

Open up a new tab in the management studio – Copy the entire auto-generated code from the previous step and paste it into the new tab.

 

Below is the auto-generated code ready to execute on the new server.
Execute the whole code to continue.

 

Refresh and expand the Jobs tab on the second server, all being well the copied job will be displayed in the job list.

 

Right-click the job and select Properties to view the details.

You will see that the steps, schedules, alerts, notifications etc. will have been copied across. The job will now run on server 2 in the same way as it ran on server 1.

 

How to allow SQL server to allow incoming connections

 

Open SQL server service area configuration

 

 

Start à all programs à SQL server 2005 à configuration tools à SQL server service area configuration

 

Select: Surface Area Configuration for Services and Connections.

 

Under the Database Engine tree, select Remote connections.

 

Select Local and remote connections.

 

I use Using both TCP/IP and named pipes, but select the option that fits your requirements.

 

 

Click apply.

 

Next you will need to re-start the Database Engine service.

 

Select the Service option under Database Engine Tree.

 

 

 

 

 

When the service has stopped successfully, slick the Start button to initiate the service.

This example shows how to build up a string to post to a web form, and return the results generated.

Element1 is the first variable the the post accepts – “Value 1″ is the literal string bieng passed to it.
Element2 is the second variable that the post accepts – “Value 2″ is the literal string bieng passed to it.
Element3 is the third variable that the post accepts – “Value 2″ is the literal string bieng passed to it.

http://www.examplewebsite.asp is the web page that the data is bieng posted to.

 

//Create Post String
String data = URLEncoder.encode(”Element1″, “UTF-8″) + “=” + URLEncoder.encode(”Value 1″, “UTF-8″);
data += “&” + URLEncoder.encode(”Element2″, “UTF-8″) + “=” + URLEncoder.encode(”Value 2″, “UTF-8″);
data += “&” + URLEncoder.encode(”Element3″, “UTF-8″) + “=” + URLEncoder.encode(”Value 3″, “UTF-8″);
                
         
// Send Data To Page
URL url = new URL(”http://www.examplewebsite.asp”);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
   
// Get The Response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
        System.out.println(line);
        //you Can Break The String Down Here
}

 

You can manipulate the line variable to read in the information returned from the post.

Here is a very simple script to return model and serial number information for your computer.

To retrieve serial number of the computer run the following command from command line

wmic bios get serialnumber

To retrieve model name of the computer run the following command from command line

wmic csproduct get name

This code doesn’t return information for all computers, but I’ve found it very usefull where its available.

Next Page »