Books Related to Java Technology

Friday, January 23, 2009

Difference Between jre jvm and jdk


JDK (Java Development Kit)

JDK= JDK tools + JRE

JRE = JVM + Java Packages Classes(like util, math, lang, awt,swing etc)+runtime libraries.

JDK contains tools required to develop the Java programs, and JRE to run the programs. The tools include compiler (javac.exe), Java application launcher (java.exe), Appletviewer, etc…

JAVA Compiler converts java code into byte code. and java application launcher opens a JRE, loads the class, and invokes its main method.

If you want to write and compile your own programs, you need JDK. If you just want to run your java programs, JRE is sufficient. JRE is targeted for execution of Java files

You can create a Java file (with the help of Java packages), compile a Java file and run a java file. JDK is mainly targeted for java development.


JRE (Java Runtime Environment)
Java Runtime Environment contains JVM, class libraries, and other supporting files. It does not contain any development tools such as compiler, debugger, etc. Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE. If you want to run any java program, you need to have JRE installed in the system.


JVM(Java Virtual Machine)
The Java Virtual Machine provides a platform-independent way of executing code, programmers can concentrate on writing application, without having to be concerned with how or where it will run.

JDK Tools and Utilities

Basic Tools:
  • javac
  • java
  • javadoc
  • apt
  • appletviewer
  • jar
  • jdb
  • javah
  • javap
  • extcheck
Security Tools:
  • keytool
  • jarsigner
  • policytool
  • kinit
  • klist
  • ktab

Internationalization Tools:
  • native2ascii
Remote Method Invocation (RMI) Tools:
  • rmic
  • rmiregistry
  • rmid
  • serialver

Java IDL and RMI-IIOP Tools:
  • tnameserv
  • idlj
  • orbd
  • servertool

Java Deployment Tools:
  • pack200
  • unpack200

Java Plug-in Tools:
  • htmlconverter

Java Web Start Tools:
  • javaws



For more details you can visit:
http://www.worldinfosoft.com

This is a one of the best tutorial site, where you can find lots of example with full description of code and you can also prepare of your interview from this site WORLDINFOSOFT.COM.

Saturday, January 17, 2009

Insert & Retrieve Image using Servlet

This article show to you how to insert blob type image into mysql database table by using servlet and how to retrieve the image from table and display it on the browser. In this example servlet is used because it dynamically process the request and generate a response for java programming language. The API of the java servlet allow to software developer to add dynamic content to a web server using java. The main advantage of java servlet is, it can be executed to handle request and response objects and it can run independently.

Now, you follow these steps to insert and retrieve image from database table:

1. Create table in your database follow this query:


CREATE TABLE `picture` (
`id` int(10) NOT NULL auto_increment,
`image` blob,
PRIMARY KEY (`id`)
)

2. create a servlet to insert the image:



import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class InsertImage extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
PrintWriter pw = response.getWriter();
String connectionURL = "jdbc:mysql://localhost:3306/test";
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(connectionURL, "root", "");
PreparedStatement ps = con.prepareStatement("INSERT INTO pictures VALUES(?,?)");
File file = new File("C:/images/5.jpg");
FileInputStream fs = new FileInputStream(file);
ps.setInt(1,1);
ps.setBinaryStream(2,fs,fs.available());
int i = ps.executeUpdate();
if(i!=0){
pw.println("image inserted successfully");
}else{
pw.println("problem in image insertion");
}
} catch (Exception e){
System.out.println(e);
}
}
}


3. create web.xml file in WEB-INF folder of your tomcat server and put this code:


<servlet>
<servlet-name>InsertImage</servlet-name>
<servlet-class>InsertImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InsertImage</servlet-name>
<url-pattern>/InsertImage</url-pattern>
</servlet-mapping>



4. create one more servlet for retrieve image from database table






import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RetreiveImage extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//PrintWriter pw = response.getWriter();
String connectionURL = "jdbc:mysql://localhost:3306/test";
java.sql.Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(connectionURL,"root","");
Statement st1=con.createStatement();
ResultSet rs1 = st1.executeQuery("select image from pictures where id='1'");
String imgLen="";
if(rs1.next()){
imgLen = rs1.getString(1);
System.out.println(imgLen.length());
}
rs1 = st1.executeQuery("select image from pictures where id='1'");
if(rs1.next()){
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs1.getBinaryStream(1);
int index=readImg.read(rb, 0, len);
System.out.println("index"+index);
st1.close();
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
} catch (Exception e){
e.printStackTrace();
}
}
}


5. put this code in to your web.xml file:



<servlet>
<servlet-name>RetreiveImage</servlet-name>
<servlet-class>RetreiveImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RetreiveImage</servlet-name>
<url-pattern>/RetreiveImage</url-pattern>
</servlet-mapping>



6. Compile both servlet file into classes folder by command prompt as:

E:\apache-tomcat-6.0.14\webapps\sandeep\WEB-INF\classes> javac InsertImage.java

and

E:\apache-tomcat-6.0.14\webapps\sandeep\WEB-INF\classes> javac RetreiveImage.java


7. Run your server by startup.bat file


8. Open your browser and type on url address:

http:\\localhost:8080\sandeep\InsertImage

then you will got a success message

9. and then run the RetreiveImage servlet to display your image on browser by

http:\\localhost:8080\sandeep\RetreiveImage

10. Finally you will see the image on your browser


For more details you can visit: http://www.worldinfosoft.com

This is a one of the best tutorial site, where you can find lots of example with full description of code and you van also prepare of your interview from this site WORLDINFOSOFT.COM.

Monday, January 5, 2009

JDBC Insert Query Example




import java.io.*;
import java.sql.* ;

class JDBCInsert{
public static void main( String args[] ){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName","root", "root") ;
Statement stmt = conn.createStatement() ;
int rows = stmt.executeUpdate("INSERT INTO tableName VALUES (4, 'sandeep')");
System.out.println(rows + " Rows modified") ;
stmt.close() ;
conn.close() ;
} catch(SQLException se) {
System.out.println("SQL Exception:"+se) ;
} catch(Exception e){
System.out.println(e) ;
}
}
}

JDBC Update Query Example



import java.io.*;
import java.sql.* ;

class JDBCUpdate{
public static void main( String args[] ){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName","root", "root") ;
Statement stmt = conn.createStatement() ;
int rows = stmt.executeUpdate("UPDATE tableName SET columnName='' WHERE columnName=''") ;
System.out.println(rows + " Rows modified") ;
stmt.close() ;
conn.close() ;
} catch(SQLException se) {
System.out.println("SQL Exception:"+se) ;
} catch(Exception e){
System.out.println(e) ;
}
}
}

Sunday, January 4, 2009

JDBC Prepared Statement Example


import java.io.*;
import java.sql.*;
public class JDBCPreparedStatement {
public static void main ( String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
PreparedStatement stmt = c.prepareStatement("select * from tableName");
ResultSet rs = stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1) + " (" + rs.getString(2) + ")");
}
} catch(SQLException e){
System.err.println(e);
}
}
}

MySQL Create Database

  • In this example we are creating a databse table for establishing the connection with MySQL database by using the JDBC driver. A database is a large collection of data or information stored in our computer in a specified manner. It helps us for accessing, managing and updating the data easily. A RDBMS (Relational Database Management System) is a type of DBMS (Database Management System) which stores the data in the form of tables. So, we can view and use the same database in many different ways.

The following example Shows to you how to create a database in MYSQL:


import java.io.*;
import java.sql.*;

public class CreateDatabase{
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbname = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection (url+dbname,user,pass);
try{
Statement st = con.createStatement();
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Database name:");
String database = bf.readLine();
st.executeUpdate("CREATE DATABASE "+database);
System.out.println("Dtabase Created Successfully");
} catch (SQLException s){ System.out.println("SQL Exception!");
}
} catch (Exception e){ e.printStackTrace();
}
}
}