import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.text.MaskFormatter;
import java.sql.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
class MyFrame extends JFrame implements ActionListener,MouseListener{
Container c;
JLabel lRollNo,lName,lAge,lMarks;
JTextField tRollNo,tSName,tMarks;
JTextField tAge;
DefaultTableModel mStud;
JTable tStud;
JButton bAdd,bNew,bRemove;
MyFrame(){
c=getContentPane();
setLayout(null);
setTitle("My Frame");
setSize(800,700);
setLocation(10,10);
lRollNo=new JLabel("Roll no");
lRollNo.setBounds(25,50,100,50);
c.add(lRollNo);
lName=new JLabel("Name");
lName.setBounds(25,150,100,50);
c.add(lName);
lAge=new JLabel("Age");
lAge.setBounds(25,250,100,50);
c.add(lAge);
lMarks=new JLabel("Marks");
lMarks.setBounds(25,350,100,50);
c.add(lMarks);
tRollNo=new JTextField();
tRollNo.setBounds(150,50,100,50);
c.add(tRollNo);
tSName=new JTextField();
tSName.setBounds(150,150,100,50);
c.add(tSName);
tMarks=new JTextField();
tMarks.setBounds(150,350,100,50);
c.add(tMarks);
tAge=new JTextField();
tAge.setBounds(150,250,100,50);
c.add(tAge);
String s[]={"RollNo","SName","Age","Marks"};
mStud=new DefaultTableModel(s,0);
tStud=new JTable(mStud);
JScrollPane jsp= new JScrollPane(tStud);
jsp.setBounds(315,0,400,500);
c.add(jsp);
fillTable();
bAdd=new JButton("Add");
bAdd.setBounds(230,525,150,50);
c.add(bAdd);
bAdd.addActionListener(this);
bNew=new JButton("New");
bNew.setBounds(420,525,150,50);
c.add(bNew);
bNew.addActionListener(this);
bRemove=new JButton("Remove");
bRemove.setBounds(610,525,150,50);
c.add(bRemove);
bRemove.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(3);
}
public void actionPerformed(ActionEvent ae){
Object o=ae.getSource();
if(o.equals(bAdd)){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb);DBQ=student.mdb");
PreparedStatement st=con.prepareStatement("insert into stud Values(?,?,?,?)");
st.setInt(1,Integer.parseInt(tRollNo.getText()));
st.setString(2,tSName.getText());
st.setInt(3,Integer.parseInt(tAge.getText()));
st.setInt(4,Integer.parseInt(tMarks.getText()));
st.executeUpdate();
[CODE][/CODE]con.close();
}
catch (Exception ex) {
}
Vector v=new Vector();
v.add(Integer.parseInt(tRollNo.getText()));
v.add(tSName.getText());
v.add(Integer.parseInt(tAge.getText()));
v.add(Integer.parseInt(tMarks.getText()));
mStud.addRow(v);
}
else if(o.equals(bNew)){
int n=(Integer)mStud.getValueAt(tStud.getSelectedRow(),0);
n++;
tRollNo.setText(String.valueOf(n));
tSName.setText("");
tAge.setText("");
tMarks.setText("");
}
else if(o.equals(bRemove)){
try{
int rollno=(Integer)mStud.getValueAt(tStud.getSelectedRow(),0);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb);DBQ=student.mdb");
PreparedStatement st=con.prepareStatement("delete from stud where rollno=?");
st.setInt(1,rollno);
int n=st.executeUpdate();
if(n>0)
JOptionPane.showMessageDialog(c,"delete");
else
JOptionPane.showMessageDialog(c,"sorry");
con.close();
}
catch(Exception ex){
}
}
}
void fillTable(){
try {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb);DBQ=student.mdb");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from stud");
while(rs.next())
{
Vector v=new Vector();
v.add(rs.getInt(1));
v.add(rs.getString(2));
v.add(rs.getInt(3));
v.add(rs.getInt(4));
mStud.addRow(v);
}
con.close();
}
catch (Exception ex) {
}
}
public void mousePressed(MouseEvent me){
}
public void mouseClicked(MouseEvent me){
Object e=me.getSource();
if(e.equals(tSName)) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection.con=DriverManager.getConnection("jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb);DBQ=student.mdb");
PreparedStatement st=con.prepareStatement("select sname from stud where rollno=?");
st.setInt(1,Integer.parseInt(tSName.getText()));
ResultSet rs=null;
rs=st.executeQuery();
con.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void mouseReleased(MouseEvent me){
}
public void mouseEntered(MouseEvent me){
}
public void mouseExit(MouseEvent me){
}
}
public class DemoDatabase{
public static void main(String[] args) {
new MyFrame();
}
}
harsimran05 0 Light Poster
Majestics 84 Posting Pro
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Majestics 84 Posting Pro
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.