/* database connectivity with MS-Access is done by creating
DataSourceName(dsn) in this example*/
/* Steps :
* go to ms-access and make a table called "student_db" and give it a
file name student_db.mdb
* 1. Go to Control Panel
2. Click on Administrative Tools(windows 2000/xp)
3. click on ODBC Database source
4. Then , you will see a ODBC dialog box. Click on UserDSn
5. Click on Add Button
6. Select Microsoft Access Driver(*.mdb) driver and click on finish
7. Give a Data Source Name : student_db
8. Then Click on create
9. give the database name student_db.
give any path to the file
10. Click on OK.
Once the DSN is created, you can do this*/
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.ListSelectionModel.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class InputDialog extends JDialog implements ActionListener{
String s;
JTextField tf;
JButton b;
InputDialog(Frame parent,String title,boolean mode)
{
super(parent,title,mode);
setLayout(new FlowLayout());
setSize(150,100);
setLocation(550, 300);
add(tf = new JTextField(10));
add(b = new JButton("Ok"));
b.addActionListener(this);
show();
}
public String getInput()
{
return s;
}
public void actionPerformed(ActionEvent e)
{
s=tf.getText();
dispose();
}
}
public class JdbcApp extends JFrame implements ActionListener{
JButton BtnInsert,BtnUpdate,BtnShow;
JTextField TfRollno,TfName,TfMark;
JTable Table;
DefaultTableModel Model;
Panel PLeft,PRight;
Container ContentPane;
Connection Con;
int r,c;
JdbcApp()
{
ContentPane = getContentPane() ;
ContentPane.setLayout(new BorderLayout(2,5));
TfRollno = new JTextField(15);
TfName = new JTextField(15);
TfMark = new JTextField(15);
BtnInsert = new JButton("Insert") ;
BtnInsert.addActionListener(this);
PLeft = new Panel();
PLeft.setLayout(new GridLayout(7,1,2,2));
PLeft.add(new JLabel("Roll No:"));
PLeft.add(TfRollno);
PLeft.add(new JLabel("Name:"));
PLeft.add(TfName);
PLeft.add(new JLabel("Mark:"));
PLeft.add(TfMark);
PLeft.add(BtnInsert);
ContentPane.add(PLeft,BorderLayout.WEST);
BtnUpdate = new JButton("Update");
BtnUpdate.addActionListener(this);
BtnShow = new JButton("Show");
BtnShow.addActionListener(this);
PRight = new Panel();
PRight.setLayout(new FlowLayout());
PRight.add(BtnUpdate);
PRight.add(BtnShow);
ContentPane.add(PRight,BorderLayout.SOUTH);
Model = new DefaultTableModel();
Table = new JTable(Model){
public boolean isCellEditable(int row,int column)
{
return false;
}
};
Model.addColumn("Roll No");
Model.addColumn("Name");
Model.addColumn("Mark");
Table.setCellSelectionEnabled(true);
ListSelectionModel cellSelectionModel = Table.getSelectionModel();
cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
c=Table.getSelectedColumn();
r=Table.getSelectedRow();
}
});
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(Table,v,h);
ContentPane.add(jsp);
setSize(700,300);
setLocation(300,200);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
try{
//loading the driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//connection object created using DriverManager class
//student_base is the name of the database
Con = DriverManager.getConnection("jdbc:odbc:student_db");
//create a table student
Statement Stmnt = Con.createStatement();
Stmnt.executeUpdate("create table student (Rollno integer, Name varchar(30),Mark integer)");
}
catch(Exception e)
{
JDialog Ed = new JDialog(this,"Error",true);
Ed.add(new JLabel(e.toString(),JLabel.CENTER));
Ed.setSize(700, 100);
Ed.setLocation(350, 300);
Ed.show();
}
}
public void showMessage(String m)
{
JDialog Ed = new JDialog(this,"Message",true);
Ed.add(new JLabel(m,JLabel.CENTER));
Ed.setSize(700, 100);
Ed.setLocation(350, 300);
Ed.show();
}
public static void main(String args[])
{
JdbcApp app = new JdbcApp() ;
}
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getActionCommand()=="Update")
{
if(Table.isRowSelected(r))
{
InputDialog dialog = new InputDialog(this,"Enter new value",true);
if(!dialog.getInput().isEmpty())
{
Statement stmt = Con.createStatement();
stmt.executeUpdate("update student set " +Table.getColumnName(c)+ " =\'" + dialog.getInput() +"\' where Rollno=" + Table.getValueAt(r, 0) );
showMessage("One row updated successfully....");
}
}
}
else if(e.getActionCommand()=="Insert")
{
PreparedStatement pstm = Con.prepareStatement("insert into student values(?,?,?)");
pstm.setInt(1,Integer.parseInt(TfRollno.getText()));
pstm.setString(2,TfName.getText());
pstm.setInt(3,Integer.parseInt(TfMark.getText()));
pstm.executeUpdate();
TfRollno.setText("");
TfName.setText("");
TfMark.setText("");
showMessage("One row inserted successfully......");
}
else if(e.getActionCommand()=="Show")
{
int i =0;
Statement stmt = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("select Rollno,Name,Mark from student");
Model.setRowCount(0);
while(rs.next())
{
Model.insertRow(i,new Object[]{rs.getInt(1),rs.getString(2),rs.getInt(3)});
i++;
}
}
}
catch(Exception ex)
{
JDialog Ed = new JDialog(this,"Error",true);
Ed.add(new JLabel(ex.toString(),JLabel.CENTER));
Ed.setSize(700, 100);
Ed.setLocation(350, 300);
Ed.show();
}
}
}