2016-10-31 20 views
0

在使用Java和Oracle運行我的查詢時,出現以下錯誤。使用Java和ORACLE獲取SQL異常錯誤

INFO :Server setup in 533 ms 

java.sql.SQLException :ORA-01400 :can not insert NULL into ("SYSTEM","CUSTOMER143","NAMEOFCUSTOMER") 

我在下面解釋我的查詢。

create table Customer143 
(
Id int not null, 
NameOfCustomer varchar(255) not null, 
Age int, 
Country varchar(255) not null, 
Address varchar(255) not null, 
Gender varchar(255) not null, 
Maritial_Status varchar(255)not null, 
primary key(Id) 
); 

create sequence sq2 start with 100 increment by 10; 

select * from Customer143; 

我的代碼如下。

package com.DAO; 

import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 

import com.model.Customer; 
import com.util.DBConnection; 

public class CustomerDAO 
{ 
    static PreparedStatement pst=null; 
    static Connection con=null; 

    public static int addCustomer(Customer cu) 
    { 
     int flag=0; 

     try { 
      con=DBConnection.getConnection(); 
      pst=con.prepareStatement("insert into Customer143 values(sq2.nextval,?,?,?,?,?,?)"); 

      pst.setString(1,cu.getName()); 
      pst.setInt(2,cu.getAge()); 
      pst.setString(3,cu.getCountry()); 
      pst.setString(4,cu.getAddress()); 
      pst.setString(5,cu.getGender()); 
      pst.setString(6,cu.getMaritialStatus()); 

      flag=pst.executeUpdate(); 

     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     finally 
     { 
      DBConnection.closeStatement(pst); 
      DBConnection.closeConnection(con); 
     } 
     return flag;    
    } 
    public static ArrayList<Customer> getCustomer(String namee) 
    { 
     ArrayList<Customer> customerList=new ArrayList<Customer>(); 
     try { 
      con=DBConnection.getConnection(); 
      pst=con.prepareStatement("select * from Customer143 where NameOfCustomer=?"); 

      pst.setString(1,namee);    
      ResultSet rs=pst.executeQuery(); 

      while(rs.next()) 
      { 
       String name=rs.getString(1); 
       int age=rs.getInt(2); 
       String country=rs.getString(3); 
       String address=rs.getString(4); 
       String gender=rs.getString(5); 
       String status=rs.getString(6); 

       Customer cu=new Customer(); 
       cu.setName(name); 
       cu.setAge(age); 
       cu.setCountry(country); 
       cu.setAddress(address); 
       cu.setGender(gender); 
       cu.setMaritialStatus(status); 

       customerList.add(cu);     
      }    
     }catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      DBConnection.closeStatement(pst); 
      DBConnection.closeConnection(con); 
     } 
     return customerList; 
    } 

請幫我解決這個錯誤。

+0

您的NameOfCustomer列被定義爲NOT NULL,儘管您嘗試向其中插入NULL值 – hammerfest

+0

您是否在文檔中查找了「ORA-01400」的解釋? –

+1

顯示insert sql命令代碼 – scaisEdge

回答

0

列出插入語句中的列名稱。現在,您只列出了值並且現在可以控制列的順序。