先进行好配置
public class Config{
public static String MYSQLNAME="相关百度云应用对应的数据库名";
public static String MONGODBNAME="AssmgrRwYGgBljKjZrwH";
public static String REDISNAME="leWSVUPWnANxwELAdyFl";
public static String BCS_AK="";
public static String BCS_SK="";
public static String BUCKET="";
public static String POSTURL = "";
public static String POSTDATA = "a=b&c=d";
public static String SOURCEURL = "http://www.baidu.com/img/bdlogo.gif";
public static String DESTURL = "";
}
实现与数据库之间的连接
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import weizixun.conf.Config;
import weizixun.mysql.MySQLBasic;
public class DBUtil {
public static Connection getConnection(HttpServletRequest request, HttpServletResponse response) throws Exception{
Connection conn = null;
try{
String databaseName = Config.MYSQLNAME;
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 connName = dbUrl + serverName + databaseName+"?useUnicode=true&characterEncoding=utf8";
Class.forName(driverName);
conn = DriverManager.getConnection(connName,username,password);
}catch(Exception e){
e.printStackTrace();
throw e;
}
return conn;
}
public static void close(Connection conn){
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
好像就是这样子,然后就可以像访问本机数据库一样的方式来访问了~