BAE的数据库运用的mysql,还有phpmyadmin,怎么说呢,太像那种php空间了。 不过都是免费的哈~~ 第一个疑问就是联接数据的疑问。 做了一个简略的聊天室项目,方案放上去试试。 BAE的数据库联接的用户名和暗码需要从request央求中获取。 只需数据库名是我们可以直接拿来用的。 定义一个JdbcUtil类。用来获取联接。 为了节省代码,直接写成 共有静态变量了。 public fin http://www.fpzhangsha.com/linked/20130313.do al class JdbcUtil {
private static String dbUrl = "jdbc:mysql://";
public static String port;
public static String host;
public static String username;
public static String password;
public static String databaseName = "zJtjKTokkLUoGqQZMBkC";
//拒绝new一个实例
private JdbcUtil() {};
static {//注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws SQLException {
String connName = dbUrl host ":" port "/" databaseName;
return DriverManager.getConnection(connName);
} 访问每个页面都都要设置 联接的用户名和暗码。 爽性直接来个过滤器,过滤每个央求。
jdbc
filter.InitFilter
jdbc
*.*
public class InitFilter implements Filter{
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
JdbcUtil.host = request.getHeader("BAE_ENV_ADDR_SQL_IP");
JdbcUtil.port =request.getHeader("BAE_ENV_ADDR_SQL_PORT");
JdbcUtil.username = request.getHeader("BAE_ENV_AK");
JdbcUtil.password = request.getHeader("BAE_ENV_SK");
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
}
} http://www.fpshamen.com/linked/20130313.do