Hi
I need help to insert IP Address values dynamically from jsp page into MS Access database, This is my code:
<body> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page language="java" import="java.sql.*"%> <title> Create Page </title> <h1 align="Center">Welcome to Create Item Page</h1> <form action="additemtodatabase.jsp" method="post"> <br><br><br> <TABLE align="center" width="300px" style="background-color:#EDF6EA;border:1px solid #000000;"> <tr><td colspan=2 style="font-weight:bold;" align="center">Create Item</td></tr> <tr><td colspan=2 align="center" height="10px"></td></tr> <tr> <td style="font-weight:bold;">IP Address</td> <td><input type="numeric" name="IPAddress" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">Subnet Mask</td> <td><input type="numeric" name="Subnetmask" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">VLAN ID</td> <td><input type="numeric" name="VLANID" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">Allocated Hostname</td> <td><input type="text" name="AllocatedHostname" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">Aggregated Switch</td> <td><input type="text" name="AggregatedSwitch" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">Headend Name</td> <td><input type="text" name="HeadedName" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">Date Issued</td> <td><input type="text" name="DateIssued" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">Remarks</td> <td><input type="text" name="Remarks" value="" size="20"></td> </tr> <tr> <td></td> <td><input type="submit" name="Submit" value="Save" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td> </tr> <tr><td colspan=2 align="center" height="10px"></td></tr> </TABLE> <% String IPAddress = request.getParameter("IPAddress"); String SubnetMask = request.getParameter("SubnetMask"); String VLANID = request.getParameter("VLANID"); String AllocatedHostname = request.getParameter("AllocatedHostname"); String AggregatedSwitch = request.getParameter("AggregatedSwitch"); String HeadendName = request.getParameter("HeadendName"); String DateIssued = request.getParameter("DateIssued"); String Remarks = request.getParameter("Remarks"); String connectionURL = "jdbc:odbc:Databaseipadd"; Connection connection = null; PreparedStatement pstatement = null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); int updateQuery = 0; if (IPAddress != null && SubnetMask != null && VLANID != null && AllocatedHostname != null && AggregatedSwitch != null && HeadendName != null && DateIssued != null && Remarks != null) { if (IPAddress != "" && SubnetMask != "" && VLANID != "" && AllocatedHostname != "" && AggregatedSwitch != "" && HeadendName != "" && DateIssued != "" && Remarks != "") { try { connection = DriverManager.getConnection(connectionURL); String queryString = "INSERT INTO iptable (IPAddress,SubnetMask,VLANID,AllocatedHostname,AggregatedSwitch,HeadendName,DateIssued,Remarks) VALUES (?, ?, ?, ? , ? , ? , ? , ? )"; pstatement = connection.prepareStatement(queryString); pstatement.setString(1, IPAddress); pstatement.setString(2, SubnetMask); pstatement.setString(3, VLANID); pstatement.setString(4, AllocatedHostname); pstatement.setString(5, AggregatedSwitch); pstatement.setString(6, HeadendName); pstatement.setString(7, DateIssued); pstatement.setString(8, Remarks); pstatement.executeUpdate(); %> <br> <TABLE style="background-color: #E3E4FA;" WIDTH="30%" border="1"> <tr><th>Data is inserted successfully in database.</th></tr> </TABLE> <% } catch (Exception ex) { out.println("Unable to connect to database"); } finally { pstatement.close(); connection.close(); } } } %> </form>
Please kindly correct the code if it is wrong, or help me to solve this problem, Its very urgent. Please help me.
Thanks Lissy
Hi
The code seems ok. But for all exceptions you are printing
out.println("Unable to connect to database");
You should actually print the exception message also.
You have to debug it. Probable it is giving some database error. You can find the error just by adding our.println() information.
out.println(ex.getMessage());
The about code will print the exception on the web page and then you can modify the SQL code and then resolve the error.
Here is the code with added statement:
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ page language="java" import="java.sql.*"%> <html> <body bgcolor="white"> <body> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <title> Create Page </title> <h1 align="Center">Welcome to Create Item Page</h1> <form action="additemtodatabase.jsp" method="post"> <br><br><br> <TABLE align="center" width="300px" style="background-color:#EDF6EA;border:1px solid #000000;"> <tr><td colspan=2 style="font-weight:bold;" align="center">Create Item</td></tr> <tr><td colspan=2 align="center" height="10px"></td></tr> <tr> <td style="font-weight:bold;">IP Address</td> <td><input type="numeric" name="IPAddress" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">Subnet Mask</td> <td><input type="numeric" name="Subnetmask" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">VLAN ID</td> <td><input type="numeric" name="VLANID" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">Allocated Hostname</td> <td><input type="text" name="AllocatedHostname" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">Aggregated Switch</td> <td><input type="text" name="AggregatedSwitch" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">Headend Name</td> <td><input type="text" name="HeadedName" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">Date Issued</td> <td><input type="text" name="DateIssued" value="" size="20"></td> </tr> <tr> <td style="font-weight:bold;">Remarks</td> <td><input type="text" name="Remarks" value="" size="20"></td> </tr> <tr> <td></td> <td><input type="submit" name="Submit" value="Save" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td> </tr> <tr><td colspan=2 align="center" height="10px"></td></tr> </TABLE> <% String IPAddress = request.getParameter("IPAddress"); String SubnetMask = request.getParameter("SubnetMask"); String VLANID = request.getParameter("VLANID"); String AllocatedHostname = request.getParameter("AllocatedHostname"); String AggregatedSwitch = request.getParameter("AggregatedSwitch"); String HeadendName = request.getParameter("HeadendName"); String DateIssued = request.getParameter("DateIssued"); String Remarks = request.getParameter("Remarks"); String connectionURL = "jdbc:odbc:Databaseipadd"; Connection connection = null; PreparedStatement pstatement = null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); int updateQuery = 0; if (IPAddress != null && SubnetMask != null && VLANID != null && AllocatedHostname != null && AggregatedSwitch != null && HeadendName != null && DateIssued != null && Remarks != null) { if (IPAddress != "" && SubnetMask != "" && VLANID != "" && AllocatedHostname != "" && AggregatedSwitch != "" && HeadendName != "" && DateIssued != "" && Remarks != "") { try { connection = DriverManager.getConnection(connectionURL); String queryString = "INSERT INTO iptable (IPAddress,SubnetMask,VLANID,AllocatedHostname,AggregatedSwitch,HeadendName,DateIssued,Remarks) VALUES (?, ?, ?, ? , ? , ? , ? , ? )"; pstatement = connection.prepareStatement(queryString); pstatement.setString(1, IPAddress); pstatement.setString(2, SubnetMask); pstatement.setString(3, VLANID); pstatement.setString(4, AllocatedHostname); pstatement.setString(5, AggregatedSwitch); pstatement.setString(6, HeadendName); pstatement.setString(7, DateIssued); pstatement.setString(8, Remarks); pstatement.executeUpdate(); %> <br> <TABLE style="background-color: #E3E4FA;" WIDTH="30%" border="1"> <tr><th>Data is inserted successfully in database.</th></tr> </TABLE> <% } catch (Exception ex) { out.println("Unable to connect to database"); out.println(ex.getMessage()); } finally { pstatement.close(); connection.close(); } } } %> </form> </body> </html>
Thanks
Ads