%@ page
import="java.sql.*"
language="java"
contentType="text/html; charset=utf-8"
%>
<%
try {
Class.forName("org.postgresql.Driver");
}
catch(Exception e) {
System.out.println("
JDBCドライバロードエラー
" + e.toString() + "
");
//Tomcat5.5で「System」を記載すると出力されない。
}
String jdbcUrl = "jdbc:postgresql:データベース名?charSet=UTF8";
String user = "ユーザ名";
String password = "パスワード";
Connection con = null;
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);
//Tomcat5.5で「System」を記載すると出力されない。
}finally{
try{
if(rs != null){
rs.close();
}
if(st != null){
st.close();
}
if(con != null){
con.close();
}
}catch(Exception e){
}
}
%>
Java JSP PostgreSQL TEST
DB接続テスト(Java JSP PostgreSQL版)
<%=test%>