当我试图运行此代码以建立连接时......此错误即将到来
我正在使用Apache-tomcat-7.0.8
org.apache.jasper.JasperException: Unable to compile class for JSP:
行发生错误:1 in the jsp file: /test_conn.jsp
无法将连接解析为类型
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";
我试过的代码:
<%@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());
}
%>
答案 0 :(得分:1)
您必须使用Servlet编写数据库代码,但是您可以导入java.sql包或使用具有完全限定包名的Connection接口。
java.sql.Connection connection = null;
编辑:
您需要将.jar(例如oracle 10g - ojdbc14.jar)复制到WEB-INF / lib文件夹中。
答案 1 :(得分:0)
在JSP中你忘了包含
<%@page import="java.sql.Connection"%>
虽然,这不是好习惯。您应该编写Servlet和DAO类来执行此操作。 JSP是写HTML。