Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Problem Logic

Practice
Solving Building
Advance Object Oriented Programming

CRUD - JDBC
Course Code: CSAO-267, Sem:4
Pre: CSOO-142-Sem:2
By Zohair Ahmed
1 Practice More
By Zohair Ahmed
Validation on Text Box & Dropdown/Combo Box

if(txtname.getText().trim().length()<1) {
JOptionPane.showMessageDialog(null, "Validation Error: Enter Name");
return;
}

Validate two Fields else if(cb_degree.getSelectedItem()== "Select"){


JOptionPane.showMessageDialog(null, "Validation Error: Select any option");
return;}
2

By Zohair Ahmed
Design the Form in JSwing
import javax.swing.*; f.add(txtname); lbl_degree.setSize(100, 20);
import java.awt.*; lbl_degree.setLocation(100, 250);
import java.awt.event.ActionEvent; f.add(lbl_degree);
import java.awt.event.ActionListener; JLabel lblclass = new JLabel("Class");
lblclass.setSize(100, 20); String
public class form implements ActionListener { lblclass.setLocation(100, 150); degrees[]={"Select","BSCS","BSIT","BSAI"};
f.add(lblclass);
private JTextField txtname; cb_degree =new JComboBox(degrees);
private JTextField txtclass; txtclass = new JTextField();
private JButton btn_submit; txtclass.setSize(190, 20); cb_degree.setSize(200, 20);
private JRadioButton rd_gender_male; txtclass.setLocation(200, 150); cb_degree.setLocation(200, 250);
private JRadioButton rd_gender_female; f.add(txtclass); f.add(cb_degree);
private JComboBox cb_degree;

private JButton btn_loadfromDB_cb_degree; JLabel lblgender = new JLabel("Gender"); btn_submit = new JButton("Submit");
lblgender.setSize(100, 20); btn_submit.setSize(100, 20);
public form() { lblgender.setLocation(100, 200); btn_submit.setLocation(150, 350);
f.add(lblgender); btn_submit.addActionListener(this);
JFrame f = new JFrame(); f.add(btn_submit);
rd_gender_male = new JRadioButton("Male");
JLabel title = new JLabel("NUML rd_gender_male.setSelected(true);
Registration"); rd_gender_male.setSize(75, 20); f.setSize(600,600);
title.setFont(new Font("Arial", Font.PLAIN, rd_gender_male.setLocation(200, 200); f.setLayout(null);
30)); f.add(rd_gender_male); f.setTitle("My APP");
title.setSize(300, 30); f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
title.setLocation(200, 30); rd_gender_female = new f.setVisible(true);
f.add(title); JRadioButton("Female");
rd_gender_female.setSelected(false);
rd_gender_female.setSize(80, 20); }}
rd_gender_female.setLocation(275, 200);
JLabel lblname= new JLabel("Name"); f.add(rd_gender_female);
lblname.setSize(100, 20);
lblname.setLocation(100, 100); ButtonGroup rd_gender_group = new
f.add(lblname); ButtonGroup();
rd_gender_group.add(rd_gender_male);
rd_gender_group.add(rd_gender_female);
txtname= new JTextField();
txtname.setSize(190, 20); JLabel lbl_degree = new JLabel("Select
txtname.setLocation(200, 100); Degree");

By Zohair Ahmed
Design the Dummy JTable on Form

String[] columnNames = {};


DefaultTableModel model = new
DefaultTableModel(null,columnNames);
tbl=new JTable(model) ;
tbl.setSize(500, 200);
tbl.setLocation(490, 100);
tbl.getTableHeader().setBounds(490, 50,700,50);
f.add(tbl.getTableHeader());
f.add(tbl);

By Zohair Ahmed
Insert Records in Database
public void std_gender, std_degree) statement.setString(4,
actionPerformed(ActionEven VALUES (?, ?, ?, ?)"; cb_degree.getSelectedItem(
t event) { PreparedStatement ).toString());
statement =
String dbURL = con.prepareStatement(qry_i int rowsInserted =
"jdbc:mysql://localhost:33 nsert); statement.executeUpdate();
06/std_db"; statement.setString(1, if (rowsInserted > 0) {
String username = "root"; txtname.getText()); JOptionPane.showMessageDia
String password = ""; statement.setString(2, log(null, "Inserted
txtclass.getText()); Records");
try { }
String rd_gender_select; }
if (event.getSource() ==
btn_submit) { if(rd_gender_male.isSelect else
ed()) {
Connection con = { JOptionPane.showMessageDia
DriverManager.getConnectio rd_gender_select="male"; log(null, "DB Connection
n(dbURL, username, } Error");
password); else }
if (con != null) { {
rd_gender_select="female"; }
String qry_insert = } }
"INSERT INTO tbl_std_info catch (Exception e) {
(std_name, std_class, statement.setString(3,rd_g e.printStackTrace();
ender_select); }
5

By Zohair Ahmed
Show Records in Database
else if ResultSetMetaData {
(event.getSource() == rs_meta= colname[i]=rs_meta.getC
btn_show) { rs.getMetaData(); olumnName(i+1);
}
Connection con =
DriverManager.getConnec DefaultTableModel tbl_model.setColumnIden
tion(dbURL, username, tbl_model= tifiers(colname);
password); (DefaultTableModel) //tbl.setModel(tbl_mode
if (con != null) { tbl.getModel(); l);
tbl_model.setRowCount(0
String qry_show = );// Removing All rows while (rs.next()) {
"Select * from on JTABLE before print
tbl_std_info"; String[] row=
Statement st = String[] colname= new {rs.getString(1),rs.get
con.createStatement(); String[rs_meta.getColum String(2),rs.getString(
ResultSet rs = nCount()]; 3),rs.getString(4),rs.g
st.executeQuery(qry_sho etString(5)};
w); for(int i =0; i tbl_model.addRow(row);
<rs_meta.getColumnCount
6 (); i++) }}}
By Zohair Ahmed
Before Deleting and Updating Records Get Records
tbl.addMouseListener(new MouseAdapter() {
public void txtclass.setText(tbl.getValueAt(tbl.getSelected
mouseClicked(MouseEvent e) { Row(), 2).toString());
std_id= if((tbl.getValueAt(tbl.getSelectedRow(),
Integer.parseInt(tbl.getValueAt(tbl.getSelected 3).toString()).equals("male"))
Row(), 0).toString()); {
//System.out.println(std_id);
btn_delete.setEnabled(true); rd_gender_male.setSelected(true);
btn_Update.setEnabled(true); }
else
{rd_gender_female.setSelected(true);}

txtname.setText(tbl.getValueAt(tbl.getSelectedR
ow(), 1).toString()); cb_degree.setSelectedItem(tbl.getValueAt(tbl.ge
tSelectedRow(), 4).toString());

Get ID when click on JTable Row


Set JTable value to Textbox for updating

By Zohair Ahmed
Delete and Update Records in Database
else if (event.getSource() == else if (event.getSource() ==
btn_delete) { btn_Update) {
Connection con =
Connection con = DriverManager.getConnection(dbURL,
DriverManager.getConnection(dbURL, username, password);
username, password); if (con != null) {
if (con != null) { String rd_gender_select;
if(rd_gender_male.isSelected())
String qry_delete = "delete from {
tbl_std_info where std_id="+std_id; rd_gender_select="male";
Statement st = con.createStatement(); }else
int result = {
st.executeUpdate(qry_delete); rd_gender_select="female";}

if (result > 0) { String qry_update = "update


tbl_std_info set
JOptionPane.showMessageDialog(null, std_name='"+txtname.getText()+"',
"Delete Records, Now Refersh Records"); std_class='"+txtclass.getText()+"',
} std_gender='"+rd_gender_select+"',
std_degree='"+cb_degree.getSelectedItem(
} ).toString()+"' where std_id="+std_id;
} Statement st = con.createStatement();
int result =
st.executeUpdate(qry_update);
if (result > 0) {
JOptionPane.showMessageDialog(null,
"Update Records, Now Refersh Records");
}}}
8

By Zohair Ahmed

You might also like