Solved

Database and Servlets To Msterjev (Values in my Database for the insertion problem )

Posted on 2002-04-04
3
134 Views
Last Modified: 2013-11-24
OrderNumber = autonumber;
ItemCode = text;
Category = text;

and all the other fields have a text value except the currency . There are 8fields and I only want to send 6 of them to the database and leave 2(i.e. OrderNumber,Status )



>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
I still have a problem with the insertion of other values where the primary key is auto number in the database. The thing is if I assign as below I get the message

You tried to assign the Null value to a variable that is not a Variant data type.

AND


If I leave them blank this is what I get

Syntax error in INSERT INTO statement.

but the insertin part is OKay right.

Could you please help with the sample code of doing this.


String SQL = "INSERT INTO KeepOrdersTable ItemCode,OrderNumber,Category,AnimalType,AgeGroup,UnitPrice,Quantity,Status VALUES(?,?,?,?,?,?,?,?)";

try{

 PreparedStatement  stmt = conn.prepareStatement(SQL) ;

       stmt.setString(1,  null);
        stmt.setString(2,  ItemCode);
        stmt.setString(3,  Category);
        stmt.setString(4,  AnimalType);
        stmt.setString(5,  AgeGroup);
        stmt.setString(6,  UnitPrice);
        stmt.setString(7,  Quantity);
        stmt.setString(8,  null);

       stmt.executeUpdate();

}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
0
Comment
Question by:wbmorapedi
3 Comments
 
LVL 3

Expert Comment

by:msterjev
ID: 6917592
Coud you send the database field description:
For example:

field1: integer; default value: 0;

etc.

The rule is you never insert autoincrement primary key field (I've alredy answered this question to you). Open MS Access and if you want some field to accept null values check it (but you never send null values into the INSERT statements, just omit this field from INSERT statement). Or, specify some default value for some field if you don't want to use this field into INSERT statement.

DON'T SEND null VALUES INTO THE INSERT STATEMENT!
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6917919
>DON'T SEND null VALUES INTO THE INSERT STATEMENT!

Curious as to why not?  I think I do that someplaces without problems.. but I use setNull method.

CJ
0
 
LVL 4

Accepted Solution

by:
pellep earned 50 total points
ID: 6919628
String SQL = "INSERT INTO KeepOrdersTable ItemCode,OrderNumber,Category,AnimalType,AgeGroup,UnitPrice,Quantity,Status
VALUES(?,?,?,?,?,?,?,?)";

should be

String SQL = "INSERT INTO KeepOrdersTable (ItemCode,OrderNumber,Category,AnimalType,AgeGroup,UnitPrice,Quantity,Status)
VALUES(?,?,?,?,?,?,?,?)";

apart from that, I agree with cheekycj and msterjev. I'd like to know which JDBC driver/database you're using that allows
- null values in inserts
- including identity fields (OrderNumber) in the insert statement

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now