aspose file tools*
The moose likes JDBC and the fly likes How to cast a String to java.sql.types.CHAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to cast a String to java.sql.types.CHAR " Watch "How to cast a String to java.sql.types.CHAR " New topic
Author

How to cast a String to java.sql.types.CHAR

Tim Jones
Greenhorn

Joined: Apr 06, 2008
Posts: 28
The following, returns "unable to cast String to CHAR " on final line.

Object foo = "Name";
table.setName((CHAR) foo);

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Types.CHAR isn't a type, it's a constant that represents the CHAR type. To give such a column values you can simply use String itself.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tim Jones
Greenhorn

Joined: Apr 06, 2008
Posts: 28
Rob Spoor wrote:Types.CHAR isn't a type, it's a constant that represents the CHAR type. To give such a column values you can simply use String itself.


If you mean passing the string e.g. (table.setName"foo") this retruns a casting error. Do you have an exmaple? Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

This setName method, is this part of your own code, or of some third party library? Because there is no SQL related setName method in the JSE API.
Tim Jones
Greenhorn

Joined: Apr 06, 2008
Posts: 28
Rob Spoor wrote:This setName method, is this part of your own code, or of some third party library? Because there is no SQL related setName method in the JSE API.


Its a method I created and is part of the program. I am trying to pass a CHAR value to to test this method. Can't think of another way without createdinga String then casting it?

In normal circumtances there will not be a need to cast a String to a CHAR becuse the value will be in a "OracleResultSet" and then cast to CHAR.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Please post the contents of this method.
Tim Jones
Greenhorn

Joined: Apr 06, 2008
Posts: 28
Rob Spoor wrote:Please post the contents of this method.


private static CHAR schemaName;
public void setSchemName(CHAR schemaName) {
JDBC Stuff. here...
CHAR schematName = (CHAR)((OracleResultSet)schemaNameResultSet).getOracleObject(1);
}

Method Name should be setName as above but should make not difference as it it the same type CHAR .
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Why use the Oracle specific (I'm assuming) CHAR type, and not the general String type:
Tim Jones
Greenhorn

Joined: Apr 06, 2008
Posts: 28
Rob Spoor wrote:Why use the Oracle specific (I'm assuming) CHAR type, and not the general String type:


Its a choice that was made at the begining, too late to go back an change all that code.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

If there's a lot of code involving Oracle-specific things, perhaps it might already contain an example of this cast?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Then you need to find out which constructors CHAR has, and create such a CHAR.

You're in luck, I got bored so I decided to check it out for you. It appears there are 4 constructors:
- CHAR(byte[], CharacterSet)
- CHAR(byte[], int, int, CharacterSet)
- CHAR(String, CharacterSet)
- CHAR(Object, CharacterSet)

You need one of the latter two, which leads to another problem - how to get a CharacterSet. CharacterSet is abstract, but it has a static method make(int). As far as I can tell, this int needs to be one of the constants in CharacterSetFactory: DEFAULT_CHARSET, ASCII_CHARSET, ISO_LATIN_1_CHARSET, UNICODE_1_CHARSET or UNICODE_2_CHARSET.
So my attempt:
Still, I should note that using driver specific classes is bad design. If you need to change to some other RDMS like MySQL, MS SQL Server or PostgreSQL, you're essentially screwed because you will need to rewrite all of your code. You need to do that now anyway, but if the design should have used generic types (String) from the start.
Tim Jones
Greenhorn

Joined: Apr 06, 2008
Posts: 28
Rob Spoor wrote:Then you need to find out which constructors CHAR has, and create such a CHAR.

You're in luck, I got bored so I decided to check it out for you. It appears there are 4 constructors:
- CHAR(byte[], CharacterSet)
- CHAR(byte[], int, int, CharacterSet)
- CHAR(String, CharacterSet)
- CHAR(Object, CharacterSet)

You need one of the latter two, which leads to another problem - how to get a CharacterSet. CharacterSet is abstract, but it has a static method make(int). As far as I can tell, this int needs to be one of the constants in CharacterSetFactory: DEFAULT_CHARSET, ASCII_CHARSET, ISO_LATIN_1_CHARSET, UNICODE_1_CHARSET or UNICODE_2_CHARSET.
So my attempt:
Still, I should note that using driver specific classes is bad design. If you need to change to some other RDMS like MySQL, MS SQL Server or PostgreSQL, you're essentially screwed because you will need to rewrite all of your code. You need to do that now anyway, but if the design should have used generic types (String) from the start.


Thank You, I think I will refactor the code to use String afteraall.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to cast a String to java.sql.types.CHAR