2011年3月30日(水) 20:52
mysql> grant all on *.* to test_user@localhost identified by "password"; Query OK, 0 rows affected (0.00 sec)
mysql> create database local collate utf8_general_ci; Query OK, 1 row affected (0.00 sec)
mysql> use local; Database changed mysql> set character set utf8; Query OK, 0 rows affected (0.00 sec) mysql> create table test_table(no char(8) primary key, name text not null, score int); Query OK, 0 rows affected (0.03 sec)
mysql> show fields from test_table; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | no | char(8) | NO | PRI | NULL | | | name | text | NO | | NULL | | | score | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 3 rows in set (0.01 sec)
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf8"> <title>jsp のテスト</title> </head> <body> <ul> <li><a href="insert.jsp">レコードの追加</a> <li><a href="display.jsp">レコードの表示</a> <li><a href="MySQL">サーブレットの実行</a> </ul> </body> </html>
<html>
<head><title>全レコードの追加</title></head>
<%@ page language="java" contentType="text/html; charset=utf8" %>
<%@ page import="java.sql.*" %>
<body>
<%
Connection con = null;
Statement objSql= null;
int rs = 0;
try {
// MySQL JDBC Driverのロード
Class.forName("com.mysql.jdbc.Driver");
/* データベースへ接続 */
con = DriverManager.getConnection("jdbc:mysql://localhost/local?characterEncoding=utf8","test_user","password");
// ――――― コネクションに対する操作 ―――――
// JDBC 問い合わせ SQL 作成
objSql = con.createStatement();
// SQL 問い合わせ
rs += objSql.executeUpdate("insert into test_table (no, name, score) values('12345678', '漢字の名前', 0)");
rs += objSql.executeUpdate("insert into test_table (no, name, score) values('00000001', 'name of alpha', 0)");
rs += objSql.executeUpdate("insert into test_table (no, name, score) values('00000002', '斎藤 ・・', 0)");
rs += objSql.executeUpdate("insert into test_table (no, name, score) values('00000005', '赤城 ・・・', 0)");
rs += objSql.executeUpdate("insert into test_table (no, name, score) values('01010101', '湯沢 ・', 0)");
rs += objSql.executeUpdate("insert into test_table (no, name, score) values('11111111', '唐沢 ・・〜', 0)");
rs += objSql.executeUpdate("insert into test_table (no, name, score) values('23232323', '新庄 ・・', 0)");
rs += objSql.executeUpdate("insert into test_table (no, name, score) values('21212121', '赤松 ・・', 0)");
rs += objSql.executeUpdate("insert into test_table (no, name, score) values('55555555', '榊 ・', 0)");
rs += objSql.executeUpdate("insert into test_table (no, name, score) values('99999999', '横木 ・・・', 0)");
rs += objSql.executeUpdate("insert into test_table (no, name, score) values('00000000', '町田 ・・', 0)");
//接続をクローズ
con.close();
out.println("テーブルに"+rs+"レコード追加しました。");
}catch (ClassNotFoundException e){
out.println("ドライバのロードに失敗しました");
}catch (Exception e){
out.println(e.getMessage( ));
}
%>
</body>
</html>
<html>
<head><title>jsp のテスト</title></head>
<%@ page language="java" contentType="text/html; charset=utf8" %>
<%@ page import="java.sql.*" %>
<body>
<table border="2">
<tr><td>id</td><td>name</td><td>score</td></tr>
<%
Connection con = null;
Statement objSql= null;
ResultSet rs = null;
try {
// MySQL JDBC Driverのロード
Class.forName("com.mysql.jdbc.Driver");
/* データベースへ接続 */
con = DriverManager.getConnection ("jdbc:mysql://localhost/local?characterEncoding=utf8","test_user","password");
// ――――― コネクションに対する操作 ―――――
// 読みだすだけなので、読み込み専用にする。
con.setReadOnly(true);
// JDBC 問い合わせ SQL 作成
objSql = con.createStatement();
// SQL 問い合わせ
rs = objSql.executeQuery("select * from test_table");
while( rs.next() ) {
out.println("<tr><td>"+rs.getString("no")+"</td>");
out.println("<td>"+rs.getString("name")+"</td>");
out.println("<td>"+rs.getInt("score")+"</td></tr>");
}
//接続をクローズ
con.close();
}catch (ClassNotFoundException e){
out.println("ドライバのロードに失敗しました");
}catch (Exception e){
out.println(e.getMessage( ));
}
%>
</table>
</body>
</html>
written by sirius [その他] [この記事のURL] [コメントを書く] [コメント(0)] [TB(0)]
MySketch 2.7.2 written by 夕雨