我正在编写一个Java JDBC代码来输入表中的记录,但是我收到错误 java.sql.SQLException:ORA-01438:大于此列允许的指定精度的值。根据我的理解,我在类StudentDAO 中收到错误 int result = pst.executeUpdate(); 这是我的代码
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);
}
}
我得到的错误是
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
根据我的理解,我在声明 int result = pst.executeUpdate(); 中类StudentDAO 时收到错误 任何帮助表示赞赏。提前致谢。
答案 0 :(得分:1)
问题在于sub3,因为它是3位数。
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)
然后在代码上更改此内容:Student17.setSub3(100);