Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

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!

Reading a variable from one method to another method

843842Apr 16 2010 — edited Apr 17 2010
I am pretty new to Java and object code, so please understand if you think I should know the answer to this.

I wish to pass a variable from one method to another within one servet. I have a doPost method:
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    	 HttpSession session = request.getSession();
 	    String userID = (String)session.getAttribute("userID");
 	    String title = request.getParameter("title");
 	   PrintWriter out = response.getWriter();
 	   out.println("userID is ..."+userID);
.....................
}
and wish to pass the variable userID to:

  public static void writeToDB(String title) {
    	try{
    		String connectionURL = "jdbc:mysql://localhost/cms_college";
    		Connection connection=null;	
             Class.forName("com.mysql.jdbc.Driver");
             connection = DriverManager.getConnection(connectionURL, "root", "");
             Statement st = connection.createStatement();          
			st.executeUpdate ("INSERT INTO cmsarticles (title, userID) VALUES('"+title+"','"+userID+"')"); 
    	}
    	catch(Exception e){
    	      System.out.println("Exception is ;"+e);
    	      }
    }
because, at the moment the userID cannot be resolved in the writeToDB method. Thanking you in anticipation.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 15 2010
Added on Apr 16 2010
5 comments
253 views