Application of the MySQL database of Bae Baidu Cloud Platform (Java)

Source: Internet
Author: User

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 {}}
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.