Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Binary datatype in MS SQL via JDBC

 
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone help me how can i insert binary data into MS SQL through JDBC.

For example,
I created a table like

I couldnt find any method like setBinary() in PreparedStatement class..

Also here sample_id is the primary key. So I will have to create an auto-increment value for this. Will it be fine if I create it through DB or manually through Java.

Thanks,
Muthu

 
Marshal
Posts: 69395
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to create auto increment columns in the database, and don't attempt to set them from a client application. Don't even try to update the primary key.
By the way: Why are you using binary? In MySQL you usually use serial which is shorthand for "longint unique auto_increment not null" or similar.
 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay..
But I am using Microsoft SQL Server. And in case I am using binary, I dont have an option for auto incrementing it (i.e. through IDENTITY function in Microsoft SQL Server) available for binary data. They are available only for integers.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Treat it like you would any binary data type, use setBytes() to set its content.

Using a binary value for a primary key is not a good idea, why have you chosen this data type? You can't auto increment it using SQL Server functionality, you'll have to do this yourself.
 
Campbell Ritchie
Marshal
Posts: 69395
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sorry, I misread the post, thinking MSSQL was MySQL. Sorry.
 
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic