zoukankan      html  css  js  c++  java
  • BAE百度云平台的mysql数据库的施用(Java)

    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);
        }  


    访问每个页面都都要设置 连接的用户名和密码。

    干脆直接来个过滤器,过滤每个请求。


      <filter>
    		<filter-name>jdbc</filter-name>
    		<filter-class>filter.InitFilter</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>jdbc</filter-name>
    		<url-pattern>*.*</url-pattern>
    	</filter-mapping>



    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 {
    		
    	}
    	
    }
  • 相关阅读:
    大物—⑦热力学基础1
    组原——③系统总线
    组原——①计算机系统概论
    递归
    字符串
    Linux下安装glibc-2.14,解决“`GLIBC_2.14' not found”问题
    libc.so.6被删除修复ln: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory
    正则表达式高级用法(分组与捕获)
    查看Android应用包名、Activity的几个方法
    logcat随笔
  • 原文地址:https://www.cnblogs.com/cjt-java/p/3813973.html
Copyright © 2011-2022 走看看