Solved

Problem in MS SQL Connection Using JSP

Posted on 2006-11-16
6
1,679 Views
Last Modified: 2012-08-14
Helo,

I tried to connect to MS SQL using JSP and JTDS driver. I got the following errors:

[ErrorMessage-START]
An error occurred at line: 4 in the jsp file: /search2.jsp
Generated servlet error:
ds cannot be resolved
..
..
con cannot be resolved
..
stm cannot be resolved
..
result cannot be resolved
..
e cannot be resolved
..
..
[ErrorMessage-END]

Below are all the related files:
1. search.jsp (in C:\apache-tomcat\webapps\leone\)
2. context.xml (in C:\apache-tomcat\conf\Catalina\localhost\)

search.jsp:
********
<%@ page import="java.text.*,javax.sql.*,javax.naming.*, com.syslore.multicolumn.*" %>
<html>
<body>
<%
try {
Context context = new InitialContext();
Context envCtx = (Context) context.lookup("java:comp/env");
ds = (DataSource)envCtx.lookup("jdbc/SQLServer");
if (ds != null) {
  con = ds.getConnection();
  stm = con.createStatement();
  result = stm.executeQuery("SELECT * FROM Employees");
  }//if
}

catch (NamingException ex) {
  ex.printStackTrace();
} catch (SQLException e) {
  e.printStackTrace();
}//try
%>
</body>
</html>

context.xml:
*********
<Context path="/TesteSQLServer" docBase="TesteSQLServer" debug="5" reloadable="true">
  <Resource name="jdbc/SQLServer"
    auth="Container"
    type="javax.sql.DataSource"
    maxActive="10"
    maxIdle="1"
    maxWait="10000"
    username="26226255"
    password="tt"
    driverClassName="net.sourceforge.jtds.jdbc.Driver"
    url="jdbc:jtds:sqlserver://XX.XX.XX.XX/ControleOper_DES" />
</Context>

Anyone has idea of how to fix the error?

Thanks.
0
Comment
Question by:SWB-Consulting
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 10

Accepted Solution

by:
jaggernat earned 300 total points
ID: 17962569
Replace

con = ds.getConnection();
stm = con.createStatement();
result = stm.executeQuery("SELECT * FROM Employees");

with

Connection con = ds.getConnection();
Statement stm = con.createStatement();
ResultSet  result = stm.executeQuery("SELECT * FROM Employees");
0
 

Author Comment

by:SWB-Consulting
ID: 17962674
After I added the above (as you recommended) below is the error message:

ds cannot be resolved
Connection cannot be resolved to a type
Statement cannot be resolved to a type
SQLException cannot be resolved to a type
e cannot be resolved

But then I replace
ds = (DataSource)envCtx.lookup("jdbc/SQLServer");
with
DataSource ds = (DataSource)envCtx.lookup("jdbc/SQLServer");

Below are the error messages (notice the ds is fixed, but the rest remains the same):

Connection cannot be resolved to a type
Statement cannot be resolved to a type
SQLException cannot be resolved to a type
e cannot be resolved

Hmm.. what did I miss?
0
 
LVL 10

Assisted Solution

by:mbvvsatish
mbvvsatish earned 150 total points
ID: 17963056
you have imported javax.sql.*;
you also need to import java.sql.*;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SWB-Consulting
ID: 17968227
Thanks. It's much better now after adding the object declaration at the front and importing java.sql.*.  However, there is still one error:

"The type ResultSet is ambiguous"

Is it because wrong connection parameters (url) in context.xml or a pure java error?

0
 
LVL 4

Assisted Solution

by:orhanbaba
orhanbaba earned 50 total points
ID: 17975320
use full package name
java.sql.ResultSet result;
0
 

Author Comment

by:SWB-Consulting
ID: 17992416
yes.  now it works!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is a lot to be said for protecting yourself and your accounts with 2 factor authentication.  I found to my own chagrin, that there is a big downside as well.
Microsoft is moving in-place eDiscovery & hold from ECP to EOP console under Content Search in Search and Investigation Options.  In this post, I will be showing you how to export emails to a PST file using the Content Search Options.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question