0% found this document useful (0 votes)
117 views

Java File Prog 12-13

The program creates a frame with a choice component that allows selecting countries, and adds an item listener to update a label with the selected country; it also adds a button that opens a new child frame when clicked. The code uses anonymous inner classes to handle the item and action events. The program illustrates using JDBC to connect to an MS Access database and retrieve data from a table.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Java File Prog 12-13

The program creates a frame with a choice component that allows selecting countries, and adds an item listener to update a label with the selected country; it also adds a button that opens a new child frame when clicked. The code uses anonymous inner classes to handle the item and action events. The program illustrates using JDBC to connect to an MS Access database and retrieve data from a table.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Program 12

Objective: Write a program to create a standalone window to handle ItemEvent


corresponding to a choice component added to it using the concept of Anonymous Inner
Classes. Also add a button to open a child frame inside this frame.

import java.awt.*;
import java.awt.event.*;
public class AnonymousInner extends Frame
{
AnonymousInner(){
Label label = new Label();
label.setAlignment(Label.CENTER);
label.setSize(400,100);
Button b= new Button("open new window");
b.setBounds(100,200,100,20);
Choice c=new Choice();
c.setBounds(100,100, 100,75);
c.add("India");
c.add("United States");
c.add("Japan");
c.add("France");
c.add("Australia");
this .add(c);
this.add(label); this.add(b);
this.setSize(400,400);
this.setLayout(null);
this.setVisible(true);
c.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String data = "Country Selected: "+ c.getItem(c.getSelectedIndex());
label.setText(data);
}
});

b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Frame f1 = new Frame();
f1.setTitle("Child Frame");
f1.setSize(100,100);
f1.setLayout(null);
f1.setVisible(true);
}
});

}
public static void main(String args[])
{
new AnonymousInner();
}
}
Output: Program 12
Program 13
Objective: Write a program to illustrate the concept of JDBC.

package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import java.sql.*;
class Test{
public static void main(String ar[]){
try{
String url="jdbc:ucanaccess://C:\\Users\\Shubham\\Desktop\\javafile\\Record.accdb";
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection(url);
Statement st=c.createStatement();
ResultSet rs=st.executeQuery("select * from User");

while(rs.next()){
System.out.println("SNo." + rs.getString(1));
System.out.println("Name: " +rs.getString(2));
System.out.println("Roll No " + rs.getString(3));
}

}catch(Exception ee){System.out.println(ee);}

}}
Output: Program 13

MS Access Database with Table named as User

You might also like