JDBC - How to set char in a prepared statement

☆樱花仙子☆ 提交于 2019-12-04 08:18:31

问题


I cannot find any method like

char c = 'c';

preparedStatement.setChar(1, c);

How to set character to a prepared statement?


回答1:


The JDBC Specification 4.0 in Appendix B (Data Type Conversion Tables) states the following conversions:

This table also shows the conversions used by the SQLInput reader methods, except that they use only the recommended conversions.

JDBC Type              Java Type
-------------------------------------------
CHAR                   String
VARCHAR                String
LONGVARCHAR            String
NUMERIC                java.math.BigDecimal
DECIMAL                java.math.BigDecimal
BIT                    boolean
BOOLEAN                boolean
TINYINT                byte
SMALLINT               short

TABLE B- 1  JDBC Types Mapped to Java Types

Therefore PreparedStatement.setString(1, String.valueOf(myChar)) should do the trick.




回答2:


Use setString() to set the variable.

To get it back use getString() and assuming it is not null do something like this to get the character:

getString("your_column").charAt(0);



回答3:


PreparedStatement.setString(1,c+" ");

PreparedStatement.setString(1,String.valueOf(c));


来源:https://stackoverflow.com/questions/5686514/jdbc-how-to-set-char-in-a-prepared-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!