2. Covid Test Application Using JDBC

Open Apache NetBeans IDE

If you can findout in your computer if it install otherwise install required software Required software and API for JDBC

Click on File then New Project

You can use CTRL + Shift + N as a shortcut

Select Java with Ant then Java Application and Click on Next>


Please see the screenshot

Write Your Project Name and Uncheck Create Main Class

Please see the screenshot for more verification

You will see Project Hierarchy as per left screenshot

Now next step is to add MySql connector in Libraries for jdbc connectivity

Right Click on Libraries and Go to third option ( Add Jar/Folder)


You will get an option browse and select the location where mysql connection file is located , if you have not downloaded then please use the below link

Now You will able to see mysql connector jar file in Library Folder.


Now Environment is ready and lets work on Design

Right Click on Source Package then new and then select JFrame Form


Give JFrame name "CovidTest


Once you will Create JFrame then you will get a window to design GUI Interface.


Drag from Palette Window and Drop to JFrame ( 3 Labels, 3 TextFields, and 2 JButtons)


Now Your Design Part is ready


Creating Table in Database As per above Design

use rkmishradb

create table covidtest

( medicalid varchar(100) primary key,

pname varchar(100),

covidresult varchar(100)

)

Right click on Submit button then Events then Action then actionPerformed


Now you cursor will start blinking on jButtonActionPerformed()

//Lets write the Code on Submit Button


try{

// Loading Driver

Class.forName("com.mysql.jdbc.Driver");

//Creating Connection

Connection con=DriverManager.getConnection("jdbc:mysql://172.16.100.8/rkmishradb","rkmishra","a");

// Create Statement

Statement stmt =con.createStatement();

// Storing Values to String avraible

String mid=jTextField1.getText();

String pname=jTextField2.getText();

String result =jTextField3.getText();

// Execute Query

stmt.executeUpdate("insert into covidtest(medicalid,pname,covidresult)values('"+mid+"','"+pname+"','"+result+"')");

JOptionPane.showMessageDialog(null, "Data Successfully Saved");

}

catch(Exception e)

{

JOptionPane.showMessageDialog(null, "Something Wrong" + e);

}

Drag and Drop Table from ToolBox


Coding on Display Jbutton

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

try{

// Loading Driver

Class.forName("com.mysql.jdbc.Driver");

//Creating Connection

Connection con=DriverManager.getConnection("jdbc:mysql://172.16.100.8/rkmishradb","rkmishra","a");

// Create Statement

Statement stmt =con.createStatement();

// Execute Query

ResultSet rs= null;

rs=stmt.executeQuery("select *from covidtest");

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

tm.setRowCount(0);

while(rs.next())

{

Object o[]={rs.getString(1),rs.getString(2),rs.getString(3)};

tm.addRow(o);

}

}

catch(Exception e)

{

JOptionPane.showMessageDialog(null, "Something Wrong" + e);

}

}