有可能访问带有问号的sql表吗? [英] Is that possible that access to a sql table with question mark?

查看:80
本文介绍了有可能访问带有问号的sql表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个sql表客户,员工和经理.我想动态访问我的表.我有一个这样的声明,

I have 3 sql tables customer, employee and manager. I want to access dynamically to my tables. I had a statement like this,

"update customer set AMOUNT where ID= ?"

但是,在这种情况下,我只能访问客户.我需要访问所有表以进行不同的操作.可以这样写吗,

But int this situation i can only access to customer. I need to access all of the tables for different operations. Is that possible that write this,

"update ? set AMOUNT where ID=?"

或者我该怎么做,例如访问其他班级的员工.

or what can i do to access for example employee for a different class.

推荐答案

参数只能在否则可以使用文字值(例如带引号的字符串或数字值)的地方使用.

The parameters can be used only in the place where you could otherwise use a literal value, like a quoted string or a numeric value.

参数不能用于表名之类的标识符.也不表达.也没有SQL关键字.

Parameters cannot be used for identifiers like table names. Nor expressions. Nor SQL keywords.

在准备查询之前,必须将查询的所有其他这些部分固定在SQL查询字符串中.

All those other parts of the query must be fixed in the SQL query string before you prepare the query.

要查询其他表,只需将表名称连接到字符串中即可.

To query other tables, you just have concatenate the table name into the string.

String query = "update " + tableName + " set amount where ID=?";

由您确定变量tableName实际上仅包含表名之一.做到这一点的一种好方法是将其与已知表名的列表进行比较,如果不在列表中,则抛出异常.

It's up to you to make sure your variable tableName in fact only contains one of your table names. A good way to do this is to compare it to a list of known table names, and if it isn't in the list, throw an exception.

这篇关于有可能访问带有问号的sql表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆