簡體   English   中英

為什么我的查詢僅適用於 number<10?

[英]Why does my query works only for number<10?

我在輸入數據時使用這個函數。 所以我需要檢查數據庫中是否有具有此 ID 的員工。 我使用相同的 function 進行注冊,此方法有效。 但是當涉及到 integer 數字時,它僅在小於 10 時檢查(或者可能是第一個插入的值)這是我檢查 id 唯一性的代碼:

private int checkUnique() {
    try {
        Scanner scan = new Scanner(System.in);
        id = scan.nextInt();
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:...", "...", "...");
        Statement st = connection.createStatement();
        ResultSet res = st.executeQuery("select id_emp from employees");
        while (res.next()) {
            if (res.getInt("id_emp")==getId()) {
                res.close();
                st.close();
                connection.close();
                System.out.println("There is employee with this id");
                System.out.println("Enter id");
                checkUnique();
            } else {
                res.close();
                st.close();
                connection.close();
                return id;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}

這就是我在代碼中使用它的方式:

Statement st = connection.createStatement();
String sql = "INSERT INTO employees (id_emp, first_name, last_name, cnt_kids, cnt_dkids,is_single,added_by) " +
        "VALUES (?, ?, ?, ?, ?,?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
System.out.println("Enter id");
id = checkUnique();

這有什么問題嗎?

例如,此代碼要求在 id=2 時輸入其他 id(它確實在表中),但是當我插入 id=12(也在表中)時它只是跳過。 我的表:

CREATE TABLE `employees` (
  `id_emp` int NOT NULL,
  `first_name` varchar(30) DEFAULT NULL,
  `last_name` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `cnt_kids` int DEFAULT NULL,
  `cnt_dkids` int DEFAULT NULL,
  `is_single` bit(1) DEFAULT NULL,
  `added_by` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  PRIMARY KEY (`id_emp`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

使用准備好的語句

這是一些示例代碼:

public class SQLMain {
    public static void main(String[] args) throws SQLException {
        System.out.println("ID to check:");
        Scanner scanner = new Scanner(System.in);
        int id = scanner.nextInt();
        
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
        PreparedStatement preparedStatement = connection.prepareStatement("select id_emp from employees where id_emp = ?");
        preparedStatement.setInt(1, id);
        
        ResultSet resultSet = preparedStatement.executeQuery();
        if(resultSet.next()) {
            // we have a row
            System.out.println("Found employee with id of: " + resultSet.getInt(1));
        } else {
            // no row found - therefore unique 
            System.out.println("not found");
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
        scanner.close();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM