The use of the MySQL database of Bae's Baidu Cloud Platform (Java)
BAE's database uses MySQL, and phpmyadmin, how to say, too much like that PHP space.
But it's all free, huh ~ ~
The first problem is the problem of connecting data.
Make a simple chat room project and try to put it on.
The user name and password for the BAE database connection needs to be obtained from the request requests.
Only the database name is available to us directly.
Defines a Jdbcutil class. Used to get the connection.
In order to save code, it is written directly into a common static variable.
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"; Deny new one instance private Jdbcutil () {}; static {//register drive 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 + "/" + da Tabasename; Return drivermanager.getconnection (Connname); }
Access each page to set the user name and password for the connection.
Simply go to a filter and filter each request.
<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, servletrespons E 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 {}}