| Author |
cannot find symbol
|
Megha Singhal
Ranch Hand
Joined: Feb 28, 2012
Posts: 128
|
|
in the following code
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection.*;
import java.sql.Statement.*;
import java.sql.ResultSet.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import oracle.sql.*;
import javax.naming.*;
import javax.naming.directory.*;
import oracle.jdbc.pool.OracleDataSource;
public class DBPhoneLookup extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
OracleDataSource ds = (OracleDataSource) envContext.lookup("jdbc/mastr");
if (envContext == null)throw new Exception("Error: No Context");
if (ds == null) throw new Exception("Error: No DataSource");
if (ds != null)
con = ds.getConnection();
stmt = con.createStatement();
// Execute an SQL query, get a ResultSet
rs = stmt.executeQuery("SELECT NAME, PHONE FROM MASTER");
// Display the result set as a list
out.println("<HTML><HEAD><TITLE>Phonebook</TITLE></HEAD>");
out.println("<BODY>");
out.println("<UL>");
while(rs.next()) {
out.println("<LI>" + rs.getString("name") + " " + rs.getString("phno"));
}
out.println("</UL>");
out.println("</BODY></HTML>");
}
catch(ClassNotFoundException e) {
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e) {
out.println("SQLException caught: " + e.getMessage());
}
finally {
// Always close the database connection.
try {
if (con != null) con.close();
}
catch (SQLException ignored) { }
}
}
}
i am getting below error
cannot find symbol
symbol : class Connection
location: class DBPhoneLookup
Connection con = null;
DBPhoneLookup.java:27: cannot find symbol
symbol : class Statement
location: class DBPhoneLookup
Statement stmt = null;
^
DBPhoneLookup.java:28: cannot find symbol
symbol : class ResultSet
location: class DBPhoneLookup
ResultSet rs = null;
i don't knw why
|
 |
Neeraj Dhiman
Ranch Hand
Joined: Dec 19, 2011
Posts: 63
|
|
Please use code tag for java code. it will easier to read.
May be Something wrong with yours java.sql.* package;
you are using import as java.sql.Connection.*;
use java.sql.Connection; same for Resultset and Statement
|
Correct Me if i am wrong
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Neeraj is right. You are not importing java.sql.Connection; instead, your importing any nested classes / interfaces it has (none).
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Megha Singhal
Ranch Hand
Joined: Feb 28, 2012
Posts: 128
|
|
Neeraj Dhiman wrote:Please use code tag for java code. it will easier to read.
May be Something wrong with yours java.sql.* package;
you are using import as java.sql.Connection.*;
use java.sql.Connection; same for Resultset and Statement
Thanks my that error has remove but now following exceptions are coming
DBPhoneLookup.java:33: unreported exception javax.naming.NamingException; must be caught or declared to be thrown
Context initContext = new InitialContext();
^
DBPhoneLookup.java:34: unreported exception javax.naming.NamingException; must be caught or declared to be thrown
Context envContext = (Context) initContext.lookup("java:/comp/env");
^
DBPhoneLookup.java:35: unreported exception javax.naming.NamingException; must be caught or declared to be thrown
OracleDataSource ds = (OracleDataSource) envContext.lookup("jdbc/racltc");
^
DBPhoneLookup.java:36: unreported exception java.lang.Exception; must be caught or declared to be thrown
if (envContext == null)throw new Exception("Error: No Context");
^
DBPhoneLookup.java:37: unreported exception java.lang.Exception; must be caught or declared to be thrown
if (ds == null) throw new Exception("Error: No DataSource");
^
5 errors
and if i throws NamingException also along with other Exception than following error is coming
DBPhoneLookup.java:20: service(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) in DBPhoneLookup cannot override service(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) in javax.servlet.http.HttpServlet; overridden method does not throw javax.naming.NamingException
public void service(HttpServletRequest req,
|
 |
Neeraj Dhiman
Ranch Hand
Joined: Dec 19, 2011
Posts: 63
|
|
you should catch the naming exception.
try this
hope this will work.
Please post you code in CODE TAG.
|
 |
Megha Singhal
Ranch Hand
Joined: Feb 28, 2012
Posts: 128
|
|
Neeraj Dhiman wrote:you should catch the naming exception.
try this
hope this will work.
now following exception is coming
DBPhoneLookup.java:40: unreported exception java.lang.Exception; must be caught or declared to be thrown
if (envContext == null)throw new Exception("Error: No Context");
^
DBPhoneLookup.java:41: unreported exception java.lang.Exception; must be caught or declared to be thrown
if (ds == null) throw new Exception("Error: No DataSource");
|
 |
Neeraj Dhiman
Ranch Hand
Joined: Dec 19, 2011
Posts: 63
|
|
you have to handle the Exceptions.
use
i think it will work or give you the exact problem. hope so...
|
 |
Megha Singhal
Ranch Hand
Joined: Feb 28, 2012
Posts: 128
|
|
Neeraj Dhiman wrote:you have to handle the Exceptions.
use
i think it will work or give you the exact problem. hope so...
Thanks alot it finaly works
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: cannot find symbol
|
|
|