Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A sample JDBC program which combines basic SQL functions (useful for students)

 
Vishal Pandit
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//Let me know in case of any difficulties... Will run in an IDE only.

 
Jan Cumps
Bartender
Posts: 2608
14
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishal. Welcome to the Ranch!

There are some quality issues with your example. I'm not going through all of them.
You close the resources on multiple places in a method. This is best done in a finaly block.
Your code is pone to sql injection. Try using a PreparedStatement.
You use "if(rs == null)" to check if a table is empty. An executeQuery() will return a ResultSet even if the table is empty.

Regards, Jan
 
Vishal Pandit
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jan,

Thank you for warm welcome and valuable feedback:
1) As you suggested, I used PreparedStatement. It’s easier and more efficient!
2) Yes, I should have used” finally” instead.
3) I will skip “rs==null” part now and will try to find out a better way to accomplish this.

As a learner, this suggestions are of a great help to me.
Thank you, Jan.

Regards,
Vishal
 
Nam Ha Minh
Ranch Hand
Posts: 508
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishal Pandit,

Thanks for sharing your code. But what do you mean by this:

Will run in an IDE only.
 
Vishal Pandit
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nam,

I meant the code will run in a workspace like Eclipse or Netbeans. I tried running it in the traditional way (by writing the code in a notepad file with .java extension and running it in command prompt) but it dint work.

Sorry for the confusion. Just a beginner...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic