Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC - How to set char in a prepared statement

I cannot find any method like

char c = 'c';

preparedStatement.setChar(1, c);

How to set character to a prepared statement?

like image 358
Yatendra Avatar asked Apr 16 '11 12:04

Yatendra


People also ask

Which character is used for input parameter in PreparedStatement?

symbol, which is known as the parameter marker. You must supply values for every parameter before executing the SQL statement. The setXXX() methods bind values to the parameters, where XXX represents the Java data type of the value you wish to bind to the input parameter.

Can you write statements for JDBC connectivity?

Create a Statement: From the connection interface, you can create the object for this interface. It is generally used for general–purpose access to databases and is useful while using static SQL statements at runtime. Syntax: Statement statement = connection.

What is PreparedStatement JDBC?

The PreparedStatement interface extends the Statement interface it represents a precompiled SQL statement which can be executed multiple times. This accepts parameterized SQL quires and you can pass 0 or more parameters to this query.


1 Answers

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.

like image 99
Edwin Dalorzo Avatar answered Sep 19 '22 10:09

Edwin Dalorzo