Tuesday, September 22, 2009

Java: String index out of range

If you try a substring with an index greater than the length of the string then - String index out of range - exception is be thrown by Java.

Thursday, September 17, 2009

How to drop Not Null constraint

SQL>

ALTER TABLE table_name MODIFY column [datatype] NULL;

datatype is optional if you only want to modify the Not Null constraint. Doing this will allow Oracle to enter null in the column if no value is provided.

Oracle date do not support "0000-00-00"

The date string "0000-00-00" is invalid in Oracle, so it should not be used. However, the date "0001-01-01" is valid in both Oracle. But MySQL support "0000-00-00" value as a default value for date.

Instead Oracle date column accept only null value if date is not available.

Thursday, July 23, 2009

JSP compilation

The JSP pages are compliled into Servlet classes the first the JSP in invoked.

Like Servlet, JSP pages operate within a container. The JSP container provides the same services as a Servlet container. But requires the additional steps of conversion to Servlet code and compilation before the JSP pages are executed.

Tomcat server includes both Servlet and compiled JSP pages(named Catanilna), and the compiler for JSP pages called Jasper compiler. The combination of JSP container and Servlet container is known as a Web Container.

Primary key, Candidate key, Unique key

Primary Key:
The primary key of a relational table uniquely identifies each record in the table. A Primary key can not be NULL at all. Primary keys may consist of a single attribute or multiple attributes in combination.

Candidate Key:
An attribute/key is a candidate key if it is able to be a Primary key.

Foreign key:
A Foreign key of a relational table is a Primary key in another table such that
the Foreign key should be a proper subset of the Primary key, i.e each and every in the Foreign key can only come from the Primary key.

Unique Key:
A Unique key is of a relational table uniquely identifies each record in the table but is can be NULL.

Important Facts:
  • Every candidate key can be a primary key.
  • Every candidate key is always a unique key.
  • This implies every primary key is also a unique key
  • But every unique key can not always be a primary/candidate key.
  • Because a primary key can not be a NULL value but a unique key can be a NULL value.

Wednesday, July 15, 2009

MySQL 4.1 : TIMEDIFF( , ) : java.sql.SQLException : Illegal hour value for java.sql.Time type

MySQL's TIMEDIFF runs well on MySQl Shell but is an sql exception when using with JDBC.
The problem is that the TIMEDIFF(expr1,expr2) function returns returns expr1expr2 expressed as a time value. This value is handled by java.sql.Time. But TIMEDIFF( , ) may return (for example) 12:27:58 or 48:30:58 as the case may be. For first value it works but for the second value it is not a proper time value according to java.sql.Time, hence the exception.

However
this is a known bug in mysql 5.0

A workaround of this problem is to convert this query to return a string.
E.g. you can query like this

CONCAT('',TIMEDIFF(expr1,expr2))

Then the returned value will be a string instead of a time value and JDBC will not parse it.

Tuesday, July 7, 2009

Subnet Masking

Subnet mask
A subnet mask is a mechanism used to split a network into subnetworks; it can be used to reduce the traffic on each subnetwork by confining traffic to only the subnetwork(s) for which it is intended, thereby eliminating issues of associated congestion on other subnetwork(s) and reducing congestion in the network as a whole. Each subnet functions as though it were independent, keeping traffic local and forwarding traffic to another subnetwork only if the address of the data is external to the subnetwork.

Subnetting Concept
Subnetting an IP network allows for the flow of network traffic to be segregated based on a network configuration. It essentially organizes the hosts into logical groups, and provides for improving network security and performance. The most common reason for subnetting IP networks is to control network traffic. Traditionally, in an Ethernet network, it is very common for all nodes on a segment to see all the packets transmitted by all the other nodes on that segment, which introduces collisions, and the resulting retransmissions under heavy traffic loads. For additional information on subnetting, see RFC 1817, and RFC 1812.

Class Address Ranges
Class A - 1.0.0.0 to 126.0.0.0
Class B - 128.0.0.0 to 191.255.0.0
Class C - 192.0.1.0 to 223.255.255.0
Class D* - 224.0.0.0 to 239.255.255.255
Class E* - 240.0.0.0 to 255.255.255.255
Class A, Class B, and Class C are the three classes of addresses used on IP networks in common practice. Class D addresses are reserved for multicast. Class E addresses are simply reserved, meaning they should not be used on IP networks (used on a limited basis by some research organizations for experimental purposes).

Reserved Address Ranges
Address ranges below are reserved by IANA for private intranets, and not routable to the Internet. For additional information, see RFC 1918.
10.0.0.0 - 10.255.255.255 (10/8 prefix)
172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
192.168.0.0 - 192.168.255.255 (192.168/16 prefix)

Other reserved addresses
127.0.0.0 is reserved for loopback and IPC on the localhost.
224.0.0.0 - 239.255.255.255 is reserved for multicast addresses.
255.255.255.255 is the limited broadcast address (limited to all other nodes on the LAN)

Subnet Calculator Explanation

When the host-id is cancelled, i.e. when the bits reserved for the machines on the network are replaced by zeros (for example 194.28.12.0), something called a network address is obtained. This address cannot be allocated to any of the computers on the network.
When the net-id is cancelled, i.e. when the bits reserved for the network are replaced by zeros, a machine address is obtained. This address represents the machine specified by the host-ID which is found on the current network.
When all the bits of the host-id are at 1, the address obtained is called the broadcast address. This a specific address, enabling a message to be sent to all the machines on the network specified by the net-ID.
Conversely, when all the bits of the net-id are at 1, the address obtained is called the multicast address.
Finally the address 127.0.0.1 is called the loopback address because it indicates the localhost.

The Network part of the IP address is deduced by operating a binary AND between the IP address and the Network Mask of the Class. The binary AND will let "as-is" all binary values combined with "1"s and will zero all others (see "Binary operators").
For example, for Class A:

IP address : aaaaaaaa.bbbbbbbb.cccccccc.dddddddd
Network mask: 11111111.00000000.00000000.00000000
Binary AND --------------------------------------
Network : aaaaaaaa.00000000.00000000.00000000

The Host part of the IP address is deduced by operating a binary AND between the IP address and the Network Mask of the Class inversed (binary NOT).
For example, for the same Class A:
IP address : aaaaaaaa.bbbbbbbb.cccccccc.dddddddd
Network mask inversed: 00000000.11111111.11111111.11111111
Binary AND --------------------------------------
Host : 00000000.bbbbbbbb.cccccccc.dddddddd

Example:
Dot-decimal Address | Operator | Binary
-----------------------------------------------------------------------------------------------
IP address | 192.168.5.10 |-| 11000000.10101000.00000101.00001010

Subnet Mask | 255.255.255.0 |-| 11111111.11111111.11111111.00000000

Subnet Mask Inversed | 0.255.255.255 | Binary NOT | 00000000.11111111.11111111.11111111

Network ID/Portion 192.168.5.0 | Binary AND | 11000000.10101000.00000101.00000000

Host ID/Portion 0.0.0.10 | Binary AND | 00000000.00000000.00000000.00001010

This calculator will calculate the subnet mask to use, given a TCP/IP network address and the number of subnets or nodes per subnet required.

To create the subnet mask, first remember that the purpose of the subnet mask is to separate the (32 bit) ip address into the network prefix and the host number. If a bit in the subnet mask is 1, the corresponding bit in the IP address is part of the network address; if the bit in the subnet mask is 0, the corresponding bit in the IP address is part of the host address.

First depict the ip address in binary. Take 61.246.19.18 and convert to binary:
ip address: 00111101.11110110.00010011.00010010

First we determine what class of address it is:
• If the first bit is 0 it is a Class A address.
• If the first two bits are 10 it is a Class B address.
• If the first three bits are 110 it is a Class C address.
• If the first four bits are 1110 it is a Class D multicast address.
• If the first four bits are 1111 it is a Class E experimental address

Your example is a Class A address. The default subnet mask for a Class A address is:
subnet mask: 11111111.00000000.00000000.00000000


The formula for figuring out the number of 'host' bits in a subnet mask is:

2^n=number of nodes

n is number of ‘0’s from the right side of the mask.
If you know the number of host/nodes, you need to find 'n'.

if you want 64 node(s), you want to leave 6 - '0' bits in the subnet mask since 64 = 2 ^ 6.

This will give you the following subnet mask:
subnet mask: 11111111.11111111.11111111.11000000
Which is referred to as /26 or in dotted decimal notation as 255.255.255.192

The Subnet Mask and Class determines how many subnets and hosts you get (i.e. how you subnet you network).

The number of hosts is: 2^(number of host bits)-2
Class | Number of possible networks | Maximum number of computers on each one
A | 126 | 16777214
B | 16384 | 65534
C | 2097152 | 254


Notations:
o Dotted decimal notation a.b.c.d
o We denote a mask address as /x if number of ‘1’s in the mask address is x.