从请求header中获取ip、端口、用户名和密码信息
String host = request.getHeader("BAE_ENV_ADDR_SQL_IP");
String port = request.getHeader("BAE_ENV_ADDR_SQL_PORT");
String username = request.getHeader("BAE_ENV_AK");
String password = request.getHeader("BAE_ENV_SK");
String driverName = "com.mysql.jdbc.Driver";
String dbUrl = "jdbc:mysql://";
String serverName = host + ":" + port + "/";
//从平台查询应用要使用的数据库名
String databaseName = "NvZwtrQsgwQyjVrzdpVH";
String connName = dbUrl + serverName + databaseName;
out.print(connName+" |username="+username+"|password=="+password+"<br/>");
String sql = "select * from weixin";
Connection connection = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(driverName);
//具体的数据库操作逻辑
connection = DriverManager.getConnection(connName, username,
password);
stmt = connection.createStatement();
rs = stmt.executeQuery(sql);
String id = "", name = "";
out.println("id name<br/>");
while (rs.next()) {
id = rs.getString("id");
name = rs.getString("count");
out.println(id + " " + name + "<br/>");
}
} catch (ClassNotFoundException ex) {
// 异常处理逻辑
throw ex;
} catch (SQLException e) {
// 异常处理逻辑
throw e;
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
throw e;
}
}
http://developer.baidu.com/wiki/index.php?title=docs/cplat/rt/java/mysql