Thanks in advance.. :-)
JDBC 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 with jdbc code.. It will create table in MS access database. Here student is our dsn.
import java.sql.*; public class CreateTable { public static void main(String[] args) throws Exception { Connection conn = DriverManager.getConnection("jdbc:odbc:student"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Statement st = conn.createStatement(); st.executeUpdate("create table employee (empid int,empname varchar(30));"); System.out.println("Table is created successfully!"); st.executeUpdate("insert into employee (empid,empname ) values (1,'Roseindia')"); System.out.println("Data is inserted successfully!"); st.close(); conn.close(); } }
Create table in MS Access Database manually.
Follow these steps:
1)Open your MS Access Database. You will get a prompt box, select your dsn from there.
2)Then double click the Create table in Design view.
3)Fill out the details in the "Field Name" column and the "Data Type" column.
4)Click the "Save" icon, enter the table name ("Individual"), and click "OK".
5)When prompted to set a primary key, click "Yes", if you want to create, otherwise click No. (A primary key ensures that the data in this column is unique - no two values can be the same. This is important for when you need to select or reference data from this column).
Servlet Program to insert the data into database table Individuals which consists of two fields name and address(suppose).
import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class DataInsertion extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:odbc:student"); Statement statement = conn.createStatement(); String query = "insert into Individual values('roseindia', 'Rohini')"; int i = statement.executeUpdate(query); if(i!=0){ out.println("The record has been inserted"); } else{ out.println("Sorry! Failure"); } statement.close(); } catch (Exception e){ System.out.println(e); } } }
Servlet example that fetch the data from the table 'Individual' and display the data on the browser.
import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletFetchingDataFromDatabase extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ response.setContentType("text/html"); PrintWriter pw = response.getWriter(); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:odbc:student"); Statement st = connection.createStatement(); ResultSet rs = st.executeQuery("Select * from Individual"); while(rs.next()){ pw.println("Name" + " " + "Address" + "<br>"); pw.println(rs.getString(1) + " " + rs.getString(2) + "<br>"); } } catch (Exception e){ pw.println(e); } } }
Ads