javaサーブレットとmysqlの連携
【MYSQL】
・データベース:sampledbを作る
mysql>create database sampledb;
・テーブル:userinfoを作る
mysql>CREATE TABLE userinfo(
->userid VARCHAR(10) PRIMARY KEY,
->status INTEGER NOT NULL
->)ENGINE=InnoDB;
・データの入力
mysql>INSERT INTO userinfo(userid, status) VALUES("aaaa1234", 0);
mysql>INSERT INTO userinfo(userid, status) VALUES("zzzz9876", 2);
【Eclipse】
設定
JDBCドライバをプロジェクトに追加する。
ダウンロードしておいた mysql-connector-java-5.1.10-bin.jar(最新版を使う) をエクスプローラからlibディレクトリ[TomcatTest/WebContent/WEB-INF/lib]にドラッグ&ドロップする
【MysqlTestサーブレット】
package a;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MysqlTest
*/
@WebServlet("/MysqlTest")
public class MysqlTest extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MysqlTest() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql:///sampledb?user=root&password=H1234");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT userid,status FROM userinfo");
response.setContentType("text/plain");
while (rs.next()) {
response.getWriter().write("userid=" + rs.getString("userid") + ", ");
response.getWriter().write("status=" + rs.getString("status") + "\n");
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if (rs != null ) { try {rs.close(); } catch (SQLException e) {e.printStackTrace();} }
if (stmt != null ) { try {stmt.close(); } catch (SQLException e) {e.printStackTrace();} }
if (conn != null ) { try {conn.close(); } catch (SQLException e) {e.printStackTrace();} }
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
【表示】
userid=aaaa1234, status=0
userid=zzzz9876, status=2