Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cannot find symbol

 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 68
IBM DB2 Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeraj is right. You are not importing java.sql.Connection; instead, your importing any nested classes / interfaces it has (none).
 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should catch the naming exception.

try this



hope this will work.


Please post you code in CODE TAG.
 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have to handle the Exceptions.
use

i think it will work or give you the exact problem. hope so...
 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic