質問編集履歴
2
テーブル作成済みの旨記載
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            サーブレットからPostgreSQLにJDBCを使用してデータを登録したいのですが。
         | 
| 2 2 | 
             
            シーケンスを使用して登録する方法がわからず、ご教授いただきたいです。
         | 
| 3 | 
            +
            ※シーケンスにより自動採番されたテーブルは作成済みです
         | 
| 3 4 |  | 
| 4 | 
            -
             | 
| 5 5 | 
             
            ```java
         | 
| 6 6 |  | 
| 7 7 |  | 
1
シーケンス不使用のコード掲載しました
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -1,2 +1,59 @@ | |
| 1 1 | 
             
            サーブレットからPostgreSQLにJDBCを使用してデータを登録したいのですが。
         | 
| 2 | 
            -
            シーケンスを使用して登録する方法がわからず、ご教授いただきたいです。
         | 
| 2 | 
            +
            シーケンスを使用して登録する方法がわからず、ご教授いただきたいです。
         | 
| 3 | 
            +
             | 
| 4 | 
            +
             | 
| 5 | 
            +
            ```java
         | 
| 6 | 
            +
             | 
| 7 | 
            +
             | 
| 8 | 
            +
            import java.sql.Connection;
         | 
| 9 | 
            +
            import java.sql.DriverManager;
         | 
| 10 | 
            +
            import java.sql.PreparedStatement;
         | 
| 11 | 
            +
            import java.sql.ResultSet;
         | 
| 12 | 
            +
            import java.sql.SQLException;
         | 
| 13 | 
            +
            import java.util.ArrayList;
         | 
| 14 | 
            +
            import java.util.List;
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            import model.Question;
         | 
| 17 | 
            +
             | 
| 18 | 
            +
             | 
| 19 | 
            +
            public class QestionDAO {
         | 
| 20 | 
            +
            // データベースに使用する情報
         | 
| 21 | 
            +
             private final String jdbc_url ="jdbc:postgresql://localhost:5432/QandA";
         | 
| 22 | 
            +
             private final String user = "postgres";
         | 
| 23 | 
            +
             private final String pass = "abcd";
         | 
| 24 | 
            +
             
         | 
| 25 | 
            +
            static{
         | 
| 26 | 
            +
             try{Class.forName("org.postgresql.Driver");
         | 
| 27 | 
            +
            } catch(ClassNotFoundException e) {
         | 
| 28 | 
            +
             e.printStackTrace();
         | 
| 29 | 
            +
             }
         | 
| 30 | 
            +
            }
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            public boolean create(Question question) {
         | 
| 33 | 
            +
            //  データベース接続
         | 
| 34 | 
            +
            try(Connection conn = DriverManager.getConnection(jdbc_url, user, pass)) {
         | 
| 35 | 
            +
            //  INSERT文の準備
         | 
| 36 | 
            +
            String sql = "INSERT INTO question(hanle_name, title, contents, urgency, edit_delete_key, regist_timestamp, update_timestamp) VALUES(?, ?, ?, ?, ?, ?, ?)";
         | 
| 37 | 
            +
             | 
| 38 | 
            +
            PreparedStatement pStmt = conn.prepareStatement(sql);
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            //INSERT文の「?」に使用する値を設定しSQLを完成
         | 
| 41 | 
            +
            pStmt.setString(1, question.getHandle_name());
         | 
| 42 | 
            +
            pStmt.setString(2, question.getTitle());
         | 
| 43 | 
            +
            pStmt.setString(3,question.getContents());
         | 
| 44 | 
            +
            pStmt.setInt(4, question.getUrgency());
         | 
| 45 | 
            +
            pStmt.setString(5, question.getEdit_delete_key());
         | 
| 46 | 
            +
            pStmt.setString(6, question.getRegist_timestamp());
         | 
| 47 | 
            +
            pStmt.setString(7, question.getUpdate_timestamp());
         | 
| 48 | 
            +
             | 
| 49 | 
            +
            //INSERT文を実行
         | 
| 50 | 
            +
            int result  = pStmt.executeUpdate();
         | 
| 51 | 
            +
            if(result != 1) {
         | 
| 52 | 
            +
             return false;
         | 
| 53 | 
            +
             }
         | 
| 54 | 
            +
            } catch (SQLException e){
         | 
| 55 | 
            +
             e.printStackTrace();
         | 
| 56 | 
            +
            }
         | 
| 57 | 
            +
            return true;
         | 
| 58 | 
            +
            }
         | 
| 59 | 
            +
            ```
         | 
