Monday, May 16, 2011

Installing Connector/J MySQL JDBC Driver

Download Connector/J MySQL JDBC Driver driver from websites or

vistit the link


http://www.mysql.com/downloads/connector/j/

Among the extracted files will be a JAR file with a name like 'mysql-connector-java-3.0.8-stable-bin.jar'. Copy this file to your %JAVA_HOME%/jre/lib/ext folder which on my system happens to be D:\j2sdk1.4.2\jre\lib\ext>.

You should also add the complete path to this JAR file in your CLASSPATH environment variable. In case if you don't know how to do that, go to Start -> Settings -> Control Panel -> System -> Advanced (tab) -> Environment Variables. Double-click 'CLASSPATH' if it is already listed there or click 'New' to create one with the name of 'CLASSPATH'. If you already had a 'CLASSPATH environment variable listed, simply type in (or paste) the complete path including file name in the window that opens up *at the end of* existing CLASSPATH values. If you are creating a new 'CLASSPATH' environment variable then simple enter '.;' and then append the complete path to Connector/J's JAR file.

Note: The CLASSPATH variable contains a list of semi-colon separated folders and JAR files where JVM will search for Java class files. You should always separate paths with semi-colon (;) so that JVM can understand where one path ends and next one begins. Also keep in mind that in CLASSPATH variable paths, the first path is always '.' (single dot) which means current folder, so that JVM starts the search for Java class files from the current folder.


If you've correctly added the path to Connector/J JDBC driver's JAR file in your CLASSPATH environment variable then you are done with installing MySQL Connector/J JDBC driver.

Tip: When you make changes to any of your environment variables (like CLASSPATH), close and re-open any open command prompt windows so that they refresh their environment variable value's cache.

How to test if Connector/J has been installed and configured correctly?
We will now create a simple Java program which will try to connect to our MySQL database server using Connector/J JDBC driver. Create a new JdbcExample1.java file and copy/paste following code in it:


import java.sql.*;

public class JdbcExample1 {

public static void main(String args[]) {
Connection con = null;

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql:///test", "root", "secret");

if(!con.isClosed())
System.out.println("Successfully connected to MySQL server...");

} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
}


How to configure Connector/J to use named pipes to connect to MySQL Database server?

The above Java program used TCP/IP to connect to MySQL database. We'll now make just one change and this time it will use name pipe to connect to our MySQL server instance.

Simply convert this line in JdbcExample1.java:

con = DriverManager.getConnection("jdbc:mysql:///test", "root", "secret");

To this one:

con = DriverManager.getConnection("jdbc:mysql:///test" +
"?socketFactory=com.mysql.jdbc.NamedPipeSocketFactory",
"root", "secret");


Compile and run this class, this time it'll use named pipe to connect to MySQL database. If you again get a success message then congratulations you've setup named pipe on both sides (server and client) correctly.


Why and when to use named pipes in place of TCP/IP with MySQL/Connector/J?

Named pipes are a high performane alternative to TCP/IP on a Windows NT/2000/XP/2003 only environment. The Connector/J JDBC driver's documentation suggests an increase in data access speed of 30%-50% as compared to TCP/IP. You can always use named pipes in an intranet environment. So to keep it short, use named pipes when you can and TCP/IP when you cannot.

RSA algorithm in JAVA

Rivest, Shamir and Adleman, who invented the RSA code.

RSA Implementation in Java(I found this paper in journal hope that it will help you)

Generating a Pair of Keys

In this example we will generate a public-private key pair for the algorithm named "RSA". We will generate keys with a 1024-bit modulus, using a user-derived seed, called userSeed. We don't care which provider supplies the algorithm implementation.

Creating the Key Pair Generator

The first step is to get a key pair generator object for generating keys for the RSA algorithm:
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

Initializing the Key Pair Generator

The next step is to initialize the key pair generator. All key pair generators share the concepts of a keysize and a source of randomness. A KeyPairGenerator class initialize method has these two types of arguments. Thus, to generate keys with a keysize of 1024 and a new SecureRandom object seeded by the userSeed value (e.g. userSeed = 123456789), you can use the following code:
long userSeed = 123456789;
SecureRandom random = SecureRandom.getInstance("SHA1PRNG","SUN");
random.setSeed(userSeed);
keyGen.initialize(1024, random);

Generating the Pair of Keys

The final step is generating the key pair. The following code is used to generate the key pair:
KeyPair pair = keyGen.generateKeyPair();


Generating and Verifying a Signature

The following signature generation and verification examples use the key pair generated in the key pair example above.

Generating a Signature

We first create a signature object:
Signature rsa = Signature.getInstance("SHA1withRSA");
Next, using the key pair generated in the key pair example, we initialize the object with the private key, and then sign a byte array called data.
/* Initializing the object with a private key */
PrivateKey priv = pair.getPrivate();
rsa.initSign(priv);

/* Update and sign the data */
rsa.update(data);
byte[] sig = rsa.sign();

• Verifying a Signature
Verifying the signature is straightforward. (Note that here we also use the key pair generated in the key pair example.)
/* Initializing the object with the public key */
PublicKey pub = pair.getPublic();
rsa.initVerify(pub);

/* Update and verify the data */
rsa.update(data);
boolean verifies = rsa.verify(sig);
System.out.println("signature verifies: " + verifies);

Implementing RSA by using BigInteger class

BigInteger class
Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types). BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

We can construct a randomly generated positive BigInteger that is probably prime, with the specified bitLength as follows:
BigInteger (int bitLength, int certainty, Random rnd)
bitLength - bitLength of the returned BigInteger.
certainty - a measure of the uncertainty that the caller is willing to tolerate. The probability that the new BigInteger represents a prime number will exceed
(1 - 1/2certainty). The execution time of this constructor is proportional to the value of this parameter.
rnd - source of random bits used to select candidates to be tested for primality.

Generating the Pair of Keys
Generate two distinct large prime numbers p, q:
int SIZE = 512;
p = new BigInteger(SIZE, 10, new Random());
q = new BigInteger(SIZE, 10, new Random());

Calculate their product:
N = p.multiply(q);

Calculate φ(N) = (p-1)(q-1):
PhiN = p.subtract(BigInteger.valueOf(1));
PhiN = PhiN.multiply( q.subtract( BigInteger.valueOf(1)));

Next choose e, coprime to and less than φ(N):
do{
e = new BigInteger(2*SIZE, new Random());
}
while ((e.compareTo(PhiN) != -1) || (e.gcd(PhiN).compareTo( BigInteger.valueOf(1)) != 0));

Compute d, the inverse of e mod PhiN:
d = e.modInverse(PhiN);


Encrypting and Decrypting messages
Encrypt a message using N and e:
ciphertext = message.modPow(e, N);

Decrypt the message using N and d:
plaintext = ciphertext.modPow(d, N);

Saturday, May 14, 2011

About this blog

After long time of programming, I have decided to publish some JAVA/J2EE tips and tricks and some helpful resources  including interview questions relating SPRING,HIBERNATE  for students and developers.Since 1995 I am involve with computer. I bought my first PC in 1996 powered with Intel Pentium II processor alongwith 4GB HDD and 64 mb RAM. I wonder what day that was!!!!.

In this blog I will publish step by step java/j2ee tutorials  with Eclispe and NetBeans IDE.Hope that people may be benefited.....


PLEASE STAY TUNED........