import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class クラス名 extends HttpServlet {
public void init() throws ServletException
{
}
//HTTP Get リクエストの処理
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
Connection con = null;
String jdbcUrl = "jdbc:postgresql:データベース名?charSet=UTF8";
String user = "ユーザ名";
String password = "パスワード";
try{
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection(jdbcUrl,user,password);
}catch(ClassNotFoundException e){
}catch(SQLException e){
}
Statement st = null;
ResultSet rs = null;
String sql1 = ""; //データ挿入用
String sql2 = ""; //データ更新用
String sql3 = ""; //データ削除用
String sql = ""; //データ表示用
int cnt;
String test = "";
try{
con = DriverManager.getConnection(jdbcUrl,user,password);
st = con.createStatement();
//sql = "set client_encoding='UTF8'";
//st.execute(sql);
///// データ挿入
//sql1 = "insert into テーブル名 values('data1','data2','data3')";
//cnt = st.executeUpdate(sql1);
///// データ更新
//sql2 = "update テーブル名 set name='new_data' where name='old_data'";
//cnt = st.executeUpdate(sql2);
///// データ削除
//sql3 = "delete from テーブル名 where name='data'";
//cnt = st.executeUpdate(sql3);
sql = "select * from テーブル名";
rs = null;
rs = st.executeQuery(sql);
while(rs.next()){
test += rs.getString("row1") + " / ";
test += rs.getString("row2") + " / ";
test += rs.getString("row3") + "
";
}
}catch(Exception e){
System.out.println(e);
}finally{
try{
if(rs != null){
rs.close();
}
if(st != null){
st.close();
}
if(con != null){
con.close();
}
}catch(Exception e){
}
}
out.println("