0

Here i am inserting data into sql server table in one column data type is binary type data it stores i am taking

When i trying to pass data it shows error message and how to insert this

String ins="insert into Users(ID,Name,Password)     values(?,?,?)";

ps.setString(1,id); 
ps.setString(2,Name); 
ps.setString(3,password);  
4
  • What is the error message? Commented Mar 27, 2017 at 11:54
  • @Olar Anddrei, I Edited my question with error Commented Mar 27, 2017 at 11:58
  • See @javaguy answer then. Commented Mar 27, 2017 at 11:59
  • @Jhon why are you removing the code/errors from question? Commented Mar 27, 2017 at 12:16

1 Answer 1

4

You need to convert your password String object to byte[] and use preparedStatement.setBytes method as shown below:

byte[] passwordBytes = password.getBytes();
ps.setBytes(9,passwordBytes);  

I am getting this error now:: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the nvarchar value 'SuperUser' to data type int

The problem with your code is that you are using preparedStatement.setString for all datatypes. You need to use preparedStatement.setInt or other suitable methods (look here for API) according to the database table's column design.

Also, I strongly recommend to close the preparestatement object in the finally block or use try with resources, otherwise you will run out of resources.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.