Solved

Problem in MS SQL Connection Using JSP

Posted on 2006-11-16
6
1,682 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article is in regards to the Cisco QSFP-4SFP10G-CU1M cables, which are designed to uplink/downlink 40GB ports to 10GB SFP ports. I recently experienced this and found very little configuration documentation on how these are supposed to be confi…
A look at what happened in the Verizon cloud breach.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

632 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