any program of hotel or school management
Hello Friend,
We have created a student Information application.Here is the code:
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.sql.*; import java.util.*; class StudentInformation{ JFrame f; JPanel p1,p2,p3,p4; JTabbedPane tp; ImageIcon btnimg1,btnimg2; JLabel l1, l2, l3, l4,l5,l6,l7,l8,l9,l10,l11; JTextField tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10,tf11; JScrollPane sp1; JButton savebtn,resetbtn,editbtn1,editbtn2,viewbtn,delbtn ; StudentInformation(){ f=new JFrame("Form"); p1=new JPanel(new GridLayout(5,2)); p2=new JPanel(new GridLayout(5,2)); p3=new JPanel(new GridLayout(2,2)); p4=new JPanel(new GridLayout(2,2)); tp=new JTabbedPane(); l1=new JLabel("Student ID:"); l2=new JLabel("First Name:"); l3=new JLabel("Last Name:"); l4=new JLabel("Address:"); l5=new JLabel("Enter Employee ID to view Record:"); l7=new JLabel("Student ID:"); l8=new JLabel("First Name:"); l9=new JLabel("Last Name:"); l10=new JLabel("Address:"); l11=new JLabel("Enter ID:"); tf1=new JTextField(12); tf2=new JTextField(12); tf3=new JTextField(12); tf4=new JTextField(12); tf5=new JTextField(12); tf6=new JTextField(12); tf7=new JTextField(12); tf8=new JTextField(12); tf9=new JTextField(12); tf10=new JTextField(12); tf11=new JTextField(12); savebtn=new JButton(" Add "); resetbtn=new JButton(" Reset"); editbtn1=new JButton(" Edit "); editbtn2=new JButton(" Save"); viewbtn=new JButton("view"); delbtn=new JButton("Delete"); p1.add(l1); p1.add(tf1); p1.add(l2); p1.add(tf2); p1.add(l3); p1.add(tf3); p1.add(l4); p1.add(tf4); p1.add(savebtn); p1.add(resetbtn); p2.add(l7); p2.add(tf7); p2.add(l8); p2.add(tf8); p2.add(l9); p2.add(tf9); p2.add(l10); p2.add(tf10); p2.add(editbtn1); p2.add(editbtn2); p3.add(l5); p3.add(tf5); p3.add(viewbtn); p4.add(l11); p4.add(tf11); p4.add(delbtn); resetbtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ tf1.setText(""); tf2.setText(""); tf3.setText(""); tf4.setText(""); }
});
continue..
savebtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ String value1=tf1.getText(); String value2=tf2.getText(); String value3=tf3.getText(); String value4=tf4.getText(); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:access"); PreparedStatement st=con.prepareStatement("insert into student(id, FirstName,LastName,Address) values(?,?,?,?)"); st.setString(1,value1); st.setString(2,value2); st.setString(3,value3); st.setString(4,value4); st.executeUpdate(); JOptionPane.showMessageDialog(p1,"Data is successfully inserted into database."); con.close(); } catch(Exception e){ JOptionPane.showMessageDialog(p1,"Error in submitting data!"); } } }); viewbtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ JTextField text1,text2,text3,text4; JLabel label1,label2,label3,label4; JPanel panel; text1=new JTextField(); text2=new JTextField(); text3=new JTextField(); text4=new JTextField(); label1=new JLabel("ID"); label2=new JLabel("First Name"); label3=new JLabel("Last Name"); label4=new JLabel("Address"); panel=new JPanel(new GridLayout(4,2)); String value1=tf5.getText(); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:access"); PreparedStatement st=con.prepareStatement("Select * from student where id=?"); st.setString(1,value1); ResultSet rs=st.executeQuery(); while(rs.next()){ text1.setText(Integer.toString(rs.getInt(1))); text2.setText(rs.getString(2)); text3.setText(rs.getString(3)); text4.setText(rs.getString(4)); } panel.add(label1); panel.add(text1); panel.add(label2); panel.add(text2); panel.add(label3); panel.add(text3); panel.add(label4); panel.add(text4); JFrame frame=new JFrame(); frame.add(panel); frame.setSize(300,200); frame.setVisible(true); con.close(); } catch(Exception exp3){} } }); editbtn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ String value=tf7.getText(); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:access"); PreparedStatement st=con.prepareStatement("select * from student where id=?"); st.setString(1,value); ResultSet res=st.executeQuery(); res.next(); tf7.setText(Integer.toString(res.getInt(1))); tf8.setText(res.getString(2)); tf9.setText(res.getString(3)); tf10.setText(res.getString(4)); con.close(); } catch(Exception e){ JOptionPane.showMessageDialog(p2,"Can not edit data"); } } });
continue..
editbtn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ try{ int x=JOptionPane.showConfirmDialog(p2,"Confirm edit? All data will be replaced"); if(x==0){ try{ String value1=tf7.getText(); String value2=tf8.getText(); String value3=tf9.getText(); String value4=tf10.getText(); int id=Integer.parseInt(value1); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:access"); Statement st=con.createStatement(); st.executeUpdate("update student set FirstName='"+value2+"', LastName='"+value3+"', Address='"+value4+"' where id="+id+""); JOptionPane.showMessageDialog(p2,"Updated successfully"); con.close(); } catch(Exception ex){ JOptionPane.showMessageDialog(p2,"Error in updating edit fields"); } } } catch(Exception ex){ JOptionPane.showMessageDialog(p2,"Error"); } } }); delbtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ String value1=tf5.getText(); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:access"); PreparedStatement st=con.prepareStatement("DELETE FROM student WHERE id = ?"); st.setString(1,value1); st.executeUpdate(); JOptionPane.showMessageDialog(p3,"Record is deleted successfully."); con.close(); } catch(Exception exp3) { JOptionPane.showMessageDialog(p3,"Error in deleting record."); } } }); } void dis(){ f.getContentPane().add(tp); tp.addTab("View Record",p3); tp.addTab("Add Record",p1); tp.addTab("Edit Record",p2); tp.addTab("Delete Record",p4); f.setSize(450,180); f.setVisible(true); f.setResizable(true); } public static void main(String z[]){ StudentInformation pro=new StudentInformation(); pro.dis(); } }
Hope that it will be helpful for you. Thanks
Ads