This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

jdbc connection

 
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I m trying this simple program to connect to MYSQL database but its giving two error.
Cannot find the symbol(con,stmt)
Whats wrong with this???
 
Ranch Hand
Posts: 90
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have declared con inside try block..declared it outside of try block..stmt shouldn't be a problem
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anisha,

Welcome to code ranch.
Do coding inside try block.

executeupdate will return int value.
So replace



Use catch block properly, don't leave it empty.
Just place e.printStackTrace();
inside catch block.

It will print error details. So that you will understand the problem clearly.

And finally, paste coding inside code block.

Regards,
Sriram.V


 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Singh Anisha wrote:
String insert= "INSERT INTO student_profile VALUES('"+first_name+"','"+last_name+"','"+course+"')";



You should use bind variables in a PreparedStatement instead of just concatenating string values as your values to be inserted. This will help protect you from SQL injection attacks. (Does nobody teach this to Java programmers any more?)

Also, it is not a good idea for your application to log in as "root", as the root user can do anything on your database: you should have a separate user for your application. Presumably this is just a student project, but in the real world you would be creating big security holes here.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Singh!

I've added the code tags for you this time - see how nice the code looks now?

You should also learn to use the PreparedStatement - among other things it helps you prevent SQL injection attacks. See this tutorial, and perhaps also this page which sums up all the benefits of PreparedStatement.
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Martin Vajsar wrote:Welcome to the Ranch, Singh!

I've added the code tags for you this time - see how nice the code looks now?

You should also learn to use the PreparedStatement - among other things it helps you prevent SQL injection attacks. See this tutorial, and perhaps also this page which sums up all the benefits of PreparedStatement.


and to add to martins list, my own personal favourite

always use a column list
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you.
Thanks ramvaithiyanathan alot followes your suggestion and yipieeee!!
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!