package com.lin.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lin.info.SaeUserInfo;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//app_name为创建的应用名
String URL="jdbc:mysql://r.rdc.sae.sina.com.cn:3307/app_name";
//在SAE上创建的studnt表
String sql = "SELECT * FROM student";
// 通过SaeUserInfo提供的静态方法获取应用的access_key和secret_key 对应的SaeUserInfo是自己创建的,使用SAE提供的key
String Username=SaeUserInfo.getAccessKey();
String Password=SaeUserInfo.getSecretKey();
String Driver="com.mysql.jdbc.Driver";
try{
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
PreparedStatement pstmt = con.prepareStatement("select * from student");
ResultSet rs = (ResultSet) pstmt.executeQuery();
while(rs.next()){
resp.getWriter().write(rs.getString("username"));
System.out.println(rs.getInt("id"));
}
}catch(Exception e){
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}
}
SAE使用java连接mysql,布布扣,bubuko.com
SAE使用java连接mysql