JSPServletとDB接続テスト2

Java

WEBで接続テスト(Eclipse)

Tomcat5.0.28使用

順不同
1)test.jspファイルの作成
2)warファイルの設定
プロジェクトのプロパティー -> Tomcat -> Export to WAR setting で、warファイル名と作成場所を設定。
3)warファイルの作成
プロジェクトを右クリック -> Tomcatプロジェクト -> プロジェクトの設定に従いwarファイルを作成するをクリック。
4)Tomcat起動。
5)ブラウザ起動 -> アドレス入力(http://localhost:8080/warファイル名/test.jsp)

test.jspファイル


<%@page pageEncoding="MS932" contentType="text/html;
  charset=MS932" import="java.sql.*" %>
<html>
<head>
<title></title>
</head>
<body>

<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
  "jdbc:mysql://localhost/testdb?user=
    ユーザー名&password=パスワード&useUnicode=true
    &characterEncoding=Shift_JIS");
Statement statement = conn.createStatement();
ResultSet resu = statement.executeQuery(
  "select name from us where id = 1;");
if(resu.next()) {
%>
<h1><%=resu.getString("name") %></h1>
<% } %>

</body>
</html>
スポンサーリンク

コメント

タイトルとURLをコピーしました