Student information using JDBC.(File must be save as: StudentInfo.java)
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class StudentInfo extends JFrame implements ActionListener
{
JButton button1=new JButton("INSERT");
JButton button2=new JButton("DELETE");
JButton button3=new JButton("UPDATE");
JButton button4=new JButton("SEARCH");
JButton button5=new JButton("DISPLAY");
JLabel lblroll=new JLabel("Roll : ");
JLabel lblname=new JLabel("Name : ");
JLabel lblmark=new JLabel("Mark : ");
JTextField roll=new JTextField("",10);
JTextField name=new JTextField("",10);
JTextField mark=new JTextField("",10);
GridBagConstraints c=new GridBagConstraints();
GridBagLayout layout=new GridBagLayout();
public StudentInfo()
{
super("Sudents informatiom");
setLayout(layout);
c.insets=new Insets(0,40,0,0);
addComponent(lblroll,0,0);
c.insets=new Insets(0,0,0,50);
addComponent(roll,1,0);
c.insets=new Insets(0,40,0,0);
addComponent(lblname,0,1);
c.insets=new Insets(0,0,0,50);
addComponent(name,1,1);
c.insets=new Insets(0,40,0,0);
addComponent(lblmark,0,2);
c.insets=new Insets(0,0,0,50);
addComponent(mark,1,2);
c.insets=new Insets(10,0,0,0);
addComponent(button1,0,3);
button1.addActionListener(this);
c.insets=new Insets(10,0,0,80);
addComponent(button2,1,3);
button2.addActionListener(this);
c.insets=new Insets(10,0,0,0);
addComponent(button3,0,4);
button3.addActionListener(this);
c.insets=new Insets(10,0,0,80);
addComponent(button4,1,4);
button4.addActionListener(this);
c.insets=new Insets(10,160,0,0);
addComponent(button5,0,5);
button5.addActionListener(this);
}
public void addComponent(Component component,int x,int y)
{
c.gridx=x;
c.gridy=y;
layout.setConstraints(component,c);
add(component);
}
public void actionPerformed(ActionEvent ac)
{
if(ac.getActionCommand()=="INSERT")
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:Atic");
PreparedStatement stat=con.prepareStatement("INSERT into AticInfo values(?,?,?)");
stat.setString(1,roll.getText());
stat.setString(2,name.getText());
stat.setString(3,mark.getText());
stat.executeUpdate();
System.out.println("Data hava been successfully inserted");
}
catch(Exception e)
{
System.out.println(" Error : "+e);
}
}
if(ac.getActionCommand()=="DISPLAY")
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:Atic","","");
Statement stat=con.createStatement();
ResultSet rs = stat.executeQuery("SELECT * FROM AticInfo");
while (rs.next())
{
String a = rs.getString("Roll");
String b = rs.getString("Name");
String c = rs.getString("Mark");
System.out.println("Roll=" +a);
System.out.println("Name= " +b);
System.out.println("Mark= " + c);
System.out.println();
}
}
catch(Exception e)
{
System.out.println("Can not retrieve data: "+e);
}
}
if(ac.getActionCommand()=="DELETE")
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:Atic","","");
PreparedStatement stat=con.prepareStatement("DELETE from AticInfo where roll=?");
stat.setString(1,roll.getText());
stat.executeUpdate();
System.out.println("Delet is successful");
}
catch(Exception e)
{
System.out.println("Error :"+e);
}
}
if(ac.getActionCommand()=="UPDATE")
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection=DriverManager.getConnection("jdbc:odbc:Atic","","");
PreparedStatement stat=connection.prepareStatement("UPDATE AticInfo set name=?,mark=? where Roll=?");
stat.setString(1, roll.getText());
stat.setString(2, name.getText());
stat.setString(3, mark.getText());
stat.executeUpdate();
System.out.println("Data updated Successfully !!!");
}
catch(Exception e)
{
System.out.println("Can not update data: "+e);
}
}
if(ac.getActionCommand()=="SEARCH");
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:Atic","","");
PreparedStatement stat=con.prepareStatement("SELECT * FROM AticInfo where roll=?");
stat.setString(1,roll.getText());
stat.executeQuery();
ResultSet rs = stat.executeQuery();
while (rs.next()) {
String a = rs.getString("roll");
String b = rs.getString("name");
String c = rs.getString("mark");
System.out.println("Roll =" +a);
System.out.println("Name= " +b);
System.out.println("Mark= " + c);
System.out.println();
}
}
catch(Exception e)
{
System.out.println("Error: search data: "+e);
}
}
}
public static void main(String a[])
{
StudentInfo studentInfo=new StudentInfo();
studentInfo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
studentInfo.setVisible(true);
studentInfo.setSize(450,300);
}
}
////////////////////////////////////////////
Copy & paste this code in your Textpad & run, then you will get output.......
If you have any problem please comment below.........
Copy & paste this code in your Textpad & run, then you will get output.......
If you have any problem please comment below.........