Pages

Sunday, September 15, 2013

Java ArrayList Example

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list with an initial capacity
      ArrayList<Integer> arrlist = new ArrayList<Integer<(8);

      // use add() method to add elements in the list
      arrlist.add(20);
      arrlist.add(25);
      arrlist.add(10);
      arrlist.add(15);        
       
      // let us print all the elements available in list
      for (Integer number : arrlist) {
         System.out.println("Number = " + number);
       }

      // list contains element 10
      boolean retval = arrlist.contains(10); 
   
      if (retval == true) {
        System.out.println("element 10 is contained in the list");
       }
      else {
        System.out.println("element 10 is not contained in the list");
       }
  
      // list does not contain element 30
      boolean retval2 = arrlist.contains(30);
   
      if (retval2 == true) {
        System.out.println("element 30 is contained in the list");
       }
      else {
        System.out.println("element 30 is not contained in the list");    
       }
   }
}
Let us compile and run the above program, this will produce the following result:
Number = 20
Number = 25
Number = 10
Number = 15
element 10 is contained in the list
element 30 is not contained in the list

Friday, September 13, 2013

send e-mail configuration using Access list to Network Services in Oracle Database 11g

•acl - The name of the access control list XML file, generated relative to the "/sys/acls" directory in the XML DB Repository.
•description - A description of the ACL.
•principal - The first user account or role being granted or denied permissions. The text is case sensitive.
•is_grant - TRUE to grant, FALSE to deny the privilege.
•privilege - Use 'connect' for UTL_TCP, UTL_SMTP, UTL_MAIL and UTL_HTTP access. Use 'resolve' for UTL_INADDR name/IP resolution. The text is case sensitive.
•start_date - Default value NULL. When specified, the ACL will only be active on or after the specified date.
•end_date - An optional end date for the ACL.


CONN sys/password@db11g AS SYSDBA

CREATE USER test1 IDENTIFIED BY test1;
GRANT CONNECT TO test1;

CREATE USER test2 IDENTIFIED BY test2;
GRANT CONNECT TO test2;

BEGIN
  DBMS_NETWORK_ACL_ADMIN.create_acl (
    acl          => 'test_acl_file.xml',
    description  => 'A test of the ACL functionality',
    principal    => 'TEST1',
    is_grant     => TRUE,
    privilege    => 'connect',
    start_date   => SYSTIMESTAMP,
    end_date     => NULL);

  COMMIT;
END;
/

BEGIN
  DBMS_NETWORK_ACL_ADMIN.add_privilege (
    acl         => 'test_acl_file.xml',
    principal   => 'TEST2',
    is_grant    => FALSE,
    privilege   => 'connect',
    position    => NULL,
    start_date  => NULL,
    end_date    => NULL);

  COMMIT;
END;
/

BEGIN
  DBMS_NETWORK_ACL_ADMIN.delete_privilege (
    acl         => 'test_acl_file.xml',
    principal   => 'TEST2',
    is_grant    => FALSE,
    privilege   => 'connect');

  COMMIT;
END;
/

BEGIN
  DBMS_NETWORK_ACL_ADMIN.drop_acl (
    acl         => 'test_acl_file.xml');

  COMMIT;
END;
/


•acl - The name of the access control list XML file.
•host - The hostname, domain, IP address or subnet to be assigned. Hostnames are case sensitive, and wildcards are allowed for IP addresses and domains.
•lower_port - Defaults to NULL. Specifies the lower port range for the 'connect' privilege.
•upper_port - Defaults to NULL. If the lower_port is specified, and the upper_port is NULL, it is assumed the upper_port matches the lower_port.

BEGIN
  DBMS_NETWORK_ACL_ADMIN.assign_acl (
    acl         => 'test_acl_file.xml',
    host        => '192.168.2.3',
    lower_port  => 80,
    upper_port  => NULL);

  DBMS_NETWORK_ACL_ADMIN.assign_acl (
    acl         => 'test_acl_file.xml',
    host        => '10.1.10.*',
    lower_port  => NULL,
    upper_port  => NULL);

  COMMIT;
END;
/

Wednesday, September 11, 2013

NetBeans: java.lang.ClassNotFoundException

Find the class that error occurs and do some frontend adjustment. then compile again. apply to netbeans 7.3.

Friday, August 30, 2013

Mysql Upgrade upgrade and bugfix

After upgrading MySQL from version 5.0 to 5.5 I started getting the following message trying to edit table structure in Navicat:

Cannot load from mysql.proc.
The table is probably corrupted Luckily there is an easy fix.

Run the following command from the command prompt (on Windows you might need to run it from MySQL's bin directory).

mysql_upgrade

In some cases you will need specify

mysql root password and --force option (if you run upgrade procedure already).

mysql_upgrade -u root -p --force

Thursday, August 29, 2013

Complete remove Mysql

sudo apt-get remove mysql-server mysql-client mysql-common
sudo apt-get purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
 
locate mysql (get lla location where install mysql and delete) 

Saturday, August 24, 2013

java resultset insert, update and delete

//STEP 1. Import required packages
import java.sql.*;

public class JDBCExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/EMP";

   //  Database credentials
   static final String USER = "username";
   static final String PASS = "password";
   
 public static void main(String[] args) {
   Connection conn = null;
   try{
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //STEP 4: Execute a query to create statment with
      // required arguments for RS example.
      System.out.println("Creating statement...");
      Statement stmt = conn.createStatement(
                           ResultSet.TYPE_SCROLL_INSENSITIVE,
                           ResultSet.CONCUR_UPDATABLE);
     //STEP 5: Execute a query
      String sql = "SELECT id, first, last, age FROM Employees";
      ResultSet rs = stmt.executeQuery(sql);

      System.out.println("List result set for reference....");
      printRs(rs);

      //STEP 6: Loop through result set and add 5 in age
      //Move to BFR postion so while-loop works properly
      rs.beforeFirst();
      //STEP 7: Extract data from result set
      while(rs.next()){
         //Retrieve by column name
         int newAge = rs.getInt("age") + 5;
         rs.updateDouble( "age", newAge );
         rs.updateRow();
      }
      System.out.println("List result set showing new ages...");
      printRs(rs);
      // Insert a record into the table.
      //Move to insert row and add column data with updateXXX()
      System.out.println("Inserting a new record...");
      rs.moveToInsertRow();
      rs.updateInt("id",104);
      rs.updateString("first","John");
      rs.updateString("last","Paul");
      rs.updateInt("age",40);
      //Commit row
      rs.insertRow();

      System.out.println("List result set showing new set...");
      printRs(rs);
      
      // Delete second record from the table.
      // Set position to second record first
      rs.absolute( 2 );
      System.out.println("List the record before deleting...");
      //Retrieve by column name
      int id  = rs.getInt("id");
      int age = rs.getInt("age");
      String first = rs.getString("first");
      String last = rs.getString("last");

      //Display values
      System.out.print("ID: " + id);
      System.out.print(", Age: " + age);
      System.out.print(", First: " + first);
      System.out.println(", Last: " + last);

     //Delete row
      rs.deleteRow();
      System.out.println("List result set after \
                                 deleting one records...");
      printRs(rs);

      //STEP 8: Clean-up environment
      rs.close();
      stmt.close();
      conn.close();
   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
   System.out.println("Goodbye!");
}//end main

   public static void printRs(ResultSet rs) throws SQLException{
      //Ensure we start with first row
      rs.beforeFirst();
      while(rs.next()){
         //Retrieve by column name
         int id  = rs.getInt("id");
         int age = rs.getInt("age");
         String first = rs.getString("first");
         String last = rs.getString("last");

         //Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);
     }
     System.out.println();
   }//end printRs()
}//end JDBCExample

Monday, July 1, 2013

Alternative colour for java jtable

jTblRefe = new javax.swing.JTable(){public Component prepareRenderer
    (TableCellRenderer renderer,int Index_row, int Index_col)
    {
        Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
        //even index, selected or not selected

        if (Index_row % 2 == 0 ) {
            comp.setBackground(new java.awt.Color(230,212,212));
        }
        else
        {
            comp.setBackground(Color.white);
        }

        return comp;
    }

    public boolean isCellEditable(int row,int column){
        return false;
    };

}
;

Check java jar application is already running.

ServerSocket ss;
try {
    ss = new ServerSocket(MY_PORT);
    // not already running.
} catch (BindException e) {
    // already running.
}
 
Steps.
 
1) open listning port.
2) check listning port alredy runnig. 

Good luck.

Thursday, May 30, 2013

java textbox + jformattedtextfield format


MaskFormatter formatter = new MaskFormatter("*****");
       jtxtSellingPrice.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(formatter));
       formatter.setValidCharacters("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ $#@!&*()");


Character 
Description
# Any valid number, uses Character.isDigit.
' Escape character, used to escape any of the special formatting characters.
UAny character (Character.isLetter). All lowercase letters are mapped to upper case.
LAny character (Character.isLetter). All upper case letters are mapped to lower case.
AAny character or number (Character.isLetter or Character.isDigit)
?Any character (Character.isLetter).
*Anything.
HAny hex character (0-9, a-f or A-F).

Friday, May 24, 2013

How to search data in java jtable

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class Main {
  public static void main(String[] args) {
    Object[][] data = { { "A"}"B"}"C"}"D"} };
    String columnNames[] "Item""Value" };
    TableModel model = new DefaultTableModel(data, columnNames) {
      public Class getColumnClass(int column) {
        return getValueAt(0, column).getClass();
      }
    };
    JTable table = new JTable(model);

    RowFilternew RowFilter() {
      public boolean include(Entry entry) {
        Integer population = (Integerentry.getValue(1);
        return population.intValue() 3;
      }
    };

    TableRowSorter sorter = new TableRowSorter(model);
    sorter.setRowFilter(filter);
    table.setRowSorter(sorter);
    JScrollPane scrollPane = new JScrollPane(table);
    JFrame frame = new JFrame("Filtering Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane);
    frame.setSize(300200);
    frame.setVisible(true);
  }
}



refer http://www.java2s.com/Code/JavaAPI/javax.swing.table/DefaultRowSortersetRowFilterRowFiltersuperTableModelsuperIntegerfilter.htm 

 mycode

 TableRowSorter sorter = new TableRowSorter(dtm);
         
 RowFilter filter = RowFilter.regexFilter(jtxtSearch.getText().toString());
         
          sorter.setRowFilter(filter);
         
          jtblData.setRowSorter(sorter);

Tuesday, February 12, 2013

Import data into Oracle8i or higher release database. import export compatibility matrix Which Export client to use when importing into an Oracle8i 8.1.7
           or a higher release target database
          (always use the Import client of the target database):

+-----------+--------------------------------------------------------------+
|  EXPORT   |                       IMPORT into:                           |
|   from    +--------+--------+--------+--------+--------+-----------------+
|    \/              8.1.7    |9.2.0     |10.1.0     |10.2.0    |11.1.0    |11.2.0   |        |
+-----------+--------+--------+--------+--------+--------+--------+--------+
|  5.x  1)2) | EXP5x   | EXP5x   | EXP5x   | EXP5x   | EXP5x   | EXP5x  |        |
|  6.x    2)  | EXP6x   | EXP6x   | EXP6x   | EXP6x   | EXP6x   | EXP6x  |        |
|  7.x    3)  | EXP7x   | EXP7x   | EXP7x   | EXP7x   | EXP7x   | EXP7x  |        |
+-----------+--------+--------+--------+--------+--------+--------+--------+
|  8.0.3      | EXP803 | EXP803 | EXP803 | EXP803 | EXP803 | EXP803 |        |
|  8.0.4      | EXP804 | EXP804 | EXP804 | EXP804 | EXP804 | EXP804 |        |
|  8.0.5      | EXP805 | EXP805 | EXP805 | EXP805 | EXP805 | EXP805 |        |
|  8.0.6      | EXP806 | EXP806 | EXP806 | EXP806 | EXP806 | EXP806 |        |
+-----------+--------+--------+--------+--------+--------+--------+--------+
|  8.1.5      | EXP815 | EXP815 | EXP815 | EXP815 | EXP815 | EXP815 |        |
|  8.1.6      | EXP816 | EXP816 | EXP816 | EXP816 | EXP816 | EXP816 |        |
|  8.1.7      | EXP817 | EXP817 | EXP817 | EXP817 | EXP817 | EXP817 |        |
+-----------+--------+--------+--------+--------+--------+--------+--------+
|  9.0.1      | EXP817 | EXP901 | EXP901 | EXP901 | EXP901 | EXP901 |        |
|  9.2.0      | EXP817 | EXP920 | EXP920 | EXP920 | EXP920 | EXP920 |        |
+-----------+--------+--------+--------+--------+--------+--------+--------+
| 10.1.0  4)| EXP817 | EXP920 |               
| 10.2.0  4)|   N/S      | EXP920 |                
+-----------+--------+--------+--------+--------+--------+--------+--------+
| 11.1.0  4)|   N/S      | EXP920*|       
| 11.2.0  4)|   N/S      | EXP920*|      
+-----------+--------+--------+--------+--------+--------+--------+--------+

Remarks:1) Import can read export dump files created by EXPORT release 5.1.22 and higher (up to same version).
2) An Oracle5 or Oracle6 export dump and an Oracle8 or Oracle8i or Oracle9i Import: see the Oracle Utilities Manual, Chapter 2 "Import" for special considerations to keep in mind.
3) An Oracle7 export dump and an Oracle8 or Oracle8i or Oracle9i Import: check constraints on the DATE columns become invalid when TO_DATE function has not been used in the constraint (this was not required in earlier Oracle versions).
4) To have a better performance and more flexibility when exporting from and importing into Oracle10g and higher release databases, we recommend the usage of the Export Data Pump (expdp) and Import Data Pump (impdp) clients.
5) The original Export client is desupported for general use as of Oracle Database 11g.