如何解析用户输入的值的数据类型?
我想知道是否有一种方法可以从用户那里获取输入并找出其数据类型。
例如,我想要如下内容:
用户输入: 5
程序将 5 解析为 int
程序将变量分配为: int x=5
另一种情况:
用户输入: Jane
程序将 Jane 解析为 String
程序将变量分配为: String x=Jane
等等 总体情况
是,用户将传递表名和一些条件,获取一些行,进行一些更改,然后将这些信息提交给 Oracle。由于我使用的 DAO 应该适用于任意数量的表,因此我需要“即时”完成所有操作。希望这是有道理的。我不想讲太多细节,因为解释所有细节可能需要一些时间。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以让 JDBC 驱动程序根据数据模型来计算它。将它们视为
Object
并使用PreparedStatement#setObject()
在 SQL 查询中设置它们。这样您就不需要转换它和/或使用特定的设置器。另一方面,它看起来很像您正在开发(重新发明)数据库管理工具。如果这是真的,那么您还可以通过
ResultSet#getMetaData()
然后例如ResultSetMetaData#getColumnClassName()
。You could just let the JDBC driver figure it based on the datamodel. Treat them as
Object
and usePreparedStatement#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 exampleResultSetMetaData#getColumnClassName()
.