How to connect Baidu Mysql_MySQL

Source: Internet
Author: User
How to connect to Baidu Mysql Use a mysql-connector-java-5.1.22.jar or an updated version.

Package org. crashxun. controller; import java. io. IOException; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. statement; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. extends; public class MySQLBasic extends HttpServlet {@ Overrideprotected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Connection connection = null; Statement stmt = null; ResultSet rs = null; string SQL = null; try {/***** 1. enter database information (please refer to the database details page) *****/String databaseName = "eiZrxDYAgZRYcJuTRljT"; String host = "sqld.duapp.com"; String port = "4050 "; string username = "urtgxzmpvigneto3167yzg7c9"; // user name (api key); String password = "secret"; // password (secret // key) String driverName = "com. mysql. jdbc. driver "; String dbUrl =" jdbc: mysql: // "; String serverName = host +": "+ port +"/"; String connName = dbUrl + serverName + databaseName; /****** 2. connect to and select the database server named databaseName *****/Class. forName (driverName); connection = DriverManager. getConnection (connName, username, password); stmt = connection. createStatement ();/* now the connection has been fully established, you can perform corresponding operations on the current database * // * 3. next, you can use other standard mysql functions to perform database operations * // create a database table SQL = "create table if not exists test_mysql (" + "id int primary key auto_increment, "+" no int, "+" name varchar (1024), "+" key idx_no (no) ";stmt.exe cute (SQL);} catch (Exception e) {e. printStackTrace (resp. getWriter ());}}}

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.