开发者

How to parse the datatype of a value entered by the user?

开发者 https://www.devze.com 2023-03-04 04:31 出处:网络
I was wondering if there is a way to take an input from the user and find out its datatype. For example I want something as follows:

I was wondering if there is a way to take an input from the user and find out its datatype.

For example I want something as follows:

user enters: 5

program parses 5 as an int

program assigns variable as: int x=5

Another scenario:

user enters: Jane

program parses Jane as String

program assigns variable as: String x=Jane

and so开发者_C百科 forth

The overall picture is that the user is going to pass in a table name and some criteria, get some rows, make some changes, and submit those chanes to Oracle. Since the DAO I'm using is supposed to work for any number of tables, I need to do everything "on the fly." Hope this makes sense. I don't want to go too much into detail because it can take awhile to explain all the details.


You could just let the JDBC driver figure it based on the datamodel. Treat them as Object and use PreparedStatement#setObject() to set them in the SQL query. This way you don't need to convert it and/or use specific setters.

On the other hand, it look much like as if you're developing (reinventing) a DB management tool. If this is true, then you could also just get the Java data type per column by ResultSet#getMetaData() and then for example ResultSetMetaData#getColumnClassName().

0

精彩评论

暂无评论...
验证码 换一张
取 消