hi every Java Master or Java Professional , my Name Is Vincent i'm java beginner can i ask a question regarding for Java application convert to Java Applet. below her is my sample application hope every master or professional can teaching me thank you.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*;
public class AddressBook extends JFrame implements ActionListener{ public void frame(){ AddressBook panel = new AddressBook(); panel.setSize(320,350); panel.setVisible(true); panel.setResizable(false); panel.setLocation(320,200);
}
JLabel lblId = new JLabel("ID Number : ",JLabel.RIGHT);
JLabel lblFName = new JLabel("First Name : ",JLabel.RIGHT);
JLabel lblAddress = new JLabel("Address : ",JLabel.RIGHT);
JLabel lblLName = new JLabel("Last Name : ",JLabel.RIGHT);
JLabel lblPNumber = new JLabel("Phone : ",JLabel.RIGHT);
JTextField txtId = new JTextField(20);
JTextField txtFName= new JTextField(20);
JTextField txtAddress= new JTextField(20);
JTextField txtLName = new JTextField(20);
JTextField txtPNumber = new JTextField(20);
JButton btnNew = new JButton(new ImageIcon("news.jpg"));
JButton btnUpdate = new JButton(new ImageIcon("update.jpg"));
JButton btnDelete = new JButton(new ImageIcon("delete.jpg"));
JButton btnSearch = new JButton(new ImageIcon("search.jpg"));
Connection cn;
Statement st;
PreparedStatement ps;
public void clear(){
txtId.setText("");
txtFName.setText("");
txtAddress.setText("");
txtLName.setText("");
txtPNumber.setText("");
}
public AddressBook() {
super("MY Electronic Address Book Application ");
JPanel pane = new JPanel();
pane.setLayout(null);
pane.setBackground(Color.pink);
lblId.setBounds(5,50,80,25);
pane.add(lblId);
txtId.setBounds(90,50,150,25);
pane.add(txtId);
lblFName.setBounds(5,85,80,25);
pane.add(lblFName);
txtFName.setBounds(90,85,150,25);
pane.add(txtFName);
lblLName.setBounds(5,120,80,25);
pane.add(lblLName);
txtLName.setBounds(90,120,150,25);
pane.add(txtLName);
lblPNumber.setBounds(5,155,80,25);
pane.add(lblPNumber);
txtPNumber.setBounds(90,155,150,25);
pane.add(txtPNumber);
lblAddress.setBounds(5,190,80,25);
pane.add(lblAddress);
txtAddress.setBounds(90,190,150,25);
pane.add(txtAddress);
btnNew.addActionListener(this);
btnNew.setBounds(5,255,75,35);
btnNew.setBackground(Color.white);
pane.add(btnNew);
btnNew.addActionListener(this);
btnUpdate.setBounds(80,255,75,35);
btnUpdate.setBackground(Color.white);
pane.add(btnUpdate);
btnUpdate.addActionListener(this);
btnDelete.setBounds(155,255,75,35);
btnDelete.setBackground(Color.white);
pane.add(btnDelete);
btnDelete.addActionListener(this);
btnSearch.setBounds(230,255,75,35);
btnSearch.setBackground(Color.white);
pane.add(btnSearch);
btnSearch.addActionListener(this);
setContentPane(pane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), " Personal Information"));
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn = DriverManager.getConnection("jdbc:odbc:AddressBook ");
}catch(ClassNotFoundException e) {
System.err.println("Failed to load driver");
e.printStackTrace();
}catch(SQLException e){
System.err.println("Unable to connect");
e.printStackTrace();}
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == btnNew){
try{
st= cn.createStatement();
ps=cn.prepareStatement("INSERT INTO PInfo " + " (IDN,FName,LName,PNumber,Address) " + " VALUES(?,?,?,?,?)");
ps.setString(1,txtId.getText());
ps.setString(2,txtFName.getText());
ps.setString(3,txtLName.getText());
ps.setString(4,txtPNumber.getText());
ps.setString(5,txtAddress.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(null,"New record has been successfully added.","Suspect sample program",JOptionPane.INFORMATION_MESSAGE);
txtId.requestFocus(true);
st.close();
clear();
}catch(SQLException sqlEx){
System.out.println(sqlEx.getMessage());}}
if(source == btnSearch){
try{
st= cn.createStatement();
int id=0;
String search;
clear();
search=JOptionPane.showInputDialog(null,"Search Id Number","Find What?",JOptionPane.PLAIN_MESSAGE);
ResultSet rs=st.executeQuery("SELECT * FROM PInfo WHERE IDN = '" + search + "'");
while(rs.next()){
txtId.setText(rs.getString(1));
txtFName.setText(rs.getString(2));
txtLName.setText(rs.getString(3));
txtPNumber.setText(rs.getString(4));
txtAddress.setText(rs.getString(5));
id=Integer.parseInt(rs.getString(1));
}
if(id==0){
JOptionPane.showMessageDialog(null,"No record found!.","Suspect sample program",JOptionPane.ERROR_MESSAGE);
clear();
txtId.requestFocus(true);
}st.close();
}catch(SQLException s){}}
if(source == btnUpdate){
try{
st= cn.createStatement();
ps = cn.prepareStatement("UPDATE PInfo SET IDN = '" + txtId.getText() + "',FName = '" + txtFName.getText() + "',LName = '" + txtLName.getText() + "',PNumber = '" + txtPNumber.getText() + "',Address = '"+ txtAddress.getText() + "'WHERE IDN = '" + txtId.getText() + "'");
ps.executeUpdate();
JOptionPane.showMessageDialog(null,"record has been successfully updated.","Suspect sample program",JOptionPane.INFORMATION_MESSAGE);
txtId.requestFocus(true);
clear();
st.close();
}catch (Exception y){
y.printStackTrace();}}
if(source==btnDelete){
try{
ps = cn.prepareStatement("DELETE FROM PInfo WHERE IDN ='"+ txtId.getText() + "'");
ps.executeUpdate();
JOptionPane.showMessageDialog(null,"record has been successfully deleted.","Suspect sample program",JOptionPane.INFORMATION_MESSAGE);
txtId.requestFocus(true);
clear();
st.close();
}catch(SQLException s){
System.out.print("SQL statement is not executed!");}
catch(Exception j){
j.printStackTrace();
}
}
}
public static void main(String[] args) { AddressBook s1=new AddressBook(); s1.setSize(320,350); s1.setLocation(320,200); s1.setResizable(true); s1.setVisible(true); }
}