Hello everyone,

Im back, with a new problem. LOL!

I need to change Java datatypes to SQL types.

I have sql queries as String object. like this one :

create table MyTable ( id bigint, name String , city String , state String ) 

OR

create table MyTable ( id bigint, name java.lang.String , city java.lang.String , state java.lang.String )

I need to change "String" or "java.lang.String" to varchar or char and same thing for other types.

One way is, Write a code to replace java datatypes to sqltypes.

For that, I need to map all the java datatypes to sqltypes.

But i want to know, is there any other way to do it ? any inbuild class for this, or any third party class ?

Waiting for your response.

Regards,

Recommended Answers

All 9 Replies

> For that, I need to map all the java datatypes to sqltypes.

This largely depends on the database engine in consideration and is normally taken care by the database driver.

If the database to be used is known in advance, a simple global search and replace should do the job. For additional flexibility, consider using a map to store the mappings which can be loaded from a properties file.

> For that, I need to map all the java datatypes to sqltypes.

This largely depends on the database engine in consideration and is normally taken care by the database driver.

If the database to be used is known in advance, a simple global search and replace should do the job. For additional flexibility, consider using a map to store the mappings which can be loaded from a properties file.

Thanks for reply!
But i didn't really get you.

Here are the few details:
Using Eclipse SDK 3.4.0
Driver : MySQL Java Connector 3.1.4 Beta Library.
Server : MySQL 5.0.3 Win32 Installer

Database details are already known in advance.

Can you please be more clear to me? :) Or if you can provide a sample code ?

Regards,

To be of more help, you will have to provide me a bit more details on the kind of application you are building. Where exactly is the query containing Java data types coming from? Why not normal queries?

The meaning of my previous post was pretty simple, let's assume you have the entire query in a single string then all that remains to be done is to use replaceAll() method. The regular expression required can be created by iterating over the map which contains the java data types to SQL data types mapping.

I would like to see an attempt from your side before providing a sample snippet. :-)

Hello,

I need to create tables based on Dataobjects classes.

EG : I have class named Student with 3 attributes
-----------------------
| Student
-----------------------
| int Id
| String fName
| String lName
-----------------------

Now, a table should be created automatically, when i call createTable(java.lang.Class classObj) method.

createMethod(java.lang.Class classObj) :
I will get Class object of "Student" class and all attributes from getDeclaredFields() method that return an array of java.lang.reflect.Field class objects.

from java.lang.reflect.Field object. I can retrieve the datatypes
getName() method returns : java.lang.String
getSimpleName() method returns : String

Im using these classes and methods to generate SQL query for creating table.

Now problem is to map Java datatypes to SQL types.

Checkout the Map returned by conn.getTypeMap() (conn being your Connection object, of course).

I guess it doesn't do what the OP is expecting:

Retrieves the Map object associated with this Connection object. Unless the application has added an entry, the type map returned will be empty.

Yes, I don't think there's any other way to do it.

Im going to create Mapping class for it.

Anyways, Thanks for help! :D

I guess it doesn't do what the OP is expecting:

Yep, oh well. ;-)

Nay, I had never actually looked at it, but I was assuming (and you know what they say about that word!) that it would return the default mapping (as it does exist). Oh, well, I guess common sense is too complicated (as common sense says it would return the default mappings). ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.