Write a GUI application that connects to the CoffeeDB database used in the lab.
ID: 3715697 • Letter: W
Question
Write a GUI application that connects to the CoffeeDB database used in the lab. The application should allow the user to insert a new row into the Customer table.
(1) Create a GUI as the following screenshot. Name each of the components properly.
(2) Implement the Action Listener of the button. When the button is pressed, insert the data into theCustomer table.
(3) If the record has been successfully inserted, show a message.
(4) If the insertion is sucessful, reset all the fields to empty.
(5) If the insertion is not successful, also show a message to inform the user.
Use
JFrame, JLabel, JTextField, and JButton are used, and named properly. (b) Database connection is created and closed.
SQL commands are executed.
The Action Listener (Inner Class) of the JButton is implemented
Explanation / Answer
package lasr;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Coffee extends JFrame {
Statement st ;
public Coffee(){
Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","shashank_shukla","shashank");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JLabel name, id, age, res;
JTextField nameT, idT, ageT;
setLayout(new GridLayout());
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jp = new JPanel();
name = new JLabel("Name");
age = new JLabel("age");
id = new JLabel("id");
res = new JLabel();
nameT = new JTextField(30);
idT = new JTextField(30);
ageT = new JTextField(30);
jp.add(id);
jp.add(idT);
jp.add(name);
jp.add(nameT);
jp.add(age);
jp.add(ageT);
this.add(jp);
try {
st = con.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JButton b = new JButton("Insert");
jp.add(b);
jp.add(res);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String nam = nameT.getText();
String idd = idT.getText();
int agg = Integer.parseInt(ageT.getText());
String s = "insert into Customer values('"+idd+"', '"+nam+"','"+agg+"')";
int r = 0;
try {
r = st.executeUpdate(s);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(r!=0)
{
res.setText("Data Inserted");
nameT.setText("");
idT.setText("");
ageT.setText("");
}
else
{
res.setText("Insertion Failed");
}
}
});
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Coffee c = new Coffee();
c.setVisible(true);
}
}
Make sure table is created-
create table customer
(
id varchar2(50),
name varchar2(50),
age number(2)
)
If there is anything that you do not understand, then please mention it in the comments.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.