我的DAO层有这么一个类
public class checkMethod { public static ResultSet select(String TableName){
PreparedStatement pre = null;
ResultSet res = null;
String sql = "select * from ? ";
Connection connection = Dao.getConnectionOracle();
try {
pre.setString(1, TableName);
pre = connection.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
res = pre.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
我想把表名当做参数传递,实现一个方法能有查询所有表的功能,问是否能实现?如果这种方法不能实现问是否有别的方法能实现,谢谢赐教。
public class checkMethod { public static ResultSet select(String TableName){
PreparedStatement pre = null;
ResultSet res = null;
String sql = "select * from ? ";
Connection connection = Dao.getConnectionOracle();
try {
pre.setString(1, TableName);
pre = connection.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
res = pre.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
我想把表名当做参数传递,实现一个方法能有查询所有表的功能,问是否能实现?如果这种方法不能实现问是否有别的方法能实现,谢谢赐教。
然后用 getString()方法输出
Connection con = getCon();
PreparedStatement ps = con.prepareStatement(sql);
if (p != null) {
for (int i = 0; i < p.length; i++) {
ps.setObject(i + 1, p[i]);
}
}
ResultSet rs = ps.executeQuery();
List objs = new ArrayList();
int cols = rs.getMetaData().getColumnCount();
while (rs.next()) { Object obj = c.newInstance();
for (int i = 0; i < cols; i++) {
Object colValue = rs.getObject(i + 1);
Object colName = rs.getMetaData().getColumnLabel(i + 1);
Method[] ms = c.getDeclaredMethods();
for (Method m : ms) {
if (m.getName().startsWith("set")
&& m.getName().toUpperCase().endsWith(
colName.toString().toUpperCase())) {
m.invoke(obj, new Object[] { colValue });
} } }
objs.add(obj);
}
rs.close();
ps.close();
con.close(); return objs; }
prepareStatement不支持传表名参数!
这个这能并字符串拉! String sql = "select * from " + tableName;
就算prepareStaement能支持传表名参数!
你的代码也是错!
pre.setString(1, TableName);空指针异常!
ResultSet res = null;
String sql = "select * from "+TableName;
Connection connection = Dao.getConnectionOracle();
try {
pre = connection.prepareStatement(sql); } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
res = pre.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
public void test(){
try {
List<Map<String, String>> list = DbHandle.executeQuery("select table_name from user_tables");
for(Map<String, String> map : list){
String table = map.get("TABLE_NAME");
List datas = DbHandle.executeQuery("select * from "+table);
}
System.out.println(list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}DbHandle是工具类,返回List<Map<String, String>>这样的数据结构,不知道这样是不是LZ需要的~
public class checkMethod { public static ResultSet select(String TableName){ Statement stmt = null;
ResultSet res = null;
String sql = "select * from "+TableName;
Connection connection = Dao.getConnectionOracle();
try {
stmt = connection.createStatement();
res = stmt.executeQuery(sql);} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
} 改成这样应该可以~~用那个PreparedStatement把表名当参数真的很别扭~~~
String sql = "select * from ? ";
Connection connection = Dao.getConnectionOracle();
try {
pre.setString(1, TableName);
pre = connection.prepareStatement(sql);至少你应该倒过来吧,肯定报null异常了。