Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting into varchar field using PreparedStatement

 
Amrit Tiwana
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm inserting records in the SQL Server 2000 database using PreparedStatement. Below is the code fragment and the result.

empimages table structure is: emp(varchar(200)), img(photo), details(varchar(200)).

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc dbc:northwind";
Connection con = DriverManager.getConnection(url, "", "");
PreparedStatement ps = con.prepareCall("insert into empimages (emp, details) values (?,?)");
ps.setString(1,"a");
ps.setString(2,"s");
ps.execute();
con.commit();

ResultSet rs = st.executeQuery("Select * from empimages");
while (rs.next()) {
System.out.println("emp>" + rs.getString(1)+ "<");
System.out.println("details>" + rs.getString(3)+ "<");
}


Result:

emp>a <

details>updated <


Using PreparedStatement for insertion, unnecessary putting blank spaces. If anbody had encountered the same issue, please let me know the workaround, as I think it is driver implmentation issue.


Thanks in advance,
Amrit
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amrit,
Are those fixed length database fields?

Anyway, you can work around the issue by calling rs.getString(1).trim()
 
Amrit Tiwana
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,
I found the solution.
Actually it is setting when creating DSN. to avoid adding unnessary spaces to be added to varchar field, uncheck (Use ANSI nulls, paddings, warnings) setting.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amrit Tiwana:
Hi Jeanne,
I found the solution.
Actually it is setting when creating DSN. to avoid adding unnessary spaces to be added to varchar field, uncheck (Use ANSI nulls, paddings, warnings) setting.


Amrit,
Your soludtion was useful to me today.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic