This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Succeeding with AI and have Veljko Krunic on-line!
See this thread for details.
Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

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

 
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.

 
Bartender
Posts: 2658
19
Netbeans IDE C++ Linux
  • 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
 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • 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...
 
    Bookmark Topic Watch Topic
  • New Topic