Reputation: 153
I am asking for a user input in the index.jsp and I take that input to a different search.jsp where I search the database with given query. However, it's not connecting to database. Can someone please help me.
Here is my code in index.jsp that asks for user input
<form method="searchh" action="search.jsp">
<table>
<tr>
<td><b class="accent">Enter College Name: </b> </td>
<td><input type="text" name="college" STYLE="color: #f4d442; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;" size="10" maxlength="30"></td>
<td> <input type="submit" value="Search Your College" <a href="#submit" class="btn btn-default btn-lg btn-satact" role="button"></a>></td>
</tr>
</table>
</form>
And here is my code in search.jsp. The database name/url/id/password is all entered correctly. I used the same code for different search file. It works perfectly in that file. However, it doesn't connect to database in this .jsp file. Here is my code for search.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!--Import some libraries that have classes that we need -->
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
try {
//Create a connection string
String url = "my_data_base_link";
//Load JDBC driver - the interface standardizing the connection procedure. Look at WEB-INF\lib for a mysql connector jar file, otherwise it fails.
Class.forName("com.mysql.jdbc.Driver");
//Create a connection to your DB
Connection con = DriverManager.getConnection(url, "user", "pass");
//Create a SQL statement
Statement stmt = con.createStatement();
String college = request.getParameter("college");
String str = "SELECT College, TUITIONFEE_IN, State FROM my_project1.all WHERE College LIKE " + "'" + college + "%'";
ResultSet result = stmt.executeQuery(str);
con.close();
} catch (Exception ex) {
out.print("insert failed");
}
It prints out insert fail in the catch part of try and catch. Any help is appreciated.
Upvotes: 4
Views: 207
Reputation: 153
Thank you. I figured it out. It was missing the mysql-connecter jar file. I added that from project properties ->buildpath -> add external jar (to bin folder under webINF->lib)
link for mysql connector http://dev.mysql.com/downloads/connector/j/5.0.html
Upvotes: 2