質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JDBC

JDBC(Java DataBase Connectivity)は、Javaとリーレーショナルデータベースに接続させる基本的なAPIです。Java上でSQLステートメントを発行することで、データベースの種類に影響を受ないDB操作を可能とします。

JSP

JSP(Java Server Pages)とは、ウェブアプリケーションの表示レイヤーに使われるサーバーサイドの技術のことです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

Q&A

解決済

1回答

2007閲覧

JDBCでの登録情報の変更で空文字をスルーしたい

退会済みユーザー

退会済みユーザー

総合スコア0

JDBC

JDBC(Java DataBase Connectivity)は、Javaとリーレーショナルデータベースに接続させる基本的なAPIです。Java上でSQLステートメントを発行することで、データベースの種類に影響を受ないDB操作を可能とします。

JSP

JSP(Java Server Pages)とは、ウェブアプリケーションの表示レイヤーに使われるサーバーサイドの技術のことです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

0グッド

0クリップ

投稿2019/12/16 12:16

編集2019/12/16 12:18

前提・実現したいこと

簡易掲示板を作成し、ログイン後に登録情報を設定画面で変更する際、パスワードやIDが未入力の場合は現状維持(現在のパスワードやIDを保持)させたいです。現在は、エラーメッセージが出るようになっています。1000文字以内ということなのでサーブレットは載せていません。

発生している問題・エラーメッセージ

エラーメッセージを出さないようにすることはできたのですが、未入力の場合空文字がパスワードに設定されてしまいます。SQLのUpdateやgetUserのメソッドの書き換えをしているうちに迷走してしまいました。

該当のソースコード

package chapter6.service; import static chapter6.utils.CloseableUtil.*; import static chapter6.utils.DBUtil.*; import java.sql.Connection; import chapter6.beans.User; import chapter6.dao.UserDao; import chapter6.utils.CipherUtil; public class UserService { public void register(User user) { Connection connection = null; try { connection = getConnection(); String encPassword = CipherUtil.encrypt(user.getPassword()); user.setPassword(encPassword); UserDao userDao = new UserDao(); userDao.insert(connection, user); commit(connection); } catch (RuntimeException e) { rollback(connection); throw e; } catch (Error e) { rollback(connection); throw e; } finally { close(connection); } } public User getUser(int userId) { Connection connection = null; try { connection = getConnection(); UserDao userDao = new UserDao(); User user = userDao.getUser(connection, userId); commit(connection); return user; } catch (RuntimeException e) { rollback(connection); throw e; } catch (Error e) { rollback(connection); throw e; } finally { close(connection); } } public void update(User user) { Connection connection = null; try { connection = getConnection(); String encPassword = CipherUtil.encrypt(user.getPassword()); user.setPassword(encPassword); UserDao userDao = new UserDao(); userDao.update(connection, user); commit(connection); } catch (RuntimeException e) { rollback(connection); throw e; } catch (Error e) { rollback(connection); throw e; } finally { close(connection); } } }
// DAOクラス package chapter6.dao; import static chapter6.utils.CloseableUtil.*; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import chapter6.beans.User; import chapter6.exception.NoRowsUpdatedRuntimeException; import chapter6.exception.SQLRuntimeException; public class UserDao { public void insert(Connection connection, User user) { PreparedStatement ps = null; try { StringBuilder sql = new StringBuilder(); sql.append("INSERT INTO users ( "); sql.append("account"); sql.append(", name"); sql.append(", email"); sql.append(", password"); sql.append(", description"); sql.append(", created_date"); sql.append(", updated_date"); sql.append(") VALUES ("); sql.append("?"); // account sql.append(", ?"); // name sql.append(", ?"); // email sql.append(", ?"); // password sql.append(", ?"); // description sql.append(", CURRENT_TIMESTAMP"); // created_date sql.append(", CURRENT_TIMESTAMP"); // updated_date sql.append(")"); ps = connection.prepareStatement(sql.toString()); ps.setString(1, user.getAccount()); ps.setString(2, user.getName()); ps.setString(3, user.getEmail()); ps.setString(4, user.getPassword()); ps.setString(5, user.getDescription()); ps.executeUpdate(); } catch (SQLException e) { throw new SQLRuntimeException(e); } finally { close(ps); } } public User getUser(Connection connection, String accountOrEmail, String password) { PreparedStatement ps = null; try { String sql = "SELECT * FROM users WHERE (account = ? OR email = ?) AND password = ?"; ps = connection.prepareStatement(sql); ps.setString(1, accountOrEmail); ps.setString(2, accountOrEmail); ps.setString(3, password); ResultSet rs = ps.executeQuery(); List<User> userList = toUserList(rs); if (userList.isEmpty() == true) { return null; } else if (2 <= userList.size()) { throw new IllegalStateException("2 <= userList.size()"); } else { return userList.get(0); } } catch (SQLException e) { throw new SQLRuntimeException(e); } finally { close(ps); } } public User getUser(Connection connection, int id) { PreparedStatement ps = null; try { String sql = "SELECT * FROM users WHERE id = ?"; ps = connection.prepareStatement(sql); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); List<User> userList = toUserList(rs); if (userList.isEmpty() == true) { return null; } else if (2 <= userList.size()) { throw new IllegalStateException("2 <= userList.size()"); } else { return userList.get(0); } } catch (SQLException e) { throw new SQLRuntimeException(e); } finally { close(ps); } } private List<User> toUserList(ResultSet rs) throws SQLException { List<User> ret = new ArrayList<User>(); try { while (rs.next()) { int id = rs.getInt("id"); String account = rs.getString("account"); String name = rs.getString("name"); String email = rs.getString("email"); String password = rs.getString("password"); String description = rs.getString("description"); Timestamp createdDate = rs.getTimestamp("created_date"); Timestamp updatedDate = rs.getTimestamp("updated_date"); User user = new User(); user.setId(id); user.setAccount(account); user.setName(name); user.setEmail(email); user.setPassword(password); user.setDescription(description); user.setCreatedDate(createdDate); user.setUpdatedDate(updatedDate); ret.add(user); } return ret; } finally { close(rs); } } public void update(Connection connection, User user) { PreparedStatement ps = null; try { StringBuilder sql = new StringBuilder(); sql.append("UPDATE users SET"); sql.append(" account = ?"); sql.append(", name = ?"); sql.append(", email = ?"); sql.append(", password = ?"); sql.append(", description = ?"); sql.append(", updated_date = CURRENT_TIMESTAMP"); sql.append(" WHERE"); sql.append(" id = ?"); ps = connection.prepareStatement(sql.toString()); ps.setString(1, user.getAccount()); ps.setString(2, user.getName()); ps.setString(3, user.getEmail()); ps.setString(4, user.getPassword()); ps.setString(5, user.getDescription()); ps.setInt(6, user.getId()); int count = ps.executeUpdate(); if (count == 0) { throw new NoRowsUpdatedRuntimeException(); } } catch (SQLException e) { throw new SQLRuntimeException(e); } finally { close(ps); } } }

試したこと

教材を読み進めてここまできました。サーブレットのif文を消すと空文字が設定できるのは分かるのですが、DAOとServiceクラスの変更点がいまいち分かりません。また、JSPのpostmethodから受け取った値はどこへいっているのでしょうか?
初心者の質問ですが、よろしくお願いします。

補足情報(FW/ツールのバージョンなど)

Tomcat7.0
java 8

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

A-pZ

2019/12/16 12:54

試したこと に書かれている ServletやJSPが書かれていないので、なんとも回答しにくい状況ですから、質問を編集して追記してください。
guest

回答1

0

ベストアンサー

すみません。単純にif文が間違っているだけでした。
ありがとうございました。

投稿2019/12/17 08:54

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問