77
SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGAR AJT- 170703 PRACTICAL – 1 (AWT) 1) Write an AWT program to create check boxes for different courses belongs to a university such that the course selected would be displayed. import java.applet.Applet; import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.*; public class pract1 extends Applet implements ItemListener,ActionListener { Button b1; Checkbox IT = null; Checkbox CE = null; Checkbox mech = null; Checkbox EE = null; Checkbox EC = null; Checkbox civil = null; public void init() { IT = new Checkbox("IT"); CE = new Checkbox("CE"); mech = new Checkbox("mech"); EE = new Checkbox("EE"); EC = new Checkbox("EC"); civil = new Checkbox("Civil"); b1 = new Button("Output"); add(IT);add(CE);add(mech);add(EE);add(EC);add(civil); add(b1); b1.addActionListener(this); IT.addItemListener(this); CE.addItemListener(this); mech.addItemListener(this); EE.addItemListener(this); EC.addItemListener(this); civil.addItemListener(this); } public void actionPerformed(ActionEvent e) INFORMATION TECHNOLOGY DEPARMENT 1

Advance Java Practical List

Embed Size (px)

DESCRIPTION

Advance Java Practical

Citation preview

SHANTILAL SHAH ENGG. COLLEGE, BHAVNAGARAJT- 170703PRACTICAL 1 (AWT)

1) Write an awt program to create check boxes for different courses belongs to a university such that the course selected would be displayed.

import java.applet.Applet;import java.awt.*;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.*;public class pract1 extends Applet implements ItemListener,ActionListener{Button b1; Checkbox IT = null; Checkbox CE = null; Checkbox mech = null;Checkbox EE = null; Checkbox EC = null; Checkbox civil = null; public void init() {IT = new Checkbox("IT");CE = new Checkbox("CE");mech = new Checkbox("mech");EE = new Checkbox("EE");EC = new Checkbox("EC");civil = new Checkbox("Civil");b1 = new Button("Output");add(IT);add(CE);add(mech);add(EE);add(EC);add(civil);add(b1); b1.addActionListener(this);IT.addItemListener(this);CE.addItemListener(this);mech.addItemListener(this);EE.addItemListener(this); EC.addItemListener(this); civil.addItemListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == b1){ repaint();} } public void paint(Graphics g) { int i=10;int k=60; if(IT.getState()==true){ g.drawString("IT",10,k+=20);}if(CE.getState()==true){ g.drawString("CE",10,k+=20);}if(mech.getState()==true){ g.drawString("mech",10,k+=20);}if(EE.getState()==true){ g.drawString("EE",10,k+=20);}if(EC.getState()==true){ g.drawString("EC",10,k+=20);}if(civil.getState()==true){ g.drawString("civil",10,k+=20);} } public void itemStateChanged(ItemEvent ie) { //repaint(); } }

Output:

2) Create a list of vegetables if you click on one of the items of the list items would be displayed in text box.

import java.applet.Applet;import java.awt.Graphics;import java.awt.*;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;/*

*/public class pract2 extends Applet implements ItemListener{ List list = null; TextField a; public void init() { a = new TextField( 70); //create a multi select list list = new List(5, true); //add items to a list list.add("potatoes,"); list.add("Brinjal,"); list.add("Cabbage,"); list.add("Carrot,"); list.add("Onion,"); list.add("Mint,"); list.add("potato,"); //add list add(list); add(a); //add listener list.addItemListener(this); } public void paint(Graphics g) { String[] items = list.getSelectedItems(); String msg = ""; for(int i=0; i < items.length; i++){msg = items[i] + " " + msg; } a.setText("vegetables:"+ msg); } public void itemStateChanged(ItemEvent ie) { repaint(); }}Output:

PRACTICAL 2 (SWING)

3) Create a login form which contains user ID, password field and two buttons submit and reset. If the user ID and password is left blank on click of submit button, show a message to the user to fill in the fields, on click of reset button clear the fields.

import java.awt.*;import java.awt.event.*;import javax.swing.*;publicclass login extendsJFrame{privateJLabel jLabel1;privateJLabel jLabel2;privateJTextField jTextField1;privateJPasswordField jPasswordField1;privateJButton jButton1;privateJPanel contentPane;public login(){super();create();this.setVisible(true);}privatevoid create(){jLabel1 = newJLabel();jLabel2 = newJLabel();jTextField1 = newJTextField();jPasswordField1 = newJPasswordField();jButton1 = newJButton();contentPane = (JPanel)this.getContentPane();jLabel1.setHorizontalAlignment(SwingConstants.LEFT);jLabel1.setForeground(newColor(0, 0, 255));jLabel1.setText("username:");jLabel2.setHorizontalAlignment(SwingConstants.LEFT);jLabel2.setForeground(newColor(0, 0, 255));jLabel2.setText("password:");jTextField1.setForeground(newColor(0, 0, 255));jTextField1.setSelectedTextColor(newColor(0, 0, 255));jTextField1.setToolTipText("Enter your username");jTextField1.addActionListener(newActionListener(){publicvoid actionPerformed(ActionEvent e){jTextField1_actionPerformed(e);}}jPasswordField1.setForeground(newColor(0, 0, 255));jPasswordField1.setToolTipText("Enter your password");

jPasswordField1.addActionListener(newActionListener(){publicvoid actionPerformed(ActionEvent e){jPasswordField1_actionPerformed(e);}}jButton1.setBackground(newColor(204, 204, 204));jButton1.setForeground(newColor(0, 0, 255));jButton1.setText("Login");jButton1.addActionListener(newActionListener(){publicvoid actionPerformed(ActionEvent e){jButton1_actionPerformed(e);}}contentPane.setLayout(null);contentPane.setBorder(BorderFactory.createEtchedBorder());contentPane.setBackground(newColor(204, 204, 204));addComponent(contentPane, jLabel1, 5,10,106,18);addComponent(contentPane, jLabel2, 5,47,97,18);addComponent(contentPane, jTextField1, 110,10,183,22);addComponent(contentPane, jPasswordField1, 110,45,183,22);addComponent(contentPane, jButton1, 150,75,83,28);this.setTitle("Login To Members Area");this.setLocation(newPoint(76, 182));this.setSize(newDimension(335, 141));this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setResizable(false);}privatevoid addComponent(Container container,Component c,int x,int y,int width,int height){c.setBounds(x,y,width,height);container.add(c);}privatevoid jTextField1_actionPerformed(ActionEvent e){}privatevoid jPasswordField1_actionPerformed(ActionEvent e){}privatevoid jButton1_actionPerformed(ActionEvent e){System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");String username = newString(jTextField1.getText());String password = newString(jPasswordField1.getText());if(username.equals("") || password.equals("")){jButton1.setEnabled(false);JLabel errorFields = newJLabel("You must entera username and password to login."); JOptionPane.showMessageDialog(null,errorFields);

jTextField1.setText("");jPasswordField1.setText("");jButton1.setEnabled(true);this.setVisible(true);}else{JLabel optionLabel = newJLabel("You entered"+username+"as your username.
Is this correct?");int confirm =JOptionPane.showConfirmDialog(null,optionLabel);switch(confirm){caseJOptionPane.YES_OPTION: jButton1.setEnabled(false); break;caseJOptionPane.NO_OPTION: jButton1.setEnabled(false);jTextField1.setText("");jPasswordField1.setText("");jButton1.setEnabled(true);break;caseJOptionPane.CANCEL_OPTION: jButton1.setEnabled(false);jTextField1.setText("");jPasswordField1.setText("");jButton1.setEnabled(true);break;}}}publicstaticvoid main(String[] args){JFrame.setDefaultLookAndFeelDecorated(true);JDialog.setDefaultLookAndFeelDecorated(true);try{UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");}catch(Exception ex){System.out.println("Failed loading L&F: ");System.out.println(ex);}new login();}}

Output:

Fig: Showing Login Form One with Empty Username And Password

4) Create a split pane which divides the frame into two parts .process a list and on selecting an item in a list the items should be displayed in the other portion.

import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.event.*;public class pract4 { @SuppressWarnings("deprecation")public static void main(String[] args){ JFrame frame = new SplitPaneFrame();frame.show(); }}class SplitPaneFrame extends JFrame implements ListSelectionListener{ public SplitPaneFrame(){setSize(400, 300);list = new JList(texts);list.addListSelectionListener(this);description = new JTextArea();JSplitPane innerPane= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list, description);getContentPane().add(innerPane, "Center"); } public void valueChanged(ListSelectionEvent event) { JList source = (JList)event.getSource(); Display value = (Display)source.getSelectedValue(); description.setText(value.getDescription()); } private JList list; private JTextArea description; private Display[] texts = { new Display("Text1", "This is text1."), new Display("Text2", "This is text2."), new Display("Text3", "This is text3."), new Display("Text4", "This is text4.") };}class Display{ public Display(String n, String t){ name = n; des = t; } public String toString() {return name; } public String getDescription() {return des; } private String name; private String des;}

Output:

Figure: Output of split pan

PRACTICAL 3 (JDBC)

5) Write a jdbc program to insert details of college student in the ms Access database.

/* database connectivity with MS-Access is done by creatingDataSourceName(dsn) in this example*//* Steps to use this example: * go to ms-access and make a database called "student_base" and createtable named student_base.mdb * 1.Go to Control Panel 2.Click on Administrative Tools(windows 2000/xp), Click onODBC(win98) OR if u have (windows 7) than go to C:\Windows\SysWOW64\odbcad32.exe 3.click on ODBC 4.Then , you will see a ODBC dialog box. Click on UserDSn 5.Click on Add Button 6.Select Microsoft Access Driver(*.mdb) driver and click on finish 7.Give a Data Source Name : student_base 8.Then Click on Select 9.Browse on the database addItemDB.mdb file on your disk by downloading it link provided..will be stored 10.Click on OK.Once the DSN is created, you can do this example*/

//Java Core Packageimport javax.swing.*;//Java Extension Packageimport java.awt.*;import java.awt.event.*;import java.sql.*;public class addItemToDatabase extends JFrame {//Initializing Componentsprivate JTextField inputs[];private JButton add, reset;private JLabel labels[];private String fldLabel[] = {"First Name: ","Last Name: ","Branch","Enroll-no "};private JPanel p1;Connection con;Statement st;ResultSet rs;String db;//Setting up GUI public addItemToDatabase() {//Setting up the Title of the Windowsuper("Adding Data to the Database");//Set Size of the Window (WIDTH, HEIGHT)setSize(300,180);//Exit Property of the WindowsetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Constructing Componentsinputs = new JTextField[4];labels = new JLabel[4];add = new JButton("Add");reset = new JButton("Reset");p1 = new JPanel();//Setting Layout on JPanel 1 with 5 rows and 2 columnp1.setLayout(new GridLayout(5,2));//Setting up the container ready for the components to be added.Container pane = getContentPane();setContentPane(pane);//Setting up the container layoutGridLayout grid = new GridLayout(1,1,0,0);pane.setLayout(grid);//Creating a connection to MS Access and fetching errors using "try-catch" to check if it is successfully connected or not.try {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//db = "jdbc:odbc:Driver={Microsoft Access Driver (*.accdb)};DBQ=nilesh1.accdb;";con = DriverManager.getConnection("jdbc:odbc:nilesh");st = con.createStatement();JOptionPane.showMessageDialog(null,"Successfully Connected to Database","Confirmation", JOptionPane.INFORMATION_MESSAGE);} catch (Exception e) {JOptionPane.showMessageDialog(null,"Failed to Connect to Database","Error Connection", JOptionPane.ERROR_MESSAGE);System.exit(0);}//Constructing JLabel and JTextField using "for loop" in their desired orderfor(int count=0; count0){for(int i=0;i

EmployeeListcom.kogent.hibernate.EmployeeList

EmployeeListServletcom.kogent.hibernate.EmployeeListServlet

AddEmployeeServletcom.kogent.hibernate.AddEmployeeServlet

DeleteEmployeeServletcom.kogent.hibernate.DeleteEmployeeServlet

EditEmployeeServletcom.kogent.hibernate.EditEmployeeServlet

EmployeeListServlet/EmployeeListServlet

AddEmployeeServlet/AddEmployeeServlet

DeleteEmployeeServlet/DeleteEmployeeServlet

EditEmployeeServlet/EditEmployeeServlet

11.AddEmployee.jsp

Employee Id

Employee Name

Employee Age

Employee Salary

12.EditEmployee.jsp

Employee Id

Employee Name

Employee Age

Employee Salary

13.EmployeeList.jsp

Employee IdName

AgeSalary

0){for(int i=0;i Delete Edit

Add New employee

Output:

Fig: Showing The Employee List Page

Figure: Showing the add employee page.

PRACTICAL 9 (STRUT-MVC ARCHITECTURE)

19) Develop a Java application using Struct framework (MVC architecture) in Eclipse or Netbeans.

import java.sql.*;import java.util.*;

publicclass GetEmployeeAddressUsingStruct {publicstaticvoid main(String s[]) throws Exception {Driver d= (Driver) ( Class.forName("oracle.jdbc.driver.OracleDriver").newInstance());Properties p=new Properties();p.put("user","scott");p.put("password","tiger");Connectioncon=d.connect("jdbc:oracle:thin:@192.168.1.123:1521:XE",p);Statement st=con.createStatement();ResultSet rs=st.executeQuery("select permanent_address from personaldetails where empno="+s[0]);if (rs.next()){System.out.println("Employee Found: Address");Struct struct=(Struct)rs.getObject(1);Object addr[]=struct.getAttributes();System.out.println("Flatno : "+addr[0]);System.out.println("Street : "+ addr[1]);System.out.println("Pin : "+addr[4]);}//ifcon.close();}//main}//classOutput:

INFORMATION TECHNOLOGY DEPARMENT