2016-11-29 66 views
0

您好我已經在包寫一個程序,插入數據如下:甲骨文數字或值錯誤PL/SQL

create or replace package Proj2_student_reg_package is 
    type data_cursor is ref cursor; 

procedure add_students(sidIn IN students.sid%type, fnameIn IN students.firstname%type, 
         lnameIn IN students.lastname%type, statusIn IN students.status%type, 
         gpaIn IN students.gpa%type, emailIn IN students.gpa%type, 
         ret_message OUT varchar2); 

end Proj2_student_reg_package; 
/

create or replace package body Proj2_student_reg_package as 

procedure add_students(sidIn IN students.sid%type, fnameIn IN students.firstname%type, 
         lnameIn IN students.lastname%type, statusIn IN students.status%type, 
         gpaIn IN students.gpa%type, emailIn IN students.gpa%type, 
         ret_message OUT varchar2) 
is 
begin 
insert into students values(sidIn ,fnameIn,lnameIn,statusIn,gpaIn,emailIn); 
COMMIT; 
ret_message := 'Success'; 

EXCEPTION 
WHEN OTHERS THEN 
ret_message := 'Error'; 
end add_students; 




end Proj2_student_reg_package; 
​ 

,我通過一個Java程序調用:

import java.sql.CallableStatement; 
import java.sql.Connection; 
import java.sql.SQLException; 
import java.sql.Types; 
import java.util.Scanner; 

import oracle.jdbc.OracleTypes; 
import oracle.jdbc.pool.OracleDataSource; 

public class AddStudent { 
    public void addStudent(){ 
     Scanner line = new Scanner(System.in); 
     try 
     {  

      //Connection to Oracle server 
      OracleDataSource ds = new oracle.jdbc.pool.OracleDataSource(); 
      ds.setURL("jdbc:oracle:thin:@localhost:1521:xe"); 
      Connection conn = ds.getConnection("*****", "****"); 

      System.out.print("Enter the sid: "); 
      String sid = line.next(); 
      System.out.print("Enter first name: "); 
      String fname = line.next(); 
      System.out.print("Enter last name: "); 
      String lname = line.next(); 
      System.out.print("Enter status: "); 
      String status = line.next(); 
      System.out.print("Enter gpa: "); 
      String gpa = line.next(); 
      System.out.print("Enter email: "); 
      String email = line.next(); 


      CallableStatement cs = conn.prepareCall("{call Proj2_student_reg_package.add_students(?,?,?,?,?,?,?)}"); 

      cs.setString(1, sid); 
      cs.setString(2, fname); 
      cs.setString(3, lname); 
      cs.setString(4, status); 
      cs.setString(5, gpa); 
      cs.setString(6, email); 
      cs.registerOutParameter(7, Types.VARCHAR); 

      cs.executeQuery(); 
      System.out.println(cs.getString(7)); 
      cs.close(); 
      //line.close(); 
     } 
     catch (SQLException e) { System.out.println("SQLException " + e.getMessage());} 
     catch (Exception e) {System.out.println(e);} 
    } 

} 

我的學生表:

create table students (sid char(4) primary key check (sid like 'B%'), 
firstname varchar2(15) not null, lastname varchar2(15) not null, status varchar2(10) 
check (status in ('freshman', 'sophomore', 'junior', 'senior', 'graduate')), 
gpa number(3,2) check (gpa between 0 and 4.0), email varchar2(20) unique); 

我給從Java程序中的值SID:B987, fname : akki, lname : aror, satus : junior , gpa : 3,email : [email protected]

但是,這是給我的錯誤:

SQLException ORA-06502: PL/SQL: numeric or value error: character to number conversion error 
ORA-06512: at line 1 

我試圖找到太多,但我無法找出其中的錯誤是。 任何人都可以告訴我這個錯誤,並幫助我解決錯誤。

+0

對於初學者來說,'sid'是'char(4)',但是你正在試圖發送一個5個字符的字符串。這可能會或可能不會導致這個特定的錯誤,但這將是一個問題。 –

+0

此外,請花些時間在您的帖子中正確設置代碼和數據的格式。當要求某人的幫助時,讓他們很容易幫助你。 –

+0

@JimGarrison我輸入錯誤,我正在以s98發送sid。 – Akshay

回答

1

在你CallableStatementcs對象,您需要更改從cs.setString(5, gpa)cs.setDouble(5, gpa);gpa是在數據庫number類型。

+0

我試過但錯誤沒有解決。我不認爲這是問題,因爲我已經將gpa聲明爲字符串,並以字符串形式發送,並在過程中從%類型轉換。 – Akshay

+0

看看你的insert語句:'insert into students values(sidIn,fnameIn,lnameIn,statusIn,gpaIn,emailIn);'那裏沒有轉換,所以你需要通過gpa as setDouble如上所示 – developer

+0

錯誤沒有修復。我以gpa作爲輸入。 – Akshay