无法在数据库中插入值结果值超出 DECIMAL/NUMERIC 数据类型 (3.1) 的范围

Unable to insert values in database The resulting value is out of range for the DECIMAL / NUMERIC data type (3.1)

当我 运行 我的代码时,它工作甚至显示这个错误:

java.sql.SQLDataException: The resulting value is out of range for the DECIMAL / NUMERIC data type (3.1).

我的 Java 文件:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Aula4 {
    public static void main (String[] args) throws ClassNotFoundException, SQLException {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            
            String url = "jdbc:derby://localhost:1527/sistema_academico";
            
            String usuario = "app";
            String senha = "app";
            Connection conexao;
            Scanner ler = new Scanner(System.in);
            conexao = DriverManager.getConnection(url, usuario, senha);
            int id_aluno;
            double nota;
            String nome = null;
            
            System.out.println("Welcome, type your id: ");
            id_aluno = ler.nextInt();
            
            System.out.println("Now, do you need to type your name: ");
            nome = ler.next();
            
            System.out.println("And finally, type your grade:  ");
            nota = ler.nextDouble();
            
            String SQL_Update = "UPDATE aluno set NOTA=? WHERE id_aluno=?";
            String sql = "insert into aluno " +
                  "(id_aluno,nome, nota) " +
                  "values (?,?,?)";
            PreparedStatement stmt = conexao.prepareStatement(sql);
            stmt.setInt(1, id_aluno);
            stmt.setString(2, nome);
            stmt.setDouble(3, nota);
            stmt.executeUpdate();
            stmt.execute();
            stmt.close();
            System.out.println("Accomplished!!");
            
            stmt = conexao.prepareStatement(sql);
            
            }
             catch (SQLException ex){
            System.out.println("Conection failed!"+ex);
        } 
    }
}

我可以输入所有内容,但最后,程序显示上面的错误。我想将值插入我的数据库,只是这个错误让我很不安。

stmt.execute(); 太多了,确实可能导致“错误”错误。 最后一个stmt = conexao.prepareStatement(sql);也是。

MySQL reference 可能会有所帮助。 DECIMAL(3, 1) 的范围是 -9.9 .. 9.9。 这可能是一个正确的比例,否则你可能想要 DECIMAL(5, 2).

nota 可能会被选中。

由于浮点数 (double) 不精确,在 java 侧使用 BigDecimal(定点)可能会更好。

        System.out.println("And finally, type your grade:  ");
        BigDecimal bdnota = ler.nextBigDecimal();

        // Check input: scale and digits
        System.out.println("Scale: " + bdnota.scale());

        stmt.setBigDecimal(3, bdnota);