This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes Jsp connect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Reply locked New topic
Author

Jsp connect

matthew irving
Ranch Hand

Joined: Apr 28, 2013
Posts: 38
I have two jsps one is for the question and the other is to check the result. The first randomly retrieves a noun and its gender from a database and then displays the noun. I then want the second page to be able to retrieve the variable and compare it and the users answer against the database. However i keep getting the same error that it cannot connect in spite of the fact that the code to connect to the db is the same. The error is below:

HTTP Status 500 -

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.RuntimeException: Cannot connect the database!

root cause

java.lang.RuntimeException: Cannot connect the database!

root cause

java.sql.SQLException: Column Index out of range, 4 > 2.

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2.2 logs.
GlassFish Server Open Source Edition 3.1.2.2



qugenpage.jsp

<%@ page language ="java" import="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*" %>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="teststyle.css">

<title>Noun Gender Question</title>
</head>
<br>
<h1>Noun Gender Question</h1><br>
Welcome <%= session.getAttribute( "userid" ) %>
<br>
<%
try {
String connectionURL = "jdbc:mysql://localhost:3306/language";
// declare a connection by using Connection interface
Connection connection = null;
// Load JBBC driver "com.mysql.jdbc.Driver".
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes parameters of
string type connection url, user name and password to connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "password");
Statement st = connection.createStatement();// sql query to retrieve values from the specified table.
ResultSet rs = null;
String QueryString = "SELECT * FROM nouns "
+ "ORDER BY RAND() "
+ "LIMIT 1";
rs = st.executeQuery(QueryString);
while (rs.next()) {

%>
The gender of the German noun <%=rs.getString(2)%> is : <br>
<%
String germannoun = rs.getString(2);
out.println("Unable to connect to database here.");
application.setAttribute( "german", germannoun );
}

rs.close();
st.close();
connection.close();
} catch (Exception ex) {
%>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
</font>
<form action="qugen.jsp" method="post">
<table>
<tr>
<td><input type="text" name="noungender" ></td><br>
</tr>
<tr>
<td><input type="submit" name="b3" value="submit"></td>
</tr>
</table>
</form>
<br>
<a href="Mainmenu.html">Return to main menu</a>
</body>
</html>


qugen.jsp

<%@ page language ="java" import="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*" %>
<%
session.getAttribute("german");

String gendernoun = request.getParameter("nounenglish");
String german = request.getParameter("german");
session.putValue("german", german);

try {
String connectionURL = "jdbc:mysql://localhost:3306/language";
// declare a connection by using Connection interface
Connection connection = null;
// Load JBBC driver "com.mysql.jdbc.Driver".
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes parameters of
string type connection url, user name and password to connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "password");
// check weather connection is established or not by isClosed() method
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select germannoun,gender from nouns");

while (rs.next()) {
String id = rs.getString(2);
String germannoun = rs.getString(2);
String english = rs.getString(2);
String gender = rs.getString(4);
if (german.equals(germannoun) && gendernoun.equals(gender)) {

%>
<jsp:forward page="random.jsp"/>
<%
} else {
out.println("Unable to connect to database.");
%>
<jsp:forward page="random.jsp"/>
<%
}
}

} catch(Exception e) {
throw new RuntimeException("Cannot connect the database!", e);
} finally {
System.out.println("Closing the connection.");
}

%>



What am i doing wrong?
matthew irving
Ranch Hand

Joined: Apr 28, 2013
Posts: 38
The code now returns a cannot connect exception and i cant get the connection to work if I input a value in qugenpage.jsp and submit it:

HTTP Status 500 -

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.RuntimeException: Cannot connect the database!

root cause

java.lang.RuntimeException: Cannot connect the database!

root cause

java.lang.NullPointerException

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2.2 logs.
GlassFish Server Open Source Edition 3.1.2.2

qugenpage.jsp



qugen.jsp

Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Please do not post the same question multiple times. I am closing this one, lets continue the discussion here.


SCJP, SCWCD.
|Asking Good Questions|
 
Consider Paul's rocket mass heater.
 
subject: Jsp connect
 
Similar Threads
retreiving all rows from a mysql table to a jsp page
Comparing value of radio button against answer from the database
Maintain a variable across jsp pages
Login Code Error
Distinct names for multiple buttons