1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

jsp coding for access connectivity

Discussion in 'Meet and Greet' started by Mirunalini, Mar 9, 2011.

  1. Mirunalini

    Mirunalini New Member

    Joined:
    Mar 9, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    hi
    everyone

    i was trying to connect jsp pages with ms access database but while trying the following code
    insertion is done successfully but update and deletion s returning the else part of it..
    can anyone help me out regarding this:
    <%@ page language="java" import="java.sql.*"%>
    <%
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
    PreparedStatement pst = null;
    int no = Integer.parseInt(request.getParameter("NO"));
    String name = request.getParameter("NAME");
    String address = request.getParameter("ADDRESS");
    String btn = request.getParameter("btn");
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con = DriverManager.getConnection("jdbc:eek:dbc:student");
    st = con.createStatement();
    rs = st.executeQuery("select * from student");
    if (btn != null) {
    if (btn.equals("Insert")) {
    int k = 0;
    while (rs.next()) {
    if (rs.getInt(1) == no) {
    k = 1;
    }
    }
    if (k == 0) {
    pst = con.prepareStatement("insert into student values (?,?,?)");
    pst.setInt(1, no);
    pst.setString(2, name);
    pst.setString(3, address);
    int i = pst.executeUpdate();
    %>
    <%
    if (i > 0) {
    out.println("record inserted succesfully");
    } else {
    out.println("record not inserted");
    }%>
    <%
    } else {
    out.println("record alredy exist");
    }
    }
    else if (btn.equals("update")) {
    pst = con.prepareStatement("update student set name=? ,address=? where no=?");
    pst.setInt(3, no);
    pst.setString(1, name);
    pst.setString(2, address);
    int j = pst.executeUpdate();
    %>
    <% if (j > 0) {
    out.println("updated");
    } else {
    out.println("not updated");
    }%>
    <% }
    else if (btn.equals("delete")) {
    pst = con.prepareStatement("delete from student where no=?");
    pst.setInt(1, no);
    int l = pst.executeUpdate();%>
    <%if (l > 0) {
    out.println("deleted");
    } else {
    out.println("record not found");
    }%>
    <% }
    else
    {
    out.println("not valid action");
    }
    }
    else
    {
    out.println("Specify the action");
    }
    %>
    <table border="0" bgcolor="lightgreen" cellspacing="5" cellpadding="2">
    <tr><th>No</th><th>Name</th><th>Address</th></tr>
    <%
    st = con.createStatement();
    rs = st.executeQuery("select * from student");
    while (rs.next()) {
    %>
    <tr>
    <td><%=rs.getInt(1)%></td>
    <td><%=rs.getString(2)%></td>
    <td><%=rs.getString(3)%></td>
    </tr>
    <% }
    %>
    </table>
    this s my code and i am trying this in netbeans 6.8
    and using tom cat server
     

Share This Page