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.