Name: Anonymous 2011-04-28 6:03
Hey, I'm getting a 500 internal server error when I try to do this, any help is appreciated.
(pre-code tl;dr - html form input calls jps1(this code), which should call jsp2, assuming the form input is not a duplicate entry for the sql table)
<code>
<%@page import="java.sql.*" %>
<%!
int ins = 0, not = 0;
boolean duplicate = false, inputerr = false;
String course, desc;
int credits;
PreparedStatement stmt;
%>
<%
//Database connection + query definition
String url = "jdbc:odbc:registrar";
Connection con;
String query = "SELECT count(*) FROM courses" + " WHERE course like ?;";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try
{
con = DriverManager.getConnection(url, "", "");
stmt = con.prepareStatement(query);
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
%>
<%
//Vars for database insert, pulled from HTML form input
course = request.getParameter("cID");
desc = request.getParameter("cDesc");
credits = Integer.parseInt(request.getParameter("credits"));
//Input validation
if (course == null || desc == null)
{
inputerr = true;
}
try
{
stmt.setString(1, course);
ResultSet rs = stmt.executeQuery();
rs.next();
boolean ok = (rs.getInt(1) == 0);
if (ok)
{
duplicate = false;
}
else if(! ok)
{
duplicate = true;
}
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
//Decision structure for JSP forward
if (duplicate == true || inputerr == true)
{
//Add to unsuccessful inserts
not += 1;
//Forward to JSP3
%>
<jsp:forward page="jsp3.jsp" />
<%
}
else
{
//Add to successful inserts
ins += 1;
//Forward to JSP2
%>
<jsp:forward page="jsp2.jsp" />
<%
}
%>
</code>
(pre-code tl;dr - html form input calls jps1(this code), which should call jsp2, assuming the form input is not a duplicate entry for the sql table)
<code>
<%@page import="java.sql.*" %>
<%!
int ins = 0, not = 0;
boolean duplicate = false, inputerr = false;
String course, desc;
int credits;
PreparedStatement stmt;
%>
<%
//Database connection + query definition
String url = "jdbc:odbc:registrar";
Connection con;
String query = "SELECT count(*) FROM courses" + " WHERE course like ?;";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try
{
con = DriverManager.getConnection(url, "", "");
stmt = con.prepareStatement(query);
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
%>
<%
//Vars for database insert, pulled from HTML form input
course = request.getParameter("cID");
desc = request.getParameter("cDesc");
credits = Integer.parseInt(request.getParameter("credits"));
//Input validation
if (course == null || desc == null)
{
inputerr = true;
}
try
{
stmt.setString(1, course);
ResultSet rs = stmt.executeQuery();
rs.next();
boolean ok = (rs.getInt(1) == 0);
if (ok)
{
duplicate = false;
}
else if(! ok)
{
duplicate = true;
}
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
//Decision structure for JSP forward
if (duplicate == true || inputerr == true)
{
//Add to unsuccessful inserts
not += 1;
//Forward to JSP3
%>
<jsp:forward page="jsp3.jsp" />
<%
}
else
{
//Add to successful inserts
ins += 1;
//Forward to JSP2
%>
<jsp:forward page="jsp2.jsp" />
<%
}
%>
</code>