Home > Database > Mysql Tutorial > 如何连接百度Mysql_MySQL

如何连接百度Mysql_MySQL

WBOY
Release: 2016-06-01 13:10:12
Original
1289 people have browsed it

请使用mysql-connector-java-5.1.22.jar或更新的版本。

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.HttpServletResponse;public class MySQLBasic extends HttpServlet {	@Override	protected void doGet(HttpServletRequest req, HttpServletResponse resp)			throws ServletException, IOException {		Connection connection = null;		Statement stmt = null;		ResultSet rs = null;		String sql = null;		try {			/***** 1. 填写数据库相关信息(请查找数据库详情页) *****/			String databaseName = " eiZrxDYAgZRYcJuTRljT ";			String host = "sqld.duapp.com";			String port = "4050";			String username = " urtgxzMPVigNEtOQF7yzg7C9";// 用户名(api key);			String password = "1e0jDqkZ7fUwNgFD5LzwPY4YAQURFGYM";// 密码(secret			// key)			String driverName = "com.mysql.jdbc.Driver";			String dbUrl = "jdbc:mysql://";			String serverName = host + ":" + port + "/";			String connName = dbUrl + serverName + databaseName;			/****** 2. 接着连接并选择数据库名为databaseName的服务器 ******/			Class.forName(driverName);			connection = DriverManager.getConnection(connName, username,					password);			stmt = connection.createStatement();			/* 至此连接已完全建立,就可对当前数据库进行相应的操作了 */			/* 3. 接下来就可以使用其它标准mysql函数操作进行数据库操作 */			// 创建一个数据库表			sql = "create table if not exists test_mysql("					+ "id int primary key auto_increment," + "no int, "					+ "name varchar(1024)," + "key idx_no(no))";			stmt.execute(sql);		} catch (Exception e) {			e.printStackTrace(resp.getWriter());		}	}}
Copy after login


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template