DataBaseに接続 (JDBC & JSP)
ファイル名 | 機能 |
---|---|
jsp_database.html | 入力フィールドと「データを入力」ボタンを配置したページ |
jsp_insert_data.jsp | データをデータベースに追加するJSP データベースに追加できたときは、"1 line insert successfully." 失敗したときは"Data insert fail" |
内容 | データベース | 入力ページ | ||||
フィールド名 | 型 | tag | type | name | 入力する値 | |
出席番号 | id | int | input | text | id | 整数 |
氏名 | name | text | input | text | name | 文字列 |
<form method="POST" action="JSPをjsp_database.htmlからみた相対パスで記述する場合、http://localhost:8080/jsp_database/は不要です。http://localhost:8080/jsp_database/jsp_insert_data.jsp"> <fieldset> <legend>データを入力する</legend> 出席番号 <input size="20" type="text" name="id"><br> 氏 名 <input size="20" type="text" name="name"> <br> <input type="submit" value="データを入力する"> </fieldset> </form>
<%@ page contentType="text/html; charset=UTF-8" %> <%@ page import="java.sql.*" %> <html> <head> <title>DataBaseに接続 (JDBC & JSP)データを入力する</title> </title> <% String msg="" ; try { String id = request.getParameter("id") ;// 出席番号を取得 String name = request.getParameter("name") ;// 氏名を取得 // name = new String( name.getBytes("8859_1"),"EUC_JP") ; //日本語の処理 (EUC-JPで入力したとき) name = new String( name.getBytes("8859_1"),"UTF-8") ; //日本語の処理 Class.forName("org.postgresql.Driver"); Connection con = DriverManager.getConnection( "jdbc:postgresql://10.30.2.29:5432/j00300_02","j00300","xxxxxxxx") ; Statement st = con.createStatement() ; String sql = "INSERT INTO sample_02 VALUES (" + id + ",'" + name +"');" ; int result = st.executeUpdate(sql) ; msg=Integer.toString(result) + " line insert successfully.<br>" ; msg += "id=" + id + "& name=" + name ; } catch (Exception ex) { msg = "TABLE creation fail.<br>" ; msg += ex.toString(); msg += "<br>" ; //msg += "id="+ id + "& name=" + name ; } %> <body> <h1>DataBaseに接続 (JDBC & JSP)データを入力する</h1> <hr> <%= msg %><br><br> <hr> <a href=jsp_database.html>DataBaseに接続 (JDBC & JSP)</a> </body> </html>
![]() |
DataBaseに接続 (JDBC & JSP) | DataBaseに接続 (JDBC & JSP)データを検索 |