是的,您可以做到这一点,但不能使用 servlet。您需要定义一个 Common 类,该类将包含您的所有常用方法和变量,如下所示
public class Common {
public static final String DEFAULT_LANGUAGE = "en"; //better to have private variables with public setters and getters
....
public static String getDateFormatted(.....) {...}
....
}
最好创建一个单独的数据库类来控制您的数据库交互。让我们说:
public class DBConnection {
private Connection dbCon;
//its more convenient to implement the connect on the no ArgumentCostructor
....
public boolean connect() throws ClassNotFoundException, SQLException {...}
public ResultSet execSQL(...)throws ClassNotFoundException,SQLException {...}
}
如果您想在 java 类中使用全局参数,您只需调用
String formatedDate = Common.getDateFormatted(date);
或者对于数据库连接的东西,你可以打电话
DBConnection con = new new DBConnection ();
rs = con.execSQL(sql);