Java J2ee Tutorials

What is Difference Between Statement and PreparedStatement

Tuesday, February 14, 2012

There are four steps for the execution of query such as
  1. Query is parsed,
  2. Query is compile,
  3. Query is optimized
  4. Query is executed
In case of statement interface these four steps are performed, each time when query is submitted for execution. But in case of prepared statement first three steps are performed only once, when the query in initially submitted. Only the last step is performed each time query is submitted (in subsequence submissions), i.e if same query is submitted to be execute with different values multiple times then prepared statement interface provides better perfromance as compared to statement interface.

Free Career Predictions

mysql-connector.jar for Connecting Java to MySQL

Tuesday, February 7, 2012

This blog helps one how to connect java to mysql database, there are some steps to follow at first check for the java if java is not installed download latest jdk from sun java, This is where you can get latest java development kit http://java.sun.com/javase/downloads/index.jsp be sure that you installed java, you can check that via command prompt or terminal use this command without quote “java -version” so you must see like this java version “1.6.0_16″, Basically java has no jar file or pre built class for mysql connection which is called mysql-connector i mean driver class which supports for mysql connection so its better to download package called mysql connector so this plays a major role during connection so download that from a mysql web site http://www.mysql.com/products/connector/ they given more version look for latest version and download ans place it in a know place say(c:\mysql or /home/mysql) but don’t forget to unzip mysql-connector and i believe that mysql-server service already started in system.

This is program check the connectivity of mysql-server through localhost

import java.sql.*;

public class DatabaseConnectionExample {
public static void main(String args[]) {
System.out.println("Database Connection Example");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
//String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
try {
Class.forName(driver).newInstance();
//con = DriverManager.getConnection(url + dbName,userName,password);
con = DriverManager.getConnection(url,userName,password);
System.out.println("Database Connected");
con.close();
System.out.println("Connection Colsed.");
}
catch (Exception e){
System.out.println("Error: "+e);
}
}
}

Save as DatabaseConnectionExample.java, while compile it you have to include classpath of java to know more about classpath sun java is best guide http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/classpath.html

Now it time to compile open command prompt go to the location of DatabaseConnectionExample.java if your your program is under c:\

use c: also may be at /home for tux users now compile it using javac -classpath /home/mysql/mysql-connector.bin.jar DatabaseConnectionExample.java so this command has three part javac, -calsspath, filepath, here javac is a compiler and we included a classpath of mysql-connector jar file locate that wherever it is for an example i given as /home/mysql/mysql-connector.bin.jar and next to that is java file. after compilation program needs to run so java interpreter shall do this job if this command is used java -classpath /home/mysql/mysql-connector.bin.jar: DatabaseConnectionExample. in my second I used colon so its specific for linux user unless windows use ;(semi-colon)

For Eclipse users, use can import mysql-connector.jar file to classpath before creating a class file, right click the project go to properties and choose java build path and then choose libraries tab and add external jar file.

Program shows this output
Database Connected.
Connection Closed.

Free Career Predictions

Creating a Database Table

Monday, February 6, 2012

package com;

import java.sql.*;

public class CreateTableExample {
public static void main(String args[]){
System.out.println("Creation Table Example");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test_db";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("Database Connected");
try {
Statement stmt = con.createStatement();
String table = "CREATE TABLE EMPLOYEE(emp_id integer, emp_name varchar(50) )";
stmt.executeUpdate(table);
System.out.println("Table created successfully");
}
catch (Exception e){
System.out.println("Sorry!! Table already exists.");
}
con.close();
System.out.println("Connection Closed");
}
catch (Exception e){
System.out.println("Error: " +e);
e.printStackTrace();
}
}
}
========
Out Put

Creation Table Example
Database Connected
Table created successfully
Connection Closed

Free Career Predictions