Click here to Skip to main content
14,691,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When I try to insert the data in the Student table that involve one of the data in other table (example: Programme) right, the dialog display "Please enter the particular details" message after I google but not suitable with it.

What I have tried:

Can check the problem insert statement only and explain the error to me, please?

Example for Programme table:

CREATE TABLE "PROGRAMME" (
    "CODE" VARCHAR(3) not null primary key,
     "NAME" VARCHAR(80),
     "FACULTY" VARCHAR(5)
  );

INSERT INTO PROGRAMME VALUES ('IA', 'Information Systems Engineering', 'FASC');
INSERT INTO PROGRAMME VALUES ('IB', 'Business Information Systems', 'FASC');

Example for Student table:

CREATE TABLE "STUDENT" (
    "ID" VARCHAR(10) not null primary key,
    "IC" VARCHAR(14) unique,
    "NAME" VARCHAR(50),
    "LEVEL" CHAR(1),
    "PROGRAMMECODE" VARCHAR(3),
    "YR" INTEGER,
    foreign key ("PROGRAMMECODE") references "PROGRAMME"("CODE")
);


Program:

package ui;

import control.MaintainProgrammeControl;
import control.MaintainStudentControl;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import domain.Programme;
import domain.Student;
import java.util.logging.*;

public class MaintainStudentFrame extends JFrame {
    private String host = "jdbc:derby://localhost:1527/collegedb";
    private String user = "nbuser";
    private String password = "nbuser";
    private final String tableName = "Student";
    private final String tableName2 = "Programme";
    private Connection conn;
    private PreparedStatement stmt;
    
    private JTextField jtfID = new JTextField();
    private JTextField jtfIC = new JTextField();
    private JTextField jtfName = new JTextField();
    private JTextField jtfLevel = new JTextField();
    private JTextField jtfProgCode = new JTextField();
    private JTextField jtfYear = new JTextField();
    
    private JButton jbtAdd = new JButton("Create");
      
    private MaintainStudentControl msControl;
    
    public MaintainStudentFrame() {
        msControl = new MaintainStudentControl();
        
        JPanel jpCenter = new JPanel(new GridLayout(6, 2));
        
        jpCenter.add(new JLabel("Student ID"));
        jpCenter.add(jtfID);
        jpCenter.add(new JLabel("Student IC"));
        jpCenter.add(jtfIC);
        jpCenter.add(new JLabel("Student Name"));
        jpCenter.add(jtfName);
        jpCenter.add(new JLabel("Level"));
        jpCenter.add(jtfLevel);
        jpCenter.add(new JLabel("Programme Code"));
        jpCenter.add(jtfProgCode);
        jpCenter.add(new JLabel("Year"));
        jpCenter.add(jtfYear);
        
        add(jpCenter);
        
        JPanel jpSouth = new JPanel();
        jpSouth.add(jbtAdd);
        jpSouth.add(jbtRetrieve);
        jpSouth.add(jbtUpdate);
        jpSouth.add(jbtDelete);
        add(jpSouth, BorderLayout.SOUTH);
        
        jbtAdd.addActionListener(new AddListener());

        createConnection();   
    }
    
    public Student selectRecord(String ID) {
        String queryStr = "SELECT * FROM " + tableName + " WHERE id = ?";
        
        try(Connection conn = createConnection();
                PreparedStatement stmt = conn.prepareStatement(queryStr);){
        
        stmt.setString(1,ID);
        
        try(ResultSet rs = stmt.executeQuery()){
            Programme prog = new Programme();
            
            if(rs.next()){
                prog.setCode(rs.getString(5));
                return new Student(ID, rs.getString(2), rs.getString(3), rs.getString(4).charAt(0), prog, rs.getInt(6));
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
        
    }   catch (SQLException ex) {
            Logger.getLogger(MaintainStudentFrame.class.getName()).log(Level.SEVERE, null, ex);
        } return null;
    }
    
    public boolean insertRecord(String ID, String IC, String name, char level, Student student, int year){

        String queryStr = "insert into " + tableName + " (id,ic,name,level,progCode,year) values (?,?,?,?,?,?)"; <--- Display please enter particular details message afte I enter the data many times

         try(Connection conn = createConnection();
            PreparedStatement stmt = conn.prepareStatement(queryStr);){
            stmt.setString(1,ID);
            stmt.setString(2,IC);
            stmt.setString(3,name);
            stmt.setString(4,String.format("%c", level));
            stmt.setString(5,student.getProgramme().getCode());
            stmt.setString(6,String.format("%d", year));
            return stmt.executeUpdate() > 0;
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return false;
    }
    
    private class AddListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
          
           Student stud = selectRecord(jtfProgCode.getText());
           
           if(insertRecord(jtfID.getText(), jtfIC.getText(), jtfName.getText(), jtfLevel.getText().charAt(0), stud, Integer.parseInt(jtfYear.getText()))){
               JOptionPane.showMessageDialog(null, "YOUR NEW RECORD IS CREATED AND ADDED", "ADD RECORD", JOptionPane.INFORMATION_MESSAGE);        
           }
           else{
               JOptionPane.showMessageDialog(null, "PLEASE ENTER THE PARTICULAR DETAIL", "ADD RECORD", JOptionPane.INFORMATION_MESSAGE);
           }
        }
    }
      
    private Connection createConnection() {
        try {
            conn = DriverManager.getConnection(host, user, password);
            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
        return conn;
    }
    
    private void shutDown() {
        if (conn != null)
            try {
            conn.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    public static void main(String[] args) {
        MaintainStudentFrame frm = new MaintainStudentFrame();
        frm.setTitle("Student CRUD");
        frm.setSize(600, 200);
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }
}
Posted
Updated 22-Oct-20 3:20am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900