my dog learned polymorphism*
The moose likes JDBC and the fly likes Problem With Inserting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem With Inserting" Watch "Problem With Inserting" New topic
Author

Problem With Inserting

Brandon Tom
Ranch Hand

Joined: Jan 11, 2006
Posts: 48
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!


Brandon Tom, SCJP, MCP<br />Programmer/Graphic Designer<br /><a href="http://mailto:tombrand@hawaii.edu" target="_blank" rel="nofollow">tombrand@hawaii.edu</a>
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
It's never too early to learn how to use prepared statements:

Tutorial : Using Prepared Statements


There is no emoticon for what I am feeling!
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333

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

Joined: Jan 11, 2006
Posts: 48
Cool. The book I have is just getting to prepared statements.
Brandon Tom
Ranch Hand

Joined: Jan 11, 2006
Posts: 48
Oddly enough, I'm getting an error that's saying that the JDBC-ODBC bridge does not support prepared statements =(
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41108
    
  45
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).


Ping & DNS - my free Android networking tools app
Brandon Tom
Ranch Hand

Joined: Jan 11, 2006
Posts: 48
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

Joined: Dec 15, 2005
Posts: 333
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem With Inserting
 
Similar Threads
Insert Problem ! Please Help !
Connecting To Database
Java SQL Exception General Error
Help with my homework please
jdbc error