hi,
Iam trying to add names and numbers to a database then save them by updating them. i run my program, but when i press any button nothing happens?????
pls help me, i do not know where is the problem in my program????
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class Ghu extends JFrame implements ActionListener {
final static String
dbURL="jdbc:odbc:addressesdb";
private final String WINDOW_TITLE= "AddressBook";
private final int WINDOW_WIDTH= 400;
private final int WINDOW_HEIGHT= 300;
private JPanel adPanel= new JPanel();
private JPanel delPanel= new JPanel();
private JPanel updPanel= new JPanel();
private JLabel adNameLabel= new JLabel("Name");
private JLabel adNumLabel= new JLabel("Number");
private JLabel delNumLabel= new JLabel("Number");
private JLabel updNameLabel= new JLabel("Name");
private JLabel updNumLabel= new JLabel("Number");
private JTextField adNameField= new JTextField(20);
private JTextField adNumField= new JTextField(20);
private JTextField updNameField= new JTextField(20);
private JTextField updNumField= new JTextField(20);
private JTextField delNumField= new JTextField(20);
private JButton adButton= new JButton("Add");
private JButton delButton= new JButton("Delete");
private JButton updButton= new JButton("Update");
private JButton exitButton= new JButton("Exit");
Connection conn;
Statement stmt;
ResultSet rset;
public static void main(String args[]) {
new Ghu();
}
public Ghu() {
setTitle(WINDOW_TITLE);
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildAdPanel();
buildDelPanel();
buildUpdPanel();
add(adPanel,BorderLayout.NORTH);
add(delPanel,BorderLayout.CENTER);
add(updPanel,BorderLayout.SOUTH);
}
public boolean isConnected(){
boolean connected=false;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(dbURL);
stmt = conn.createStatement();
connected=true;
}
catch (ClassNotFoundException e) {
System.err.println("Can't find JDBC to ODBC Bridge");
}
catch(SQLException e) {
System.err.println("An SQL exception occurred while trying
to connect to the server: " +e.getMessage());
}
return connected;
}
public void actionPerformed(ActionEvent evt) {
try {
String command=evt.getActionCommand();
if (evt.getSource()==adButton)
processAdd();
else if (evt.getSource()==delButton)
processDelete();
else if (evt.getSource()==updButton)
processUpdate();
else if (evt.getSource()==exitButton) {
stmt.close();
conn.close();
System.exit(0);
}
}
catch (SQLException e) {
System.err.println("An SQL exception occurred
while exiting: " +e.getMessage());
}
}
public void buildAdPanel() {
adPanel.setLayout(new GridLayout(3,2));
adPanel.add(adNameLabel);
adPanel.add(adNameField);
adPanel.add(adNumLabel);
adPanel.add(adNumField);
adPanel.add(adButton);
}
public void buildDelPanel() {
delPanel.setLayout(new GridLayout(2,2));
delPanel.add(delNumLabel);
delPanel.add(delNumField);
delPanel.add(delButton);
}
public void buildUpdPanel() {
updPanel.setLayout(new GridLayout(3,2));
updPanel.add(updNameLabel);
updPanel.add(updNameField);
updPanel.add(updNumLabel);
updPanel.add(updNumField);
updPanel.add(updButton);
updPanel.add(exitButton);
}
private void processAdd() {
String sql;
int nrows;
sql="insert into addressesdb values(?,?)";
try {
PreparedStatement psmt=conn.prepareStatement(sql);
psmt.setString(1,adNumField.getText());
psmt.setString(2,adNameField.getText());
nrows=psmt.executeUpdate();
psmt.close();
JOptionPane.showMessageDialog(null,"The name is
saved");
}
catch (SQLException e) {
System.err.println("An SQL exception occurred while
Inserting: " +e.getMessage());
}
}
private void processUpdate() {
String sql;
int nrows;
sql="update addressesdb set NAMES=? where NUMBERS=?";
try {
PreparedStatement psmt=conn.prepareStatement(sql);
psmt.setString(1,updNameField.getText());
psmt.setString(2,updNumField.getText());
nrows=psmt.executeUpdate();
psmt.close();
JOptionPane.showMessageDialog(null,"The database is
updated");
}
catch (SQLException e) {
System.err.println("An SQL exception occurred while
Updating: " +e.getMessage());
}
}
private void processDelete() {
String sql;
int nrows;
sql="delete from addressesdb where NUMBERS=?";
try {
PreparedStatement psmt=conn.prepareStatement(sql);
psmt.setString(1,delNumField.getText());
nrows=psmt.executeUpdate();
psmt.close();
JOptionPane.showMessageDialog(null,"The Number is
deleted");
}
catch (SQLException e) {
System.err.println("An SQL exception occurred while
Deleting: " +e.getMessage());
}
}
}