When im trying to run this code to establish a connection …… this error is coming
Im using Apache-tomcat-7.0.8
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 1 in the jsp file: /test_conn.jsp
Connection cannot be resolved to a type
1: <% Connection connection = null; try { // Load the JDBC driver String driverName = "oracle.jdbc.driver.OracleDriver"; Class.forName(driverName);
2:
3: // Create a connection to the database
4: String serverName = "URL";
Code I’ve tried:
<%@page import="java.sql.*,java.io.*,java.text.*,java.util.*" %>
<%@page import="java.util.*" %>
<%
Connection connection = null;
out.println("Before try");
try {
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
out.println("Entered try");
String url = "jdbc:oracle:thin:@:URL:port:sid";
String username = "usr";
String password = "pass";
connection = DriverManager.getConnection(url, username, password);
out.println("Successfully Connected");
}catch (SQLException e) {
out.println("Not Connected: "+ e.getMessage());
}
%>
You should have to use Servlet to write database code however you may import the java.sql package or use the Connection interface with fully qualified package name.
EDIT:
You need to copy the .jar (eg. for oracle 10g – ojdbc14.jar) into the WEB-INF/lib folder.