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