1

I am writing a Java JDBC code to enter a record int he table, but I'am getting an error java.sql.SQLException: ORA-01438: value larger than specified precision allowed for this column. In my understanding I'am getting an error at class StudentDAO in statement int result = pst.executeUpdate(); Here is my code

 package com.wipro.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.wipro.util.DBUtil;
import com.wipro.util.Student;

//Crud operation logic
public class StudentDAO {

    public String insertStudent(Student Student17){
        String output = "fail";
        try{

            Connection conn = DBUtil.getDBConnection();
            PreparedStatement pst = conn.prepareStatement("insert into Student17(stdid,name,sub1,sub2,sub3,total,avg) values(?,?,?,?,?,?,?)");
            pst.setInt(1, Student17.getStdid());
            pst.setString(2, Student17.getName());
            pst.setInt(3, Student17.getSub1());
            pst.setInt(4, Student17.getSub2());
            pst.setInt(5, Student17.getSub3());
            pst.setInt(6, Student17.getTotal());
            pst.setInt(7, Student17.getAvg());

            //System.out.println("Output===== " + Student17.getTotal());
            //System.out.println("Output===== " + Student17.getAvg());

            //System.out.println("Output===== " + Student17.getName());

            //System.out.println("Output===== " + Student17.getSub1());

            //System.out.println("Output===== " + Student17.getSub2());

            //System.out.println("Output===== " + Student17.getSub3());

            int result = pst.executeUpdate(); 
            if(result != 0){
                output = "Success";
            }
            conn.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return output;
    }

}

package com.wipro.service;

import com.wipro.dao.StudentDAO;
import com.wipro.util.Student;

public class StudentService {
    public String InsertStudentService(Student Student17){
        String result = "Validation Error";
        if(Student17.getStdid()==0||Student17.getName()==null||Student17.getSub1()<=0||Student17.getSub2()<=0||Student17.getSub3()<=0)
            return result;
        else{
            int total  = Student17.getSub1() + Student17.getSub2() + Student17.getSub3();
            Student17.setTotal(total);
            Student17.setAvg(total/3);
            StudentDAO studentdao = new StudentDAO();
            studentdao.insertStudent(Student17);
            result = studentdao.insertStudent(Student17);
            return result;
        }
    }
}


package com.wipro.util;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtil {

    public static Connection getDBConnection() {
        Connection conn = null;
        try{
        Class.forName("oracle.jdbc.driver.OracleDriver");         

        conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","oracle");  

        }catch(Exception e){
            e.printStackTrace();
        }
        return conn;
    }

}

package com.wipro.util;

public class Student {
    private int stdid;
    private String name;
    private int sub1, sub2, sub3;
    private int total;
    private int avg;


    public void setStdid(int stdid) {
        this.stdid = stdid;
    }
    public int getStdid() {
        return stdid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSub1() {
        return sub1;
    }
    public void setSub1(int sub1) {
        this.sub1 = sub1;
    }
    public int getSub2() {
        return sub2;
    }
    public void setSub2(int sub2) {
        this.sub2 = sub2;
    }
    public int getSub3() {
        return sub3;
    }
    public void setSub3(int sub3) {
        this.sub3 = sub3;
    }
    public int getTotal() {
        return total;
    }
    public void setTotal(int total) {
        this.total = total;
    }
    public int getAvg() {
        return avg;
    }
    public void setAvg(int avg) {
        this.avg = avg;
    }

}


package com.wipro.main;

import com.wipro.service.StudentService;
import com.wipro.util.Student;

public class MainClass {
    public static void main(String[] args){
        Student Student17 = new Student();

        Student17.setStdid(10);
        Student17.setName("karan");
        Student17.setSub1(80);
        Student17.setSub2(90);
        Student17.setSub3(100);

        StudentService service = new StudentService();
        String result = service.InsertStudentService(Student17);
        System.out.println("Output: "+ result);

    }
}

The error I'am getting is

java.sql.SQLException: ORA-01438: value larger than specified precision allowed for this column

    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
    at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:955)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1169)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)
    at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3368)
    at com.wipro.dao.StudentDAO.insertStudent(StudentDAO.java:37)
    at com.wipro.service.StudentService.InsertStudentService(StudentService.java:16)
    at com.wipro.main.MainClass.main(MainClass.java:17)
java.sql.SQLException: ORA-01438: value larger than specified precision allowed for this column

    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
    at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:955)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1169)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)
    at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3368)
    at com.wipro.dao.StudentDAO.insertStudent(StudentDAO.java:37)
    at com.wipro.service.StudentService.InsertStudentService(StudentService.java:17)
    at com.wipro.main.MainClass.main(MainClass.java:17)
Output: fail

In my understanding I'am getting an error at class StudentDAO in statement int result = pst.executeUpdate(); Any Help is appreciated. Thanks in advance.

6
  • O think the message is prety clear. You add a value to a column, that is too large Commented Aug 19, 2016 at 9:20
  • 1
    ...and looking at the table-definition (which we can't do since you din't post it) you should be able to figure out which column it is. Commented Aug 19, 2016 at 9:25
  • This is the SQL command I used to create my table - create table Student17(stdid number(3) primary key,name varchar2(100),sub1 number(2),sub2 number(2), sub3 number(2),total number(3),avg number(3)); Commented Aug 19, 2016 at 9:27
  • so how would you expect to insert 100 (as in Student17.setSub3(100);) in the 2-digit column sub3? Commented Aug 19, 2016 at 9:29
  • Thank You 'piet.t'. I changed the number but now I'am gettin the error java.sql.SQLException: ORA-00001: unique constraint (SYSTEM.SYS_C004025) violated. But the records are being inserted. Commented Aug 19, 2016 at 9:39

1 Answer 1

1

The problem is in sub3 because is a number of 3 digits.

create table Student17(
    stdid number(3) primary key,
    name varchar2(100),
    sub1 number(2),
    sub2 number(2),
    sub3 number(2),
    total number(3),
    avg number(3)
); 
sub3 number(2)

Then on the code, change this: Student17.setSub3(100);

Sign up to request clarification or add additional context in comments.

2 Comments

Thank You 'edu'. I changed the number but now I'am geetin the error java.sql.SQLException: ORA-00001: unique constraint (SYSTEM.SYS_C004025) violated
@tj_lucas that's probably because you already inserted a record with student id 10 into your table. just change the id and run again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.