如何在一个方法中使用另一个方法中声明的变量
how to use variable declared in one method in another method
我已经编写了一个名为 "Get_connection" 的方法来将 SQL 服务器与我的 Servlet 连接,但我无法在该方法中使用 "out.println();" 方法,即使我将 http servlet 响应作为参数..
谁能解释一下如何纠正..
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
}
}
public Connection Get_connection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433; databaseName = Colombo_Health; integratedSecurity=true;";
con = DriverManager.getConnection(url, "", "");
out.println("Connection Established");
} catch (ClassNotFoundException e) {
out.println("Class not Found " + e.toString());
e.toString();
} catch (Exception e) {
out.println("Driver not Found " + e.toString());
e.toString();
}
return con;
}
您的 Get_connection 方法应具有以下签名。
public Connection Get_connection(PrintWriter out) { }
从 processRequest 方法的 try 块调用它作为 Get_connection(out);
同样在 processRequest 方法中,您的 try 应该跟在 catch 块或 finally 之后。干杯。
我已经编写了一个名为 "Get_connection" 的方法来将 SQL 服务器与我的 Servlet 连接,但我无法在该方法中使用 "out.println();" 方法,即使我将 http servlet 响应作为参数..
谁能解释一下如何纠正..
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
}
}
public Connection Get_connection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433; databaseName = Colombo_Health; integratedSecurity=true;";
con = DriverManager.getConnection(url, "", "");
out.println("Connection Established");
} catch (ClassNotFoundException e) {
out.println("Class not Found " + e.toString());
e.toString();
} catch (Exception e) {
out.println("Driver not Found " + e.toString());
e.toString();
}
return con;
}
您的 Get_connection 方法应具有以下签名。
public Connection Get_connection(PrintWriter out) { }
从 processRequest 方法的 try 块调用它作为 Get_connection(out);
同样在 processRequest 方法中,您的 try 应该跟在 catch 块或 finally 之后。干杯。