Wednesday, 26 June 2013

establishing connection b/w netbeans and server

For Windows 64 bit
Copy the path of sqljdbc4.jar i.e. C:\Program Files\Microsoft JDBC Driver 4.0 for Server\sqljdbc_4.0\enu\ sqljdbc4.jar
Copy C:\Program Files\Microsoft JDBC Driver 4.0 for Server\sqljdbc_4.0\enu\ au\(sth).jar to bin folder in java and in system32.
Stop SQL 2005 Services and then:
Enable TCP/IP and then copy no. in dynamic port from properties of TCP/IP

Establishing SQL connection in Windows 32 bit
Control Panel à Make the icons small à choose “Administrator Tools” à choose ODBC DataSource à Choose SystemDSN tab à click Add àMake SQL Native Client with:
Name – demo (name of the database already created by user)
Server name – (name of the server)
(For server name:
            Open Microsoft SQL Server Management Studio Express
            Under File menu, click “Connect Object Explorer…” and copy the server name.)
Then, add login name i.e. “sa” and password (used during installation of server)
Click Next
Change default database à Choose database already created by user à next à Finish à OK à OK
Now, make a file in notepad:
And run this in cmd.

And we are DONE!!!!!!!


Java Frame for employee database

Need to import sql.* to use sql database by:
import java.sql.*;
     
Class.forName is used for loading the jdbc(or java) drivers.
DriverManager class is used for loading the odbc drivers. It returns the value to Connection con.
Connection con = DriverManager.getConnection("jdbc:odbc:demo","sa","qwerty"); //method with argument and returning object.
PreparedStatement class is used for inserting, updating and deleting the data. It has 2 methods (actually many methods):
1.      executeUpdate()
2.      executeQuery()

Monday, 24 June 2013

Working with jFrame and database

Main components of JFrame in Netbeans:
1.      Main window
2.      Frame window
3.      Project Explorer
4.      Pallette
5.      Output window
New project à New JFrame (on right click)
Ques.
Create form with the following fields:
Employee Name
Emp id
Salary
HRA (20%)
DA (30%)
CCA (10%)
Gross
Tax
Net
Buttons = Submit and Reset
Ques.
Create form with following fields:
College name
College id
Stream
Category
Fee
Buttons = Submit and Reset

Making frames in cmd
Constructors used:
1.      frame();
2.      frame(String str);
Menu Frame
Right Arrow: Uses
Database
RDBMS (Relational Database Management System) is a collection of related databases.
Database is a collection of tables.
Table is collection of records.
Records are collection of fields.
Fields are values of attributes.
Attributes are characteristics of an entity. e.g. Name is an attribute of a person.
Benefits of Database
It helps in:
1.      Data Storage
2.      Data Management
3.      Decision making
Operations on database:
1.      Add data
2.      Delete
3.      Modify
4.      Select
Commands in Database
DDL (Data Definition Language) commands are used to create table, modify table and drop table. With the help of these commands, e can change the structure of a table.
Commands used:
·         Create
·         Alter
·         Drop
Format for create:                   create table table_name(fieldname datatype, fieldname datatype);
e.g.                                          create table Student(name varchar, rollno int)
Format for alter:                      alter table table_name add coloumn column_name datatype
e.g.                                          alter table table_name add subject varchar(30)

Database demo
create database demo

create table student (sname varchar(30), rollno int)
sp_help student
alter table student add subject varchar(30)
alter table student drop column subject
DML (Data Manipulation Language) commands.
Commands used:
·         Insert
·         Update
·         Delete
Format for insert:                  insert into table_name(fieldname, fieldname) values(value, value)
Format for update:               update table_name set fieldname=value, fieldname=value where fielname=value
Format for delete:                 delete from table_name where fieldname=value

Database demo
create database demo
create table student (sname varchar(30), rollno int)
sp_help student
alter table student add subject varchar(30)
alter table student drop column subject
insert into student values(‘pec’,102,‘math’)
select * from student
update student set sname=‘pgi’,subject=’science’ where rollno=101
delete from student where rollno=101
Ques.
Create a table “Employee” with employee name, id, salary, bonus, address.
Ans.
create table employee(name varchar(30),id int primary key,salary int,bonus float,address varchar(30))
insert into employee values('A', 1, 15000,1000, 'India')
insert into employee values('B', 2, 15000,1000, 'USA')
insert into employee values('C', 3, 15000,1000, 'England')
insert into employee values('D', 4, 15000,1000, 'Canada')
insert into employee values('E', 5, 15000,1000, 'New York')
insert into employee values('F', 6, 15000,1000, 'Denmark')

select * from employee

update employee set name='spic', id=106, salary=1000, bonus=2000, address='Chd' where id=5

Tuesday, 18 June 2013

Applets

Applet
Applet is a small program which executes on browser or Java supported browser. There are two types of applets: Local and Remote.
1.      Local Applets are those which execute on local machine. There is no need of internet for local applet.
2.      Remote applets are those which exist on another or remote machine. We can download and execute them on local machine.
Following is the life cycle of an applet:
1.      Initialization (used to initialize methods, variables, etc.)
2.      Start (execution starts from this method)
3.      Paint (used to display output)
4.      Stop (used to stop execution)
5.      Destroy (used to de-allocate memory)
Format:
public void init()
{}
public void start()
{}
public void paint()
{}
public void stop()
{}
public void destroy()
{}
Compilation and Execution
Compilation à javac filename.java
Execution à appletviewer filename.java
Note: The class-name and file-name are same in case of applets.
e.g. (basic)

Note:
·         Browser only understands markup language. That’s why, we write the tags in comments section.
·         No need of main() in applets.
e.g. (using some methods)


Color codes:
Red = 255,0,0
Green = 0,255,0
Blue = 0,0,255
White = 0,0,0
Black = 255,255,255
e.g. (experimenting with colors)

Note:
Class Graphics and Color lie in awt.
e.g. (drawing shapes)

Controls in awt:
·         Label – static
·         Button
·         Text Field
·         Checkbox
·         Radio-button
·         List-box
·         Combo-box
·         Scrollbar
·         Text Area
Steps to use these:
1.      Initialize the class
Format: class_name object_name = new class_name();
2.      Add into the component class
Format: add(object_name)
Label is static control and has 3 constructors:
1.      Label();
2.      Label(String str);
3.      Label(String str, Allignment);
Button is a class used for event purpose. It has 2 constructors.
e.g. (adding Buttons)

Text Field is a class used for input purpose. It has 3 constructors:
1.      Default constructor
TextField();
2.      TextField(10);
3.      TextField(String str);

e.g. (including Label, text, button)

Checkbox is a class that provides true and false value. It has 3 constructors:
1.      Checkbox();
2.      Checkbox(String str);
3.      Checkbox(String str, Boolean);

Radio button
Format:
CheckboxGroup cbg = new ChackboxGroup();
Checkbox ch = new Checkbox(String str, cbg, boolean);

List is a group of items through which we can chose one or more than one items. It has 3 constructors:
1.      Default:                       List();
2.      Parametrized:              List(int num);
3.      List(int num, Boolean);

Combo-box – This is similar to List. This allows only one choice while list may allow more than one option.
Format:
Choice ch = new Choice();
To add:
ch.add();
ch.addItem();

Text Area is a combination of rows and columns. It has 3 constructors:
1.      TextArea();
2.      TextArea(int row, int column);
3.      TextArea(String str);
Event Delegation Model
Event is a change in state of an object. Change necessarily requires a source. Events are performed on static objects.
Requirements:
1.      Event class
2.      Sources
3.      Listener
Required Package for event àjava.awt.event.*
Required Interface à ActionListener (used with button)
3 necessary steps:
1.      Initialize the object
2.      Add into the component
3.      Register the objects
Format:            object_name.addInterfaceName(CurrentObject);
e.g.                  b1.addActionListener(this);
The ActionEvent class provides the method getActionCommand(). This returns 2 kinds of values – String.
e.g. (Registration of object)

e.g. (performing action)

Ques. WAP to display:
1.      First number label and textbox
2.      Second number label and textbox
3.      Result label and textbox
4.      Buttons – add, subtract, multiply, divide
(Basic calculator program)


Class               Interface                     Event Class                Method
Button             ActionListener                        ActionEvent                actionPerformed(ActionEvent ae)
TextField                     ”                                  ”                                  ”
List                              ”                                  ”                                  ”
Checkbox        ItemListener                ItemEvent                   itemStateChanged(ItemEvent)
Radio                          ”                                  ”                                  ”
Choice                         ”                                  ”                                  ”
TextArea         ActionListener                        ActionEvent                actionPerformed(ActionEvent ae)
Scrollbar          AdjustmentListener    AdjustmentEvent        adjustementValueChanged()


Swings
Swings are a part of Advanced Java.
Package required:        import java.swing.*;
Default layout of awt was “Flow Layout”. In swings, we need to set layout.
Swings are light weight components which provide extendibility to awt components. Swings is better than awt in look and feel. Swings also add further new components. e.g. JLabel, JTable, JScrollPane, JTree.
JLabel constructors:
1.      JLabel();
2.      JLabel(str, str);
3.      JLabel(str, image);
To add image:             ImageIcon ic = new ImageIcon(“Image_path”);
JButton constructors:
1.      JButton();
2.      JButton(String str)
3.      JButton(String str, image)
Demo
ImageIcon ic = new ImageIcon(“Image_path”);
JLabel l1 = new JLabel(“Name”);
JButton j = new JButton();
JButton j1 = new JButton(“Name”);
JButton j2 = new JButton(“Name”, ic);
cp.add(l1);
cp.add(j);
cp.add(j1);
cp.add(j2);
j1.addActionListener(this);

Checkbox constructors:
1.      JCheckbox();
2.      JCheckbox(str);
3.      JCheckbox(str, boolean);
4.      JCheckbox(str, image);
Method for Checkbox:                       isSelected();