Java Program which Demonstrates all type of Queries using User Inputs
package normal.question5;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class BankApp {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
static Connection connection;
static Statement statement;
private JTextField textField_4;
public static void main(String[] args) {
BankApp window = new BankApp();
window.frame.setVisible(true);
public BankApp() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblName = new JLabel("Name");
frame.getContentPane().add(lblName);
textField = new JTextField();
frame.getContentPane().add(textField);
JLabel lblNewLabel = new JLabel("Account Number");
frame.getContentPane().add(lblNewLabel);
textField_1 = new JTextField();
frame.getContentPane().add(textField_1);
JLabel lblBalance = new JLabel("Balance");
frame.getContentPane().add(lblBalance);
textField_2 = new JTextField();
frame.getContentPane().add(textField_2);
JLabel lblPhone = new JLabel("Phone");
frame.getContentPane().add(lblPhone);
textField_3 = new JTextField();
frame.getContentPane().add(textField_3);
JLabel lblAddress = new JLabel("Address");
frame.getContentPane().add(lblAddress);
final JTextArea textArea = new JTextArea();
frame.getContentPane().add(textArea);
JButton btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name=textField.getText();
int phone=Integer.valueOf(textField_3.getText());
int balance=Integer.valueOf(textField_2.getText());
String address=textArea.getText();
int ac_id=Integer.valueOf(textField_1.getText());
int acc=Integer.valueOf(textField_4.getText());
try
{
String query="UPDATE Customer SET Account_ID =
"+ac_id+" , Customer_Name = '"+name
+"', Balance = "+balance+" , Phone =
"+phone+" , Address = '"+address+"' WHERE Account_ID="+acc+"";
statement = (Statement)
connect().createStatement();
statement.executeUpdate(query);
}
catch(Exception e)
{
System.out.println(e);
}
}
});
frame.getContentPane().add(btnUpdate);
JScrollPane scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane);
final JTextArea textArea_1 = new JTextArea();
scrollPane.setViewportView(textArea_1);
textArea_1.setEditable(false);
JButton btnNewButton = new JButton("Delete");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int ac_id=Integer.valueOf(textField_4.getText());
textArea_1.setText("");
String query="DELETE FROM Customer WHERE
Account_ID="+ac_id;
statement = (Statement)
connect().createStatement();
statement.executeUpdate(query);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(531, 103, 70, 25);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Insert");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name=textField.getText();
int phone=Integer.valueOf(textField_3.getText());
int balance=Integer.valueOf(textField_2.getText());
String address=textArea.getText();
int ac_id=Integer.valueOf(textField_1.getText());
try
{
String query="INSERT INTO Customer
(Account_ID,Customer_Name,Balance,Phone,Address) VALUES ('"+ac_id+"','"+
name+"','"+balance+"','"+phone+"','"+address+"')";
statement = (Statement)
connect().createStatement();
statement.executeUpdate(query);
}
catch(Exception e)
{
System.out.println(e);
}
}
});
frame.getContentPane().add(btnNewButton_1);
JButton btnShow = new JButton("Show");
btnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String query="SELECT * FROM Customer WHERE (Balance
> 20000)";
statement = (Statement)
connect().createStatement();
statement.execute(query);
ResultSet rs = statement.getResultSet();
String str="";
while(rs.next())
{
str+="Acc No "+rs.getString(1)+"\n";
str+="Name : "+rs.getString(2)+"\n";
str+="Balance : "+rs.getString(3)+"\n";
str+="Phone: "+rs.getString(4)+"\n";
str+="Address: "+rs.getString(5)+"\n";
str+="===============================\n";
textArea_1.setText(str);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
btnShow.setBounds(531, 78, 70, 25);
frame.getContentPane().add(btnShow);
textField_4 = new JTextField();
textField_4.setBounds(517, 30, 84, 20);
frame.getContentPane().add(textField_4);
textField_4.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Account ID");
frame.getContentPane().add(lblNewLabel_1);
JButton buttonClear = new JButton("Clear");
buttonClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea_1.setText("");
}
});
frame.getContentPane().add(buttonClear);
}
private static Connection connect()
{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:BankAppDB";
String username = "";
String password = "";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
System.out.println(e.toString());
}
return null;
}
0 comments:
Post a Comment