Use of MySQL database on Bae Baidu cloud platform (Java)

Source: Internet
Author: User

The Bae database uses MySQL and phpMyAdmin. How can this problem be solved? It is too much like the PHP space.

But they are all free ~~

The first problem is data connection.

I made a simple chat room project and planned to try it out.

The user name and password of the BAE database connection must be obtained from the request.

Only the database name can be used directly.

Defines a jdbcutil class. Used to obtain the connection.

To save code, you can directly write a total of static variables.

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"; // reject the private jdbcutil () {}; static {// register the driver 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 );}

To access each page, you must set the connection user name and password.

Simply use a filter to 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, 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 {}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.