• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jdbc connection

 
Singh Anisha
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???
 
Eshwin Sukhdeve
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
 
sri ramvaithiyanathan
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


 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • 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.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • 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.
 
Wendy Gibbons
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic