• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Its not asking me for name directly throughing SQLException please help

 
Suraj Bhosale
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


what is the mistake I am doing here
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the entire stack trace you're getting.
 
Suraj Bhosale
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Post the entire stack trace you're getting.

If I am directly inserting values its working like this

but when i am using the above code its asking fot Id value but doesn't waits for me to input name value as soon as i hit enter its give me below output

Enter the Student Id :1
Enter the Student Name :no of rows inserted :1
Press any key to continue . . .

 
Henry Wong
author
Marshal
Pie
Posts: 22086
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suraj Bhosale wrote:
Ulf Dittmer wrote:Post the entire stack trace you're getting.

If I am directly inserting values its working like this

but when i am using the above code its asking fot Id value but doesn't waits for me to input name value as soon as i hit enter its give me below output

Enter the Student Id :1
Enter the Student Name :no of rows inserted :1
Press any key to continue . . .



You said, in your original post, that it is an SQLException. Ulf asked for the stacktrace from the exception -- which can be obtained if you catch the exception and use the printStackTrace() method.

Henry
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the subject you seemed to say that a SQLException is thrown - that is not actually the case?

I'm assuming that you hit return after entering the ID - since line 15 only reads the ID (but not the return character), that is read in line 17, resulting in an empty name. If you check the DB, I think you'll find an empty name has been inserted.

I *think* (not sure, I've never used a Scanner for keyboard input) that you can get around that by entering both values on the same line like "42 Ford Prefect" and then hitting return.
 
Suraj Bhosale
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:In the subject you seemed to say that a SQLException is thrown - that is not actually the case?

I'm assuming that you hit return after entering the ID - since line 15 only reads the ID (but not the return character), that is read in line 17, resulting in an empty name. If you check the DB, I think you'll find an empty name has been inserted.

I *think* (not sure, I've never used a Scanner for keyboard input) that you can get around that by entering both values on the same line like "42 Ford Prefect" and then hitting return.


I got it just tried myself I got the right output this time I have done a little modification in my code

Now this time it is asking me to input the name value and also in database name value is inserted. This time I have read the name value and then assigned to string variable It works fine but In previous code I directly assigned the read name value then It was inserting null in database.. but I don't know why this scanner class is working in this way.. Can anybody explain me over here...
 
Henry Wong
author
Marshal
Pie
Posts: 22086
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suraj Bhosale wrote:
I got it just tried myself I got the right output this time I have done a little modification in my code

Now this time it is asking me to input the name value and also in database name value is inserted. This time I have read the name value and then assigned to string variable It works fine but In previous code I directly assigned the read name value then It was inserting null in database.. but I don't know why this scanner class is working in this way.. Can anybody explain me over here...


The issue here is that you are making assumptions on how the Scanner class should work. You should not do that. You should follow the rules as defined by the JavaDoc on how the Scanner class should work.

The nextInt() method gets a token and tries to convert it to an integer. It doesn't consume the newline as part of the token. This means when the user types the student id and press return, there is still a newline left in standard input.

Henry

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic