[英]How to convert string type to Binary type in insert query in Java servlet
Here i am inserting data into sql server table in one column data type is binary type data it stores i am taking 在这里我将数据插入SQL Server表中的一列数据类型是我正在存储的二进制类型数据
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);
You need to convert your password String
object to byte[]
and use preparedStatement.setBytes
method as shown below: 您需要将密码
String
对象转换为byte[]
并使用preparedStatement.setBytes
方法,如下所示:
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
我现在收到此错误:: com.microsoft.sqlserver.jdbc.SQLServerException:将nvarchar值“ SuperUser”转换为数据类型int时转换失败
The problem with your code is that you are using preparedStatement.setString
for all datatypes. 与您的代码的问题是, 你使用
preparedStatement.setString
所有数据类型。 You need to use preparedStatement.setInt
or other suitable methods (look here for API) according to the database table's column design. 根据数据库表的列设计,您需要使用
preparedStatement.setInt
或其他合适的方法 ( 在此处查看API) 。
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. 另外, 我强烈建议在
finally
块中关闭preparestatement
对象,或者对资源使用try ,否则将耗尽资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.