bae百度云平台的mysql数据库的使用(java)
bae的数据库使用的mysql,还有phpmyadmin,怎么说呢,太像那种php空间了。
不过都是免费的哈~~
第一个问题就是连接数据的问题。
做了一个简单的聊天室项目,打算放上去试试。
bae的数据库连接的用户名和密码需要从request请求中获取。
只有数据库名是我们可以直接拿来用的。
定义一个jdbcutil类。用来获取连接。
为了节省代码,直接写成 共有静态变量了。
public final 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); }
访问每个页面都都要设置 连接的用户名和密码。
干脆直接来个过滤器,过滤每个请求。
jdbcfilter.initfilterjdbc*.*
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 { } }
