Solved

Problem in MS SQL Connection Using JSP

Posted on 2006-11-16
6
1,670 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
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

A procedure for exporting installed hotfix details of remote computers using powershell
We have come a long way with backup and data protection — from backing up to floppies, external drives, CDs, Blu-ray, flash drives, SSD drives, and now to the cloud.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now