java - 更新数据库中的图片

标签 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("");

    }

最佳答案

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

  1. 那么您想替换单张学生照片吗?为什么在这种情况下必须迭代一些数组?您应该从数据库中获取单个学生(通过 ID 或通过一些独特的属性集)。
  2. 好吧,假设您有一个学生列表,并且您对其进行迭代。但是单人还是要换照片,这样就不用检查了。 简单地做

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

    然后用相同的名称保存新图片。在这种情况下,旧图片将被覆盖,这样检查就不会出现问题。

更新: 如果您想检查图片是否存在,您可以执行相同的操作:

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

更新: 如果您不要求学生能够共享与图片相同的文件,最好也通过此 https://www.w3schools.com/sql/sql_unique.asp 在数据库级别实现此功能。 - 这样您就无法使用相同的图片路径字段写入两个不同的学生记录。在这种情况下,检查不再重要,您可以简单地覆盖图片文件,因为它只属于单个学生

关于java - 更新数据库中的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53982270/

相关文章:

java - 赋值是通过引用?

java - 如何更改 Eclipse 设置(对于 Java)以在方法/类声明之后将括号放在行上?

java - 迭代 EnumMap 不会导致每次迭代都创建新对象

java - getListenerList 或 getListeners

java - 在 java 中创建样式 xml 以供查看(按钮)

java - 如何控制 Google Android map 上 OverlayItem 的大小?

Java 定时器计数太快

Java - 每次迭代后重置数组

java - 将句子中用破折号分隔的三个或多个数字之间的破折号字符替换为空格

java - 如何使用Future<T>接口(interface)?