Write JDBC program to insert the detail of college student in the MS-Access database.

/**
/* database connectivity with MS-Access is done by creating
DataSourceName(dsn) in this example*/
/* Steps to use this example:
* go to ms-access and make a database called "student_base" and create
table named student_base.mdb
* 1. Go to Control Panel
2. Click on Administrative Tools(windows 2000/xp), Click on
ODBC(win98)
OR if u have (windows 7) than go to C:\Windows\SysWOW64\odbcad32.exe
3. click on ODBC
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_base
8. Then Click on Select
9. Browse on the database addItemDB.mdb file on your disk by downloading it link provided..
will be stored
10. Click on OK.
Once the DSN is created, you can do this example*/
//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class addItemToDatabase extends JFrame
{
//Initializing Components
private JTextField inputs[];
private JButton add, reset;
private JLabel labels[];
private String fldLabel[] = {"First Name: ","Last Name: ","Branch","Enroll-no "};
private JPanel p1;
Connection con;
Statement st;
ResultSet rs;
String db;
//Setting up GUI
public addItemToDatabase()
{
//Setting up the Title of the Window
super("Adding Data to the Database");
//Set Size of the Window (WIDTH, HEIGHT)
setSize(300,180);
//Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Constructing Components
inputs = new JTextField[4];
labels = new JLabel[4];
add = new JButton("Add");
reset = new JButton("Reset");
p1 = new JPanel();
//Setting Layout on JPanel 1 with 5 rows and 2 column
p1.setLayout(new GridLayout(5,2));
//Setting up the container ready for the components to be added.
Container pane = getContentPane();
setContentPane(pane);
//Setting up the container layout
GridLayout grid = new GridLayout(1,1,0,0);
pane.setLayout(grid);
//Creating a connection to MS Access and fetching errors using "try-catch" to check if it is successfully connected or not.
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//db = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=addItemDB.mdb;";
con = DriverManager.getConnection("jdbc:odbc:student_base");
st = con.createStatement();
JOptionPane.showMessageDialog(null,"Successfully Connected to Database","Confirmation", JOptionPane.INFORMATION_MESSAGE);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Failed to Connect to Database","Error Connection", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
//Constructing JLabel and JTextField using "for loop" in their desired order
for(int count=0; count<inputs.length && count<labels.length; count++) {
labels[count] = new JLabel(fldLabel[count]);
inputs[count] = new JTextField(20);
//Adding the JLabel and the JTextFied in JPanel 1
p1.add(labels[count]);
p1.add(inputs[count]);
}
//Implemeting Even-Listener on JButton add
add.addActionListener(new ActionListener()
{
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event)
{

if (inputs[0].getText().equals("") || inputs[1].getText().equals("") || inputs[2].getText().equals("") || inputs[0].getText() == null || inputs[1].getText() == null || inputs[2].getText() == null)
JOptionPane.showMessageDialog(null,"Fill up all the Fields","Error Input", JOptionPane.ERROR_MESSAGE);
else
try
{
String add = "insert into student (firstName,LastName,Branch,Enroll_no) values ('"+inputs[0].getText()+"','"+inputs[1].getText()+"','"+inputs[2].getText()+"',"+inputs[3].getText()+")";
st.execute(add); //Execute the add sql
Integer.parseInt(inputs[3].getText()); //Convert JTextField Age in to INTEGER
JOptionPane.showMessageDialog(null,"Item Successfully Added","Confirmation", JOptionPane.INFORMATION_MESSAGE);
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter an integer on the Field Enroll-no","Error Input", JOptionPane.ERROR_MESSAGE);
}
catch (Exception ei)
{
JOptionPane.showMessageDialog(null,"Failure to Add Item. Please Enter a number on the Field Enroll-no","Error Input", JOptionPane.ERROR_MESSAGE);
}
}
}
);
//Implemeting Even-Listener on JButton reset
reset.addActionListener(new ActionListener()
{
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event) {
inputs[0].setText(null);
inputs[1].setText(null);
inputs[2].setText(null);
inputs[3].setText(null);
}
}
);
//Adding JButton "add" and "reset" to JPanel 1 after the JLabel and JTextField
p1.add(add);
p1.add(reset);
//Adding JPanel 1 to the container
pane.add(p1);
/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);
}
//Main Method
public static void main (String[] args) {
addItemToDatabase aid = new addItemToDatabase();
}
}
Posted By Kaushal Prajapti

Make Comments..!!


Sarang Vandara
It should be executeUpdate() instead of only execute..
only execute() returns multiple result but for INSERT, UPDATE, or DELETE executeUpdate() is used..
refer:http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/Statement.html
Like · Comment ·
Joydip Panchal likes this
Sarang Vandara
u ws ri8 but here execute() returns boolean value which is nt suitable..
Like · 1 ·
Shaily Shah
Thank you!!!!
Like · Comment ·
Arpan Patel and Moni Kachhadiya like this
Moni Kachhadiya
thank you so much... bt still m looking for other que.. if possible then plez upload
Like · 1 ·
Joydip Panchal
ya..we will try!!
Like · 1 ·
Download Android App