JSP and JDBC Problem

hi there,

I had the following codes on my JSP page:

<html>

<head><title>Stored Procedure Call using JDBC</title></head>

<body>

To test JDBC call to fire stored procedure named <b>sp_GetClatTypeIE</b>

<%! Class.forName("oracle.jdbc.driver.OracleDriver"); %>

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

<%

try{

String url ="jdbc:oracle:thin:@10.97.2.50:1521:REL4";

String scall ="begin sp_GetClatTypeIE(?, ?); end;";

Connection conn = DriverManager.getConnection(url,"tux","tux");

CallableStatement cs = conn.prepareCall(scall);

cs.registerOutParameter(2, Types.VARCHAR);

cs.setInt(1, 22555);

cs.execute();

cs.close();

conn.close();

String output = cs.getString(2);

}catch (SQLException e){

System.err.println("Error: " + e.getMessage());

}

out.println("Result is: " + output);

%>

</body>

</html>

and when i run it on my Tomcat 3.3a (Sun), i get this error returned:

org.apache.jasper.JasperException: Unable to compile /software/jakarta-tomcat-3.3a/work/DEFAULT/ROOT/SpTest_2.java:12: Type expected.

Class.forName("oracle.jdbc.driver.OracleDriver");

^

1 error

at org.apache.tomcat.facade.JasperLiaison.javac(Unknown Source)

at org.apache.tomcat.facade.JasperLiaison.processJspFile(Unknown Source)

at org.apache.tomcat.facade.JspInterceptor.requestMap(Unknown Source)

at org.apache.tomcat.core.ContextManager.processRequest(Unknown Source)

at org.apache.tomcat.core.ContextManager.internalService(Unknown Source)

at org.apache.tomcat.core.ContextManager.service(Unknown Source)

at org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Unknown Source)

at org.apache.tomcat.util.net.TcpWorkerThread.runIt(Unknown Source)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(Unknown Source)

at java.lang.Thread.run(Thread.java:484)

whats wrong? please help me!

[2636 byte] By [yyvoo] at [2007-9-18]
# 1
Do you have a Oracle Driver in the tomcat lib Directory ?I am workin on the same sort of thing .. but i am using SQl server and it works fine.
deep_3 at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2
How do i check and add? My Oracle is 7.3.4 and the library files are in zip format instead of jar format...workable?
yyvoo at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3

This is how i am doing:

<%@ page import="java.sql.*" %>

<%@ page import="java.io.*" %>

<%@ page import="java.util.*" %>

<%!

final static String driver = "org.gjt.mm.mysql.Driver";

final static String url = "jdbc:mysql://142.204.57.227:3334/?";

static Statement stmtProducts = null;

static Statement stmtVersions = null;

static Statement stmtDefectType = null;

static Statement stmtResolution = null;

static Connection conn = null;

ResultSet rsProducts = null;

ResultSet rsVersions = null;

ResultSet rsDefectType = null;

ResultSet rsResolution = null;

%>

<%

try {

Class.forName(driver); // load jdcb driver

conn = DriverManager.getConnection(url,?,?);

stmtProducts = conn.createStatement();

stmtVersions = conn.createStatement();

stmtDefectType = conn.createStatement();

stmtResolution = conn.createStatement();

}

catch (Exception ee)

{

out.println("Error Connecting to the database");

}

%>

Did you set the classpath of the env ?

deep_3 at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4
The error, "Type expected", resulted from the declaration you used, which is not a valid one.Try this, changing your declaration as following:<%! Class classOracleDriver = Class.forName("oracle.jdbc.driver.OracleDriver"); %>Hope it helps.
likeair at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5

hi there,

after modifying my codes like this:

<html>

<head><title>Stored Procedure Call using JDBC</title></head>

<body>

To test JDBC call to fire stored procedure named <b>sp_GetClatTypeIE</b>

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

<%

String output = "";

try{

Class myDriver = Class.forName("oracle.jdbc.driver.OracleDriver");

String url = "jdbc:oracle:thin:@10.97.2.50:1521:REL4";

String scall = "begin sp_GetClatTypeIE(?, ?); end;";

Connection conn = DriverManager.getConnection(url, "tux",

"tux");

CallableStatement cs = conn.prepareCall(scall);

cs.registerOutParameter(2, Types.VARCHAR);

cs.setInt(1, 22555);

cs.execute();

cs.close();

conn.close();

output = cs.getString(2);

} catch (SQLException e) {

System.err.println("Error: " + e.getMessage());

}

out.println("Result is: " + output);

%>

</body>

</html>

and when i run it, it generates this error:

javax.servlet.ServletException: oracle.jdbc.driver.OracleDriver

at org.apache.jasper.runtime.PageContextImpl.handlePageException(Unknown

Source)

at SpTest_10._jspService(SpTest_10.java:88)

at org.apache.jasper.runtime.HttpJspBase.service(Unknown Source)

at javax.servlet.http.HttpServlet.service(HttpServlet.java)

at org.apache.tomcat.facade.ServletHandler.doService(Unknown Source)

at org.apache.tomcat.core.Handler.invoke(Unknown Source)

at org.apache.tomcat.core.Handler.service(Unknown Source)

at org.apache.tomcat.facade.ServletHandler.service(Unknown Source)

at org.apache.tomcat.core.ContextManager.internalService(Unknown Source)

at org.apache.tomcat.core.ContextManager.service(Unknown Source)

at

org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Unknown

Source)

at org.apache.tomcat.util.net.TcpWorkerThread.runIt(Unknown Source)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(Unknown

Source)

at java.lang.Thread.run(Thread.java:484)

Root cause:

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

at

org.apache.tomcat.util.depend.DependClassLoader.loadClassInternal1(Unknown

Source)

at org.apache.tomcat.util.depend.DependClassLoader12$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at org.apache.tomcat.util.depend.DependClassLoader12.loadClass(Unknown

Source)

at java.lang.ClassLoader.loadClass(ClassLoader.java:255)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:120)

at SpTest_10._jspService(SpTest_10.java:63)

at org.apache.jasper.runtime.HttpJspBase.service(Unknown Source)

at javax.servlet.http.HttpServlet.service(HttpServlet.java)

at org.apache.tomcat.facade.ServletHandler.doService(Unknown Source)

at org.apache.tomcat.core.Handler.invoke(Unknown Source)

at org.apache.tomcat.core.Handler.service(Unknown Source)

at org.apache.tomcat.facade.ServletHandler.service(Unknown Source)

at org.apache.tomcat.core.ContextManager.internalService(Unknown Source)

at org.apache.tomcat.core.ContextManager.service(Unknown Source)

at

org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Unknown

Source)

at org.apache.tomcat.util.net.TcpWorkerThread.runIt(Unknown Source)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(Unknown

Source)

at java.lang.Thread.run(Thread.java:484)

please help...

yyvoo at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6
Hi,Put your oracle driver jar file or folder into the lib folder & edit your setclasspath.bat file & set the classpath to this lib folder. Your problem i feel, is because of classpath not being set properly. Good luck.
tssathish at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 7
Yeah, solved!
yyvoo at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 8
did my reply help you? anyway thanks for responding.
tssathish at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 9
I got the same answer from JGuru before i read ur reply, but thanks for helping me... I hope you would also help me in the other topics coz i m a newbies converted from MS products...
yyvoo at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 10

Hi, I am having the same problem with you about the "Type expected", while I am using jsp to connect to MSSQL. I have tried all the solution that mentioned in this topic, but problem still exists. I even use MS's type 4 ODBC driver and copy the jar files into the lib folder and also edit the setclasspath.bat file, but problem still exists! But I really just follow the examples in Java hp, but nothing help! I am now getting very frastrated and feel helpless with jsp and sql...

I am using java 1.4 and tomcat 4.0.3, while my code is :

<%@ page import="java.sql.*" %>

<HTML>

<BODY>

<%!

String url = "jdbc:odbc:;DRIVER=SQL Server;Integrated Security=SSPI;Persist Security Info=False;database=db;Server=server";

final static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

Connection con;

Statement stmt;

ResultSet rs;

Class.forName(driver);

con=DriverManager.getConnection(url);

stmt=con.createStatement();

rs = stmt.executeQuery(querystring);

while (rs.next()) {

//do something

};

%>

</BODY>

</HTML>

picardth at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...