You are on page 1of 5

import

import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import

java.awt.Color;
java.awt.Font;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
java.sql.Connection;
java.sql.DriverManager;
java.sql.PreparedStatement;
java.sql.ResultSet;
java.sql.SQLException;
java.sql.Statement;
javax.swing.ImageIcon;
javax.swing.JButton;
javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JOptionPane;
javax.swing.JScrollPane;
javax.swing.JTable;
javax.swing.JTextField;
javax.swing.table.DefaultTableModel;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author H.P
*/
public class CreateResult extends JFrame implements ActionListener
{
String str;
JFrame jf;
JTextField t1,t2,t3,t4,t5,t6,t7,t8,tr,tq;
JLabel l1,l2,l3,l4,l5,l6,l7,l8;
JButton b0,b1,b2;
Font f;
Connection con;
PreparedStatement ps;
Statement stmt;
ResultSet rs;
DefaultTableModel model = new DefaultTableModel();
JTable tabGrid = new JTable(model);
JScrollPane scrlPane = new JScrollPane(tabGrid);
CreateResult()
{
jf=new JFrame();
f = new Font("Times New Roman",Font.BOLD,20);
jf.setLayout(null);
l6=new JLabel("Create Result");
l6.setFont(new Font("Times New Roman",Font.BOLD,25));
l6.setBounds(250,50,300,40);l6.setForeground(Color.blue);
jf.add(l6);
l1= new JLabel("Student Adm No. ");
l1.setBounds(150,120,130,25);
jf.add(l1);

t1=new JTextField(20);
t1.setBounds(300,120,130,25);t1.setToolTipText("Enter Student ad
m no.");
jf.add(t1);
l2 = new JLabel("Student name*");
l2.setBounds(150,160,170,25);
jf.add(l2);
t2=new JTextField(20);
t2.setBounds(320,160,150,25);t2.setToolTipText("Enter Student na
me");
jf.add(t2);
l3 = new JLabel("Test Type*");
//l3.setFont(f);
l3.setBounds(150,200,170,25);
jf.add(l3);
t3=new JTextField(20);
t3.setBounds(320,200,150,25);t3.setToolTipText("test type");
jf.add(t3);
l4 = new JLabel("English Marks*");
//l4.setFont(f);
l4.setBounds(150,260,170,25);
jf.add(l4);
t4=new JTextField(20);
t4.setBounds(320,260,100,25);t4.setToolTipText("Enter english ma
rks");
jf.add(t4);
l5 = new JLabel("Hindi Marks*");
//l5.setFont(f);
l5.setBounds(150,280,170,25);
jf.add(l5);
t5=new JTextField(20);
t5.setBounds(320,280,100,25);t5.setToolTipText("Enter Hindi Mark
s");
jf.add(t5);
l6= new JLabel ("Maths Marks*");
l6.setBounds(150, 300, 170,25);
jf.add(l6);
t6= new JTextField(20);
t6.setBounds(320,300,100,25);t6.setToolTipText("enter maths mark
s");
jf.add(t6);
l7= new JLabel ("Science Marks*");
l7.setBounds(150, 320, 170,25);
jf.add(l7);
t7= new JTextField(20);
t7.setBounds(320,320,100,25);t6.setToolTipText("enter science ma
rks");
jf.add(t7);

l8= new JLabel ("Maximum Marks*");


l8.setBounds(150, 340, 170,25);
jf.add(l8);
t8= new JTextField(20);
t8.setBounds(320,340,100,25);t6.setToolTipText("enter Maximum ma
rks");
jf.add(t8);

b0 = new JButton("Save",new ImageIcon("images//save.png"));


b0.setBounds(500,400,110,35);b0.setToolTipText("click to save Student re
sult");
jf.add(b0);b0.addActionListener(this);
b1= new JButton("open",new ImageIcon("images//save.png"));
b1.setBounds(700,400,110,35);b1.setToolTipText("click to open ");
jf.add(b1);b1.addActionListener(this);
b2= new JButton("clear",new ImageIcon("images//save.png"));
b2.setBounds(700,400,110,35);b2.setToolTipText("click to open ");
jf.add(b2);b2.addActionListener(this);
scrlPane.setBounds(0,500,900,600);
jf.add(scrlPane);
jf.setTitle("Create Result");
jf.setSize(900,700);
jf.setLocation(20,20);
jf.setResizable(false);
jf.getContentPane().setBackground(Color.orange);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b0)
{
String
String
String
String

s4=t4.getText();
s5=t5.getText();
s6=t6.getText();
s7=t7.getText();

String marksobtained =(s4+s5+s6+s7);


try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306
/student","root","always");
System.out.println("Connected to database.");
ps=con.prepareStatement("insert into result values(?,?,?,?,?,?,?,?,?
)");
ps.setString(1,t1.getText());

ps.setString(2,t2.getText());
ps.setString(3,t3.getText());
ps.setString(4,t4.getText());
ps.setString(5,t5.getText());
ps.setString(6,t6.getText());
ps.setString(7,t7.getText());
ps.setString(8,t8.getText());
ps.setString(9,marksobtained);
ps.executeUpdate();
}
catch(SQLException se)
{
System.out.println(se);
JOptionPane.showMessageDialog(null,"SQL Error:"+se);
}
catch(Exception e)
{
System.out.println(e);
JOptionPane.showMessageDialog(null,"Error:"+e);
}
}
if(ae.getSource()==b1)
{
if(((t1.getText()).equals(""))&&((t2.getText()).equals("
")))
{
JOptionPane.showMessageDialog(this,"Please enter Student adm
_no. or name !","Warning!!!",JOptionPane.WARNING_MESSAGE);
}
else
{//fetch
try
{
int foundrec = 0;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/stu
dent","root","always");
System.out.println("Connected to database.");
ps=con.prepareStatement("select * from result where adm_no='"+t1.getTex
t()+"' or name='"+t2.getText()+"'");
rs=ps.executeQuery();
while(rs.next())
{
t1.setText(rs.getString(1));
t2.setText(rs.getString(2));
foundrec = 1;
}
if (foundrec == 0)
{
JOptionPane.showMessageDialog(null,"Record is not available"
,"Dialog",JOptionPane.WARNING_MESSAGE);
}
con.close();

}
catch(SQLException se)
{
System.out.println(se);
JOptionPane.showMessageDialog(null,"SQL Error:"+se);
}
catch(Exception e)
{
System.out.println(e);
JOptionPane.showMessageDialog(null,"Error:"+e);
}
}
}
else if(ae.getSource()==b2)
{//clear
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
t6.setText("");
t7.setText("");
t8.setText("");

}
public static void main(String arg[])
{
new CreateResult();
}
}

You might also like