使用Java和ORACLE获取SQL异常错误

时间:2016-10-31 12:49:54

标签: java oracle

使用Java和Oracle运行查询时出现以下错误。

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;
    }

请帮我解决此错误。

1 个答案:

答案 0 :(得分:0)

列出insert语句中的列名。现在您只列出值,现在可以控制列的顺序。