更新数据库中的图片

时间:2018-12-30 23:29:26

标签: java

enter image description here 你好!我正在为学生数据库创建一个应用程序。最近,当我想更改与特定学生相关的数据时遇到问题。当我想更改特定学生的照片时,尤其会出现问题。我需要检查图片是否属于学生。如果更改图片,我会将图片存储到文件夹中,然后删除上一张并创建新的图片。我的问题是如何检查图片是否属于特定学生?

我以这种方式检查学生。

    // get the name of the student from first table getValueTableName

    // get the name of the picture from first table getValueTablePicture

    getValueTableName = jTable1.getModel()
            .getValueAt(jTable1.getSelectedRow(), 0).toString();

    getValueTablePicture = jTable1.getModel()
            .getValueAt(jTable1.getSelectedRow(), 3).toString();

    File sourceFile = new File(getValueTablePicture);

    setPicture = sourceFile.getName();

    // GET NAME OF THE STUDENT AND THE PICTURE FROM DATABASE AND COMPARE
    // THEM TO THE CURRENT USER
    try {
        CallableStatement statement = null;
        Connection data = getmyConnection();
        statement = data.prepareCall("{call editStudentByName}");

        myResults = statement.executeQuery();

        while (myResults.next()) {
            // COPY PATH IN getEditName
            getEditName = myResults.getString("Name");
            // COPY PATH IN getEditPicture
            getEditPicture = myResults.getString("Picture");

            // add students from database to array
            // mylist.add(getEditName.concat(getEditPicture));
            mylist.add("\n");

        }

        myResults.close();

    } catch (Exception c) {
        c.printStackTrace();
    }

    // I don't know how to move from this point when I check names with loop
    // I check the student with the loop
    for (String person : mylist) {
        if (getValueTableName.concat(sourceFile.getName()).equals(person) == true) {

        }
        System.out.print(getValueTableName.concat(sourceFile.getName())
                .equals(person));
        errors.append(
                "- Please choose another picture or rename it!\n Picture ")
                .append(getEditPicture)
                .append(" is exist for a student " + getEditName)
                .append("\n");
        jTextField3.requestFocusInWindow();
        jTextField3.setText("");

    }

2 个答案:

答案 0 :(得分:2)

我要做的第一件事就是不使用带有诸如getEditName之类的奇怪名字的单独的字符串-这很令人困惑。考虑拥有POJO(Student.class)并使用它

  1. 所以您要替换单个学生图片?为什么在这种情况下必须迭代一些数组?您应该从数据库中获得一个学生(通过ID或通过一些唯一的属性集)。
  2. 好,可以说您有一个学生名单,并且要遍历它。但是,您仍然必须为单人更改图片,因此无需检查。 只需

    String pictureFileName = person.getPicture();//assming getPicture() method returns current picture path
    

    ,然后用相同的名称保存新图片。在这种情况下,旧图片将被覆盖,因此检查没有问题。

UPD: 如果要检查图片是否存在,可以执行以下操作:

String pictureFileName = person.getPicture();
File f = new File(pictureFileName );
if(f.exists() && !f.isDirectory()) { 
    // do something, say report warning
}

UPD: 如果您不要求学生具有共享图片的能力,则最好通过此https://www.w3schools.com/sql/sql_unique.asp在数据库级别上实现此目的-这样就无法写出两个不同的学生记录具有相同的图片路径字段。在这种情况下,检查不再重要,您可以直接覆盖图片文件,因为它仅属于单个学生

答案 1 :(得分:2)

最后,我把事情做好了。那是一个非常痛苦的时刻,但是我得到了我想要的。该问题隐藏在数据库的“学生”表中的“图片”列中。首先,我添加UNIQUE约束以确保列中的所有值都不同。其次,我创建了两个存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `checkStudentByPicture`(
in picture_name varchar(100)
)
BEGIN

SELECT COUNT(*) FROM students_center.Student st WHERE st.Picture = picture_name;
END

第一个过程检查我的列是否具有唯一名称,并且不允许在该列中添加相同的名称。

然后我创建了第二个:

CREATE DEFINER=`root`@`localhost` PROCEDURE `getStudentNameByPicture`(
in name varchar(45),
in pic_name varchar(100)
)
BEGIN

SELECT COUNT(*) FROM students_center.Student st WHERE st.Name = name and st.Picture=pic_name;
END

第二个过程检查“图片”列是否与“名称”列相关。如果“图片”列不相关,则用户不允许更改名称。

以下是有关检查我的数据是否与上下文相关的代码:

private boolean validateFieldEditStudent() {
    StringBuilder errors = new StringBuilder(); 

    // call stored procedure checkStudentByPicture                        
    File sourceFile = new File(jTextField3.getText()); 
    String checkStudentName=jTable2.getValueAt(jTable2.getSelectedRow(), 0).toString(); 

    try {
        CallableStatement statement = null;
        Connection data = getmyConnection(); 
        statement = data.prepareCall("{call checkStudentByPicture(?)}");

        statement.setString(1, sourceFile.getName());
        myResults = statement.executeQuery();

        while (myResults.next()) {
            //COPY PATH IN pictureName 
            getPictureCount = myResults.getInt(1);
        }

        myResults.close();
     } catch (Exception c) {
        c.printStackTrace();
     } 
}

// call stored procedure checkStudentByPicture 
try {
    CallableStatement statement = null;
    Connection data = getmyConnection(); 
    statement = data.prepareCall("{call getStudentNameByPicture(?, ?)}");

    statement.setString(1, checkStudentName);
    statement.setString(2, sourceFile.getName());
    myResults = statement.executeQuery();

    while (myResults.next()) {
        //COPY PATH IN pictureName 
        getStudentNameCount = myResults.getInt(1);
    }

    myResults.close();
} catch (Exception c) {
    c.printStackTrace();
}

//check if data is related to the specific user

if(getFileChooserCount > 0) {
       if(getStudentNameCount != 1) {
           if(getPictureCount == 1) {
            errors.append("- Picture "+sourceFile.getName()+" existed in the database!\n");
        jTextField3.setText("");
        jTextField3.requestFocusInWindow();   
        }
    }  
}

 if (errors.length() > 0) {
           JOptionPane.showMessageDialog(EditStudent, errors, "Warning!", JOptionPane.ERROR_MESSAGE); 
            return false;
        }
    return true;
}