Results 1 to 11 of 11
Thread: problem with jdbc (sql 2000)
- 07-28-2010, 11:06 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
problem with jdbc (sql 2000)
Hey everybody,
my problem is when I insert the date it gives me this error!
and the error I get is :Java Code:DBconn.s.execute("INSERT INTO tblDoctor VALUES ('"+LicenseNum+"','"+FirstName+"'" + ",'"+surName+"','"+Specialization+"','"+StartWorkingDate+"' " );
Java Code:[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Incorrect syntax near ''.
- 07-29-2010, 02:46 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Specify the column values:
INSERT INTO tblDoctor (col1,...coln) VALUES (...)
- 07-29-2010, 07:20 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Syntax error, not a driver error at all.
Most of the people mess-up this. Actually you can do this in two ways. Followings are the two forms.
Here you don't need to specify column names, only the values you want to insert. BUT in that case you've to provide all column values except auto indexing. Basically you must specify equal number of values to the number of columns.INSERT INTO tblname
VALUES (val1, val2, val3, ...)
[QUOTE]INSERT INTO tblname (colu1, colu2, colu3, ...)
VALUES (val1, val2, val3, ...)/QUOTE]
This is more generic, you can insert values only you know at the moment. SQL engine insert default values to all other columns.
- 07-29-2010, 07:57 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Seemingly, one of those variable is an "empty" string, thereby leading to one of the "values" in the query being
. Use a PreparedStatement (do not ever cobble together your statements like this unless you want people to be performing SQL Injection attacks against your DB) and when one of the arguments is an empty string use setNull rather than setString (or whatever is appropriate for the field).Java Code:''
- 07-29-2010, 09:09 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
ok I did this:
but it keeps showing this error:Java Code:public void addDoctor(){ String msg=String.format("INSERT INTO tblDoctor" + "(LicenseNum,FirstName,surName,Specialization,StartWorkingDate)" +" VALUES (%s,'%s','%s','%s','%s')",LicenseNum,FirstName,surName,Specialization,StartWorkingDate); try{ DBconn.s.execute(msg); } catch(Exception e){ System.out.print(e.getMessage()); } }
relevant classes:Java Code:[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Cannot insert the value NULL into column 'LicenseNum', table 'DB2010-Dentist.dbo.tblDoctor'; column does not allow nulls. INSERT fails.
Java Code:public static boolean insertDoctor (String LicenseNum, String FirstName, String surName,String Specialization, String StartWorkingDate) { boolean success=false; tblDoctor details= new tblDoctor(LicenseNum, FirstName, surName, Specialization, StartWorkingDate); if (details.getDoctor(false)==false) { details.addDoctor(); success=true; } return success; }p.s. I don't enter null values!!!Java Code:private void add_dr_btActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String p1=License_add_dr_txt.getText(); String p2=pName_add_dr_txt.getText(); String p3=lName_add_dr_txt.getText(); String p4=speci_add_dr_txt.getText(); String p5=date_add_dr_txt.getText(); if (checkFieldsCorrectness(true)) if (Doctor.insertDoctor(p1, p2, p3, p4, p5)==false) JOptionPane.showMessageDialog(this, "Record with the same LicenseNum (" + p1 + ") already exists."); else JOptionPane.showMessageDialog(this, " LicenseNum (" + p1 + ") was added succesfully."); }
- 07-29-2010, 09:12 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
And, since you'll now be using a PreparedStatement, you may as well use a java.sql.Date rather than a String to represent your date...
- 07-29-2010, 09:13 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
You've already been advised, use a PreparedStatement...
- 07-29-2010, 09:59 AM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Well, if the exception you posted was the complete exception, then that
portion of the exception indicates that one of the "values" was an empty String, thereby producingJava Code:''
instead ofJava Code:''
and in an SQL statementJava Code:'someValue'
is usually the same as null, as I already said.Java Code:''
Now, like I said, use a PreparedStatement.
P.S. using format is, in this regard, exactly the same as concatenating the String.
- 07-29-2010, 10:27 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
A nice example.
kind regards,
Jos
- 07-29-2010, 10:49 AM #10
Member
- Join Date
- Apr 2009
- Posts
- 19
- Rep Power
- 0
what "PreparedStatement" means??
- 07-29-2010, 11:01 AM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
48 bits JPEG 2000
By user_java in forum Java 2DReplies: 0Last Post: 03-02-2010, 05:19 AM -
Problem while inserting UTF-8 characters in MS-SQL Server 2000 Database
By sagarsway in forum JDBCReplies: 1Last Post: 12-30-2008, 04:39 PM -
JSP Connecting to MS SQL SERVER 2000 using JDBC
By hisouka in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 09-02-2008, 09:23 AM -
Help, conect a JSP, SQL SERVER 2000
By Marcus in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-06-2007, 03:04 PM -
SQLServer 2000 EXCEPTION
By Heather in forum JDBCReplies: 3Last Post: 06-29-2007, 03:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks