how to access the MS ACCESS database with java how can we insert,delete,update,search records of ms access with java
Java MS Access database connectivity
Follow these steps:
1)Go to the start->Control Panel->Administrative Tools-> data sources.
2)Click Add button and select the driver Microsoft Access Driver(*.mdb).
3)After selecting the driver, click finish button.
4)Then give Data Source Name and click ok button.
5)Your DSN will get created.
6) Restart your compiler and compile your java code.
For Inserting Data:
import java.sql.*; class AccessDatabase{ public static void main(String[] args){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:student"); Statement st=con.createStatement(); String name="roseindia"; String address="delhi"; int i=st.executeUpdate("insert into user(name,address) values('"+name+"','"+address+"')"); System.out.println("Row is added"); } catch(Exception e){ System.out.println(e); } } }
For deleting data:
import java.sql.*; class AccessDatabase{ public static void main(String[] args){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:student"); Statement st=con.createStatement(); String name="roseindia"; String address="delhi"; int i=st.executeUpdate("delete from user where id=4"); System.out.println("Row is deleted"); } catch(Exception e){ System.out.println(e); } } }
continue..
For updating data:
import java.sql.*; class AccessDatabase{ public static void main(String[] args){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:student"); Statement st=con.createStatement(); String name="roseindia"; String address="delhi"; int i=st.executeUpdate("update user set name='rose', address='rohini' where id=5"); System.out.println("Row is updated"); } catch(Exception e){ System.out.println(e); } } }
For searching data:
import java.sql.*; class AccessDatabase{ public static void main(String[] args){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:student"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from user where id=5"); while(rs.next()){ System.out.println(rs.getString("name")+" "+rs.getString("address")); } } catch(Exception e){ System.out.println(e); } } }
update code is not working ,while compiling not shows any errors , but while running the code it shows "syntax error in update function" error
import java.sql.*;
class InsertValues{ public static void main(String args[]) {
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:student"); Statement stmt=con.createStatement(); System.out.println("Connetion establish");
stmt.executeUpdate("create table Employees (" + "ID INTEGER, " +"Name VARCHAR(30))"); System.out.println("Table Created"); stmt.executeUpdate("insert into Employees"+ "(ID,Name)"+ " values(1234, 'Shawn')"); stmt.executeUpdate("insert into Employees"+ "(ID,Name)"+ " values(2, 'Shawn')"); System.out.println("Row insert"); ResultSet message = stmt.executeQuery("select * from Employees "); message.next(); System.out.println(message.getString(2)); stmt.executeUpdate("delete from Employees where id=2"); stmt.executeUpdate("update Employees set name='rose' where id=2"); System.out.println("Row is updated"); stmt.executeUpdate("delete from Employees where id=2"); stmt.close(); con.close(); }
catch (Exception s){ System.out.println(" not executed!"); } } }
Ads