Silicon Magic

Java J2ME JSP J2EE Servlet Android

Oracle Basic: default value to a column

There are three ways in oracle of how you can specify default value to a column in oracle. That means if you insert null value to that column it will take the default value.
First, at time time of creation of the table specify the default value of the column.
Second, add a new column and specify its default value by ALTER TABLE ... ADD statement.
Third, modify the column by specifying dafault vlue by ALTER TABLE ... MODIFY statement.

1. At table creation
====================

SQL>CREATE TABLE test (name VARCHAR2(10), score NUMBER DEFAULT 0);

CREATE TABLE succeeded.

SQL>INSERT INTO test(name,score) VALUES('John','12');

1 rows inserted

SQL>INSERT INTO test(name) VALUES('Rina');

1 rows inserted

SQL>SELECT * FROM test;

NAME SCORE
---------- ----------------------
John 12
Rina 0

2 rows selected

2. At add new column to table
=============================

SQL>ALTER TABLE test ADD min_score NUMBER DEFAULT 0;

ALTER TABLE test succeeded.

SQL>INSERT INTO test(name,score) VALUES('Nisha',11);

1 rows inserted

SQL>SELECT * FROM test;

NAME SCORE MIN_SCORE
---------- ---------------------- ----------------------
John 12 0
Rina 0 0
Nisha 11 0

3 rows selected

3. At modify an existing column
===============================

SQL>ALTER TABLE test ADD max_score NUMBER;

ALTER TABLE test succeeded.

SQL>ALTER TABLE test MODIFY max_score DEFAULT 100;

ALTER TABLE test succeeded.

SQL>INSERT INTO test(name) VALUES('Asad');

1 rows inserted

SQL>SELECT * FROM test;

NAME SCORE MIN_SCORE MAX_SCORE
---------- ---------------------- ---------------------- ----------------------
John 12 0
Rina 0 0
Nisha 11 0
Asad 0 0 100

4 rows selected

Cook PHP: Finding the numbers of occurrence of a string in another string..

Lets consider a String

$str = "PHP is what PHP is and PHP is not what PHP is not";

Now lets try to find out the no of occurrence of the word 'PHP' in above string..


$cnt = count(explode("PHP",$str))-1;
echo $cnt;


This will print 4.

There are many alternative option to do the same this in php..

ORA-14552: cannot perform a DDL, commit or rollback inside a query or DML

Cause:
=====
DML operations like create, drop etc. and rollback/commit can not be performed in a SQL statement

Example:
=======
The most general case where this error occurs is calling a function as
SELECT myFunc(.. .. ..) FROM dual..
Something like this or the like, Where myFunc contains DML operations, rollback or commit statements.

Solution:
==========
You have to ensure that DML operations and rollback/commit operations must be autonomous instead of inside a query.

For the case described above you can execute the function myFunc as
var:=myFunc(.. .. ..);

Or redefine the function as
CREATE OR REPLACE function myFunc(.. .. ..) RETURN ..
AS
....
....
PRAGMA AUTONOMOUS_TRANSACTION
begin
.....
.....
.....
end;

Java By Example: Java Calendar day, month, week, year, hour getting, java.util.Calendar

Java provides a strong way to work with date and calendar. Here is an example of java calendar to get values of --


* Month

* Year

* Week of year

* Week of month

* Day of month

* Hour

etc...


In this way we can also get some other information from calendar, like

* day of year

* Hour of day

* Minutes

* Seconds

* Time Zone

etc ..




import java.util.Calendar;


publicclass JavaCalendar {

publicstaticvoid main(String args[]){

Calendar calendar = Calendar.getInstance();


/* Setting calendar for current time */

calendar.setTimeInMillis(System.currentTimeMillis());


/* Get calendar month */

System.out.println(calendar.get(Calendar.MONTH)+1);


/* Get calendar year */

System.out.println(calendar.get(Calendar.YEAR


/* Get calendar week of year */

System.out.println(calendar.get(Calendar.WEEK_OF_YEAR


/* Get calendar week of month */

System.out.println(calendar.get(Calendar.WEEK_OF_MONTH


/* Get calendar day of week */

System.out.println(calendar.get(Calendar.DAY_OF_WEEK


/* Get calendar day of the month */

System.out.println(calendar.get(Calendar.DAY_OF_MONTH


/* Get calendar Hour */

System.out.println(calendar.get(Calendar.HOUR


}

}

Java By Example: Java Menue Example [ javax.swing.JMenu ]

Here is a very basic example of Java Menu[JMenue]


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;



public class Menu extends JFrame{
public Menu()
{
super("Menu example");

JMenu file = new JMenu("File");
file.setMnemonic(’F’);
JMenuItem newItem = new JMenuItem("New");
newItem.setMnemonic(’N’);
file.add(newItem);
JMenuItem openItem = new JMenuItem("Open");
openItem.setMnemonic(’O’);
file.add(openItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic(’x’);
file.add(exitItem);

//adding action listener to menu items
newItem.addActionListener(
new ActionListener() {

public void actionPerformed(ActionEvent e)
{
System.out.println("New is pressed");
}
}
);
openItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Open is pressed");
}
}
);
exitItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Exit is pressed");
}
}
);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(file);

getContentPane();
setSize(400, 400);
setVisible(true);
}

public static void main(String[] args)
{
Menu app = new Menu();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}