Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

connecting to db: problem with variable in declared in try catch block

807597Aug 1 2005 — edited Aug 1 2005
Hi Everyone
I'm trying to get a connection to a database, handle exceptions and close the connection.
When I compile my class I get a cannot resolve symbol symbol : variable connection
location: class mySite
connection.close();
^

when closing the connection in my finally block. I have declared it in the same method it is not a private variable can anyone say why this is happening?
thanks
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*; 
import java.util.*;

public class mySite extends HttpServlet{
	String userName;
	
	public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException{
    //connecting to database
    
    PrintWriter out=res.getWriter();
        
    
    try{
		Class.forName("org.gjt.mm.mysql.Driver"); //Load driver class
		Connection connection = DriverManager.getConnection("jdbc:mysql://url","login","password"); //create connection object
		Statement statement = connection.createStatement(); //create statement object
		ResultSet results=statement.executeQuery("SELECT * FROM users where name=(' "+userName+"   ')");
		while(results.next()){}
		
		
		}//end of try block
		catch(SQLException e ){
			out.println("SQL Error: "+e.getMessage());
			}
		finally
                {
        	connection.close();		
                }//end of finally
			
                    	
                    	
                    	}//end of doPost method
}//end of mySite
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 29 2005
Added on Aug 1 2005
10 comments
329 views