Professional Documents
Culture Documents
Practical - 2: AIM - Implement Exception Handling and Multi-Threading in Program 1
Practical - 2: AIM - Implement Exception Handling and Multi-Threading in Program 1
// Create a subclass of Frame. class SampleFrame extends Frame { public Button okay, cancel; public Choice choice, choice1; public CheckboxGroup checkbox_group; public Checkbox[] checkboxes; public TextField textfield1,textfield2,textfield3; public TextArea textarea; public Panel panel1; // The layout manager for each of the containers. GridBagLayout gridbag = new GridBagLayout(); public SampleFrame(String title) { super(title); // Create pushbuttons okay = new Button("Sumbit"); cancel = new Button("Reset"); okay.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub Writer writer = null; try { writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("filename.txt"), "utf-8")); writer.write("\nBook Name: "+textfield1.getText()); ((BufferedWriter) writer).newLine(); writer.write("\nBook Type: "+checkbox_group.getSelectedCheckbox().getLabel()+"\n"); ((BufferedWriter) writer).newLine(); writer.write("Author Name: "+textfield3.getText()+"\n"); ((BufferedWriter) writer).newLine(); writer.write("Book ID: "+textfield2.getText()+"\n"); ((BufferedWriter) writer).newLine(); writer.write("Category: "+choice.getSelectedItem()+"\n"); ((BufferedWriter) writer).newLine(); writer.write("Year of Publish: "+choice1.getSelectedItem()+"\n"); JOptionPane.showMessageDialog(null, "Details are Submitted!!"); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "File not created or opened!!");
// report } finally { try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); // Create a menu of choices choice = new Choice(); choice.add("Computer Science & Engineering"); choice.add("Information Technology"); choice.add("Mechanical & Automation Engineering"); choice.add("Electrical & Communication Engineering"); choice.add("Instrumation Engineering"); choice1 = new Choice(); choice1.add("2014"); choice1.add("2013"); choice1.add("2012"); choice1.add("2011"); choice1.add("2010"); choice1.add("2009"); choice1.add("2008"); choice1.add("2007"); choice1.add("2006"); choice1.add("2005"); choice1.add("2004"); choice1.add("2003"); // Create checkboxes, and group them. checkbox_group = new CheckboxGroup(); checkboxes = new Checkbox[2]; checkboxes[0] = new Checkbox("Professional", checkbox_group, true); checkboxes[1] = new Checkbox("Magazine", checkbox_group, false); // Create a one-line text field, and multi-line text area. textfield1 = new TextField(40); textfield2 = new TextField(20); textfield3 = new TextField(40); textarea = new TextArea(6, 45); textarea.setEditable(true); // Create a Panel to contain all the components along the // left hand side of the window. Use a GridBagLayout for it. panel1 = new Panel(); panel1.setLayout(gridbag); // Use several versions of the constrain() convenience method // to add components to the panel and to specify their // GridBagConstraints values. constrain(panel1, new Label("Book Name:"), 0, 0, 1, 1); constrain(panel1, textfield1, 1, 0, 1, 1); constrain(panel1, new Label("Book Type:"), 0, 1, 1, 1, 10, 0, 0, 0); constrain(panel1, checkboxes[0], 1, 1, 1, 1, 10, 0, 0, 0);
constrain(panel1, checkboxes[1], 1, 2, 1, 1); constrain(panel1, new Label("Author Name:"), 0, 3, 1, 1, 10, 0, 0, 0); constrain(panel1, textfield3, 1, 3, 1, 1, 10, 0, 0, 0); constrain(panel1, new Label("Book ID:"), 0, 4, 1, 1, 10, 0, 0, 0); constrain(panel1, textfield2, 1, 4, 1, 1, 10, 0, 0, 0); constrain(panel1, new Label("Category:"), 0, 5, 1, 1, 10, 0, 0, 0); constrain(panel1, choice, 1, 5, 1, 1, 10, 0, 0, 0); constrain(panel1, new Label("Year of Publish:"), 0, 6, 1, 1, 10, 0, 0, 0); constrain(panel1, choice1, 1, 6, 1, 1, 10, 0, 0, 0); constrain(panel1, new Label("Other Information:"), 0, 8, 1, 1, 10, 0, 0, 0); constrain(panel1, textarea, 0, 9, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 10, 0, 0, 0); constrain(panel1, okay, 0, 14, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.3, 0.0, 10, 0, 0, 0); constrain(panel1, cancel, 1, 14, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.3, 0.0, 10, 0, 0, 0); this.setLayout(gridbag); // And add the panels to the toplevel window constrain(this, panel1, 0, 0, 1, 1, GridBagConstraints.VERTICAL, GridBagConstraints.NORTHWEST, 0.0, 1.0, 10, 10, 5, 5); // create an object to handle window events MyWindowAdapter adapter = new MyWindowAdapter(this); // register it to receive those events addWindowListener(adapter); } public void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height, int fill, int anchor, double weight_x, double weight_y, int top, int left, int bottom, int right) { GridBagConstraints c = new GridBagConstraints(); c.gridx = grid_x; c.gridy = grid_y; c.gridwidth = grid_width; c.gridheight = grid_height; c.fill = fill; c.anchor = anchor; c.weightx = weight_x; c.weighty = weight_y; if (top+bottom+left+right > 0) c.insets = new Insets(top, left, bottom, right); ((GridBagLayout)container.getLayout()).setConstraints(component, c); container.add(component); } public void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height) { constrain(container, component, grid_x, grid_y, grid_width, grid_height, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0, 0, 0, 0, 0); } public void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height, int top, int left, int bottom, int right) {
constrain(container, component, grid_x, grid_y, grid_width, grid_height, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0, top, left, bottom, right); } public static void main(String[] args) { Frame f = new SampleFrame("Book Information System"); // We should call f.pack() here. But its buggy. f.setSize(675, 475); f.setVisible(true); f.setResizable(false); } }
OUTPUT SCREEN
10
11
)VALUES('"+Book_Name+"','"+Book_Type+"','"+Book_Author+"','"+Book_ID+"','"+BOOK_C ATEGORY+"','"+BOOK_PUBLISH+"','"+Book_Description+"')"; if(!(st.execute(sql))) { JOptionPane.showMessageDialog(null,"Data saved successfully!!", "I N F O",JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null,"Data not saved!!","I N F O",JOptionPane.INFORMATION_MESSAGE); } } catch(Exception exc) { JOptionPane.showMessageDialog(null,"Db not connected"+exc.getMessage(),"I N F O",JOptionPane.INFORMATION_MESSAGE); } } }); // CREATE A MENU OF BRANCH CATEGORY choice = new Choice(); choice.add("Computer Science & Engineering"); choice.add("Information Technology"); choice.add("Mechanical & Automation Engineering"); choice.add("Electrical & Communication Engineering"); choice.add("Instrumation Engineering"); // CREATE A MENU OF YEAR OF PUBLISH choice1 = new Choice(); choice1.add("2014"); choice1.add("2013"); choice1.add("2012"); choice1.add("2011"); choice1.add("2010"); choice1.add("2009"); choice1.add("2008"); choice1.add("2007"); choice1.add("2006"); choice1.add("2005"); choice1.add("2004"); choice1.add("2003"); // CREATE CHECKBOX GROUP AND ADD CHECKBOXES TO IT checkbox_group = new CheckboxGroup(); checkboxes = new Checkbox[2]; checkboxes[0] = new Checkbox("Professional", checkbox_group, true); checkboxes[1] = new Checkbox("Magazine", checkbox_group, false); // CREATE TEXT FIELD AND MULTI-LINE TEXT AREA textfield1 = new TextField(40); /* BOOK NAME */ textfield2 = new TextField(20); /* AUTHOR NAME */ textfield3 = new TextField(40); /* BOOK ID */ textarea = new TextArea(6, 45); /* OTHER DESCRIPTION */ textarea.setEditable(true); // CREATE A PANEL TO CONTAIN ALL THE COMPONENTS panel1 = new Panel(); panel1.setLayout(gridbag);
12
// CONSTRAIN()METHOD TO ADD COMPONENTS TO THE PANEL AND TO SPECIFY THEIR GRIDBAGCONSTRAINTS VALUES constrain(panel1, new Label("Book Name:"), 0, 0, 1, 1); constrain(panel1, textfield1, 1, 0, 1, 1); constrain(panel1, new Label("Book Type:"), 0, 1, 1, 1, 10, 0, 0, 0); constrain(panel1, checkboxes[0], 1, 1, 1, 1, 10, 0, 0, 0); constrain(panel1, checkboxes[1], 1, 2, 1, 1); constrain(panel1, new Label("Author Name:"), 0, 3, 1, 1, 10, 0, 0, 0); constrain(panel1, textfield3, 1, 3, 1, 1, 10, 0, 0, 0); constrain(panel1, new Label("Book ID:"), 0, 4, 1, 1, 10, 0, 0, 0); constrain(panel1, textfield2, 1, 4, 1, 1, 10, 0, 0, 0); constrain(panel1, new Label("Category:"), 0, 5, 1, 1, 10, 0, 0, 0); constrain(panel1, choice, 1, 5, 1, 1, 10, 0, 0, 0); constrain(panel1, new Label("Year of Publish:"), 0, 6, 1, 1, 10, 0, 0, 0); constrain(panel1, choice1, 1, 6, 1, 1, 10, 0, 0, 0); constrain(panel1, new Label("Other Information:"), 0, 8, 1, 1, 10, 0, 0, 0); constrain(panel1, textarea, 0, 9, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 10, 0, 0, 0); constrain(panel1, okay, 0, 14, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.3, 0.0, 10, 0, 0, 0); constrain(panel1, cancel, 1, 14, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.3, 0.0, 10, 0, 0, 0); this.setLayout(gridbag); constrain(this, panel1, 0, 0, 1, 1, GridBagConstraints.VERTICAL, GridBagConstraints.NORTHWEST, 0.0, 1.0, 10, 10, 5, 5); // CREATE AN OBJECT TO HANDLE WINDOW EVENTS MyWindowAdapter adapter = new MyWindowAdapter(this); addWindowListener(adapter); } public void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height, int fill, int anchor, double weight_x, double weight_y, int top, int left, int bottom, int right) { GridBagConstraints c = new GridBagConstraints(); c.gridx = grid_x; c.gridy = grid_y; c.gridwidth = grid_width; c.gridheight = grid_height; c.fill = fill; c.anchor = anchor; c.weightx = weight_x; c.weighty = weight_y; if (top+bottom+left+right > 0) c.insets = new Insets(top, left, bottom, right); ((GridBagLayout)container.getLayout()).setConstraints(component, c); container.add(component); } public void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height) { constrain(container, component, grid_x, grid_y, grid_width, grid_height, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0, 0, 0, 0, 0); }
13
public void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height, int top, int left, int bottom, int right) { constrain(container, component, grid_x, grid_y, grid_width, grid_height, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0, top, left, bottom, right); } public static void main(String[] args) { Frame f = new SampleFrame("Book Information System"); f.setSize(675, 475); f.setVisible(true); f.setResizable(false); } }
/* CLASS dbAccess.java */
package PRACTICAL3; import java.sql.*; import javax.swing.JOptionPane; public class dbAccess { public static Connection connect() { Connection conn = null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection( "JDBC:ODBC:PRACTICAL3_DATABASE", "USER", "USER" ); } catch(Exception err) { JOptionPane.showMessageDialog(null,"Db not connected" + err.getMessage(),"I N F O",JOptionPane.INFORMATION_MESSAGE); } return conn; } } /* CLASS MyWindowAdapter.java */ package PRACTICAL3; import java.awt.event.*; class MyWindowAdapter extends WindowAdapter { SampleFrame sampleFrame; public MyWindowAdapter(SampleFrame sampleFrame) { this.sampleFrame = sampleFrame; } public void windowClosing(WindowEvent we) { sampleFrame.setVisible(false); } }
14
OUTPUT SCREEN
15
16
} catch(Exception e){} } } }
/* Client.java */
package PRACTICAL4; import java.io.*; import java.net.*; public class Client { private static Socket socket; public static void main(String args[]) { try { String host = "localhost"; int port = 25000; InetAddress address = InetAddress.getByName(host); socket = new Socket(address, port); //SEND THE MESSAGE TO THE SERVER OutputStream os = socket.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os); BufferedWriter bw = new BufferedWriter(osw); String number = "2"; String sendMessage = number + "\n"; bw.write(sendMessage); bw.flush(); System.out.println("Message sent to the server : "+sendMessage); //GET THE RETURN MESSAGE FROM THE SERVER InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String message = br.readLine(); System.out.println("Message received from the server : " +message); } catch (Exception exception) { exception.printStackTrace(); } finally { //CLOSING THE SOCKET try { socket.close(); } catch(Exception e) { e.printStackTrace(); } } } }
17
OUTPUT SCREEN
18