Home >Database >Mysql Tutorial >BAE百度云平台的mysql数据库的施用(Java)

BAE百度云平台的mysql数据库的施用(Java)

WBOY
WBOYOriginal
2016-05-31 08:50:16965browse

百度云

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 {			}	}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn