• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tim Jones
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following, returns "unable to cast String to CHAR " on final line.

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

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Jones
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the contents of this method.
 
Tim Jones
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why use the Oracle specific (I'm assuming) CHAR type, and not the general String type:
 
Tim Jones
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there's a lot of code involving Oracle-specific things, perhaps it might already contain an example of this cast?
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic