Вы находитесь на странице: 1из 9

Q: Create a GUI application in Java to ADD, MODIFY, DELETE, SEARCH and LIST

registration details of students in a table STUDENT with fields such as Roll No, Name,
Grade and Section in GRADE12INFO database. The application should search for details
of student in the database with the given roll number. Also display the details of students
in a tabular format (using jTable) on the GUI form. Create the table with the given
specifications:
DESIGN:

On the click event of Add Button:

String roll="",name="",grade="",section="";

roll=jTextField1.getText();

name=jTextField2.getText();

grade=jTextField3.getText();

section=jTextField4.getText();

if (roll.isEmpty())

JOptionPane.showMessageDialog(null,"Enter a Roll Number");

return;

if (name.isEmpty())

JOptionPane.showMessageDialog(null,"Enter the Student's Name");

return;

if (grade.isEmpty())

{
JOptionPane.showMessageDialog(null,"Enter the Grade");

return;

if (section.isEmpty())

JOptionPane.showMessageDialog(null,"Enter the Section");

return;

try

Class.forName("java.sql.DriverManager");

Connection con=(Connection)DriverManager.getConnection

("jdbc:mysql://localhost:3306/GRADE12INFO","root","123");

Statement stmt=(Statement)con.createStatement();

String query="INSERT INTO STUDENT VALUES("+roll+",'"+name+"',"+grade+",'"+section+"');";

stmt.executeUpdate(query);

JOptionPane.showMessageDialog(null,"Record Added Successfully");

catch(Exception e)

JOptionPane.showMessageDialog(null,e.getMessage());

OUTPUT:
On the click event of Search Button:

String roll="",name="",grade="",section="";

roll=jTextField1.getText();

name=jTextField2.getText();

grade=jTextField3.getText();

section=jTextField4.getText();

if (roll.isEmpty())

JOptionPane.showMessageDialog(null,"Enter a Roll Number");

return;

try

Class.forName("java.sql.DriverManager");

Connection con=(Connection)DriverManager.getConnection

("jdbc:mysql://localhost:3306/grade12_2017","root","123");

Statement stmt=(Statement)con.createStatement();

String query="SELECT * FROM STUDENT WHERE RollNo="+roll+";";

ResultSet rs=stmt.executeQuery(query);

if (rs.next())

name=rs.getString("Name");

grade=rs.getString("Grade");

section=rs.getString("Section");

jTextField2.setText(name);

jTextField3.setText(grade);

jTextField4.setText(section);

JOptionPane.showMessageDialog(null,"Record Found");

else

JOptionPane.showMessageDialog(null,"Record Not Found");

}
catch(Exception e)

JOptionPane.showMessageDialog(null,e.getMessage());

OUTPUT:

On the click event of Delete Button:

String roll="";

roll=jTextField1.getText();

if (roll.isEmpty())

JOptionPane.showMessageDialog(null,"Enter a Roll Number");

return;

try

Class.forName("java.sql.DriverManager");

Connection con=(Connection)DriverManager.getConnection

("jdbc:mysql://localhost:3306/grade12_2017","root","123");

Statement stmt=(Statement)con.createStatement();

String query="DELETE FROM STUDENT WHERE RollNo="+roll+";";

stmt.executeUpdate(query);
jTextField1.setText("");

jTextField2.setText("");

jTextField3.setText("");

jTextField4.setText("");

JOptionPane.showMessageDialog(null,"Record Deleted Successfully");

catch(Exception e)

JOptionPane.showMessageDialog(null,e.getMessage());

OUTPUT:

On the click event of Modify Button:

String roll="",name="",grade="",section="";

roll=jTextField1.getText();

name=jTextField2.getText();

grade=jTextField3.getText();

section=jTextField4.getText();

if (roll.isEmpty())

JOptionPane.showMessageDialog(null,"Enter a Roll Number");

return;

if (name.isEmpty())
{

JOptionPane.showMessageDialog(null,"Enter the Name of the Student");

return;

if (grade.isEmpty())

JOptionPane.showMessageDialog(null,"Enter the Grade");

return;

if (section.isEmpty())

JOptionPane.showMessageDialog(null,"Enter the Section");

return;

try

Class.forName("java.sql.DriverManager");

Connection con=(Connection)DriverManager.getConnection

("jdbc:mysql://localhost:3306/grade12_2017","root","123");

Statement stmt=(Statement)con.createStatement();

String query="UPDATE STUDENT SET


Name='"+name+"',Grade="+grade+",Section='"+section+"' where RollNo="+roll+";";

stmt.executeUpdate(query);

JOptionPane.showMessageDialog(null,"Record Updated Successfully");

catch(Exception e)

JOptionPane.showMessageDialog(null,e.getMessage());

}
OUTPUT:

On the click event of List Button:

String roll="",name="",grade="",section="";

DefaultTableModel model=(DefaultTableModel) jTable1.getModel();

try

Class.forName("java.sql.DriverManager");

Connection con=(Connection)DriverManager.getConnection

("jdbc:mysql://localhost:3306/grade12_2017","root","123");

Statement stmt=(Statement) con.createStatement();

String query="SELECT * FROM STUDENT;";

ResultSet rs=stmt.executeQuery(query);

while(rs.next())

roll=rs.getString("RollNo");

name=rs.getString("Name");

grade=rs.getString("Grade");

section=rs.getString("Section");

model.addRow(new Object[] {roll,name,grade,section});

catch (Exception e)
{

JOptionPane.showMessageDialog(null,e.getMessage());

OUTPUT:

On the click event of Clear Table Button:

DefaultTableModel model=(DefaultTableModel) jTable1.getModel();

int rows=model.getRowCount();

if (rows>0)

for (int i=0;i<rows;i++)

model.removeRow(0);

OUTPUT:
On the click event of clear:

jTextField1.setText("");

jTextField2.setText("");

jTextField3.setText("");

jTextField4.setText("");

On the click event of close:

System.out.print(0);

Вам также может понравиться