Skip to main content

Posts

Showing posts from May, 2010

Generating JTable from database table

package  process; import  javax.swing. * ; import  javax.swing.table. * ; import  java.sql. * ; /**  * This class create JTable from Database table.  * User program needs to specify database connection and corresponding atable name.  * @author Hemraj  */ public   class  TableToJTable {      //private String table;      private   Connection  con;      public   TableToJTable ( Connection  con ){          this .con = con;      }      /**      * This method return JTable object created from Database table having same data asn structure      * as in original table into database.      * @param table Name of the database table to be coverted to JTable      * @return JTable object that consist of data and structure of Database table      * @throws java.lang.Exception Original object is deferent, e.i either SQLException or NullPointerException      */      public   JTable   getTable ( String  table ) throws   Exception {          JTable  t1 = new   JTable () ;          D

Handling Transaction JDBC

Transactions   In various application we may group the series of statements in such a way that either all of them execute successfully or all to be failed. For example if we want to execute five SQL queries at a time, which are related to each other, like ü     Search the particular data ü      Again search for next from second row ü       add the data retrieved ü       Insert into third row ü       Update the row To do this in the single steps, we need to do the transaction in the given ordered. When multiple statements are executed in a single transaction, all operations can be committed (made permanent in database) or rollback (that is changes to the database are undone). When a new connection object is created, it is set to commit every time transaction automatically .so we can not rollback, to solve this problem we use the transaction scheme. There are the following methods of connection interface which are used for transactions.  boolean   getAutoCommit () Retrieves the curre

Servlet configuration

Inside servlet container, the object of  javax.servlet. ServletConfig is used to represents the servlet configurations. The configuration information conatains the servlet ‘s initialization parameters, the name of the servlet and servlet contaxt which gives the information about container. The servletinitialization parameter and the servlet name can be spacified in the deployment descriptor (web.xml). For example < web-app >      < servlet >          < servlet-name > s1 </ servlet-name >          < servlet-class > FirstServlet </ servlet-class >          < init-param >              < param-name > p1 </ param-name >              < param-value > bye user </ param-value >          </ init-param >      </ servlet >      < servlet-mapping >          < servlet-name > s1 </ servlet-name >               < url-pattern > /s1 </ url-pattern >      </ servlet-mapping > <

Thread synchronization

TestSynchronized.java // Program to Test Synchronized class MyClass { void myMethod() { System.out.println( "Callin method from MyClass " ); } } class CommonClass { synchronized public void print(String msg) { System.out.println( "Hi" ); try { Thread.sleep(1000); } catch (Exception e) { } System.out.println(msg); } } class Syn1 extends Thread { CommonClass cc = null; public void run() { cc.print( "From Syn1" ); } } class Syn2 extends Thread { CommonClass cc2 = null; public void run() { cc2.print( "From Syn2" ); } } class TestSynchronized { public static void main(String ar[]) { //creating the object of Synchronized method`s class CommonClass c = new CommonClass();

Find and count files of specified name or extension

import java.io.*; class CountFileFolder { static String searchFile= ".mp3" ; static int count=0; public static void main(String ar[]) throws IOException { long s=System.currentTimeMillis(); search( "F:/" ); long e=System.currentTimeMillis(); long total=e-s; double d=total/1000; System.out.println( "Time consumed = " +d+ " sec" ); System.out.println( "Found = " +count+ " files" ); } public static void search(String dir) throws IOException { File fil = new File(dir); File flist[]= fil.listFiles(); for (File s:flist) { //System.out.println(s); if (s.getName().contains(searchFile)){ System.out.println( "File found at = " +s.getAbsolutePath()); count++; } else if (s.isDirectory()){

Using JPQL in JPA

This example is using JPQL and executing select query to get multiple result from table. Here Employee is the name of entity class not a table name in database and emp is a alias for Employee entity. Query object is used to represent a query and it has getResultList method that returns list of multiple entities for each row fetched from database using JPA. import  java.util. * ; import  javax.persistence. * ; public   class  JavaApp2 {      public   static   void   main ( String  ar []){                  EntityManagerFactory emf = Persistence. createEntityManagerFactory ( "persistunit" ) ;         EntityManager em = emf. createEntityManager () ;         Query q = em. createQuery ( "SELECT OBJECT(emp) FROM Employee emp" ) ;          List < entities.Employee >  rl = q. getResultList () ;          for ( entities.Employee e:rl ){              System .out. println ( "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - "

JApplet

Like awt’s Applet swing also provides the class JApplet to create the applet that may contains the swing components. JApplet has some enhanced features. The JApplet class is the subclass of Applet. JApplet support for adding menus and menu items. Default layout out of JApplet is border layout while in Applet default layout is FlowLayout. Painting in JApplet is not as like Applet class. Paint code can not be placed directly to paint something. // // An example of the JApplet class.  For use with the applet.html file. // import  javax.swing. * ; import  javax.swing.border. * ; import  java.awt. * ; /*  *<applet code="JAppletDemo" width=300 height=250> </applet>  */ public   class  JAppletDemo  extends   JApplet  {    public   void  init() {      JPanel  p  =   new   JPanel ();     p.setBorder( BorderFactory .createTitledBorder( "Login" ));     p.setBackground( new   Color (0xCCFFCC));     p.setLayout( new   GridLayout (2, 2, 

Swing : Menu

JMenu is the standard menu that may have various JMenuItems. JMunu are placed into JMenuBar. and JMenuBar is added to the JFrame using setMenuBar method. you can add ActionListener to JMenuItem to deal with events.  import  javax.swing. * ; import  java.awt. * ; import  java.awt.event. * ; class  MenuDemo {      public   static   void  main( String  ar[])     {          JFrame  frame = new   JFrame ( "Menus" );                   JMenu  file = new   JMenu ( "File" );         file.setMnemonic( KeyEvent .VK_F);                            JMenu  edit = new   JMenu ( "Edit" );          JMenu  view = new   JMenu ( "View" );          JMenu  help = new   JMenu ( "Help" );                  file.add( new   JMenuItem ( "Open" ));         file.add( new   JMenuItem ( "New" ));         file.addSeparator(  );          //add separator         file.add( new   JMenuItem ( "Save" ));         file.add( n