34

I cannot find any method like

char c = 'c';

preparedStatement.setChar(1, c);

How to set character to a prepared statement?

Community
  • 1
  • 1
Amit
  • 33,847
  • 91
  • 226
  • 299

3 Answers3

64

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.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
5

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);
jzd
  • 23,473
  • 9
  • 54
  • 76
0
PreparedStatement.setString(1,c+" ");

PreparedStatement.setString(1,String.valueOf(c));
Lakhan
  • 1
  • 1
  • While this code may answer the question, providing additional context regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. – spongebob Aug 20 '15 at 14:05