• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem With Inserting

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So here's the code:



It compiles fine, but when I try to run it, an exception is thrown.



I'm REALLY new with JDBC, so I have no idea what this "Too few parameters" deal is all about.

Thanks!
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's never too early to learn how to use prepared statements:

Tutorial : Using Prepared Statements
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The MS driver and/or database is really really really stupid in certain repects; it reports that error in way too many cases when the real problem is either bad SQL or a mismatch between the SQL and the table structure.

In your case, it's bad SQL, change \"Hello\" to 'Hello'.

Actually, now is the time to learn one of the most important things you need to know about JDBC and the number one beginner mistake - you should usually use a PreparedStatement instead of a Statement. The reasons are many and very very very substantial; performance, security, and ease of coding top the list. For example, Google for: JDBC "sql injection"

Here's how you should do it:


[ February 15, 2006: Message edited by: stu derby ]
 
Brandon Tom
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool. The book I have is just getting to prepared statements.
 
Brandon Tom
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oddly enough, I'm getting an error that's saying that the JDBC-ODBC bridge does not support prepared statements =(
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stus advice on using PreparedStatement is a good one. Here's another one: Avoid the JDBC/ODBC bridge. It's slow, it's buggy, it doesn't support a number of JDBC features, and doesn't multi-thread well, if at all. Use a real driver (unless you're using Access, in which case I think there aren't any other drivers).
 
Brandon Tom
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am, in fact, using Access, but really only for learning purposes. All is not lost however, I'm downloading MS SQL Express Server 2005 and the 350 megabyte .NET Framework 2.0 SDK required to run it on right. *twiddles thumbs patiently*

I guess I could always just download MySQL. There's a type 4 JDBC for that right?
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brandon Tom:
I guess I could always just download MySQL. There's a type 4 JDBC for that right?



Yep. There are/were a dozen or so other drivers for Access of varying quality, but I think they're all commercial. Sun has a partial driver list here:
http://developers.sun.com/product/jdbc/drivers

Other good free choices for self education that I know of these days are:

Totaly free (as in beer):
Postgres (some people swear it's better than mysql, but it doesn't have the broad user community)
Derby (an Apache project; derived from an IBM donation)
Hypersonic (HSQLDB)

Free (as in beer) with significant restrictions:
Oracle
(either any version, single user development only license, or the new free Express Edition (in public beta for Linux and Windows), it's multi-user, missing a number of uber-advanced features and has a 4GB size limit on the DB and other restrictions, but is also free for commercial or other use, with no support)
http://www.oracle.com/technology/products/database/xe/index.html

They all have their own quirks: Oracle's got the biggest learning curve, Derby and
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic