SQL 查询将作为字符串传递给我的方法,如果查询是例如,我如何检索查询中的列名
Select name, dob, age, gender from table1; or
Select uname AS name, hgt AS height, wgt AS weight from table1;
我想获取数组或列表中的列名。
SQL 查询将作为字符串传递给我的方法,如果查询是例如,我如何检索查询中的列名
Select name, dob, age, gender from table1; or
Select uname AS name, hgt AS height, wgt AS weight from table1;
我想获取数组或列表中的列名。
虽然有点令人费解,但这对问题中列出的两个查询都有效:
String query = "Select uname AS name, hgt AS height, wgt AS weight from table1";
Pattern p = Pattern.compile("\\s*\\w+,");Pattern p1 = Pattern.compile("\\s+\\w+\\s+from");
Matcher m = p.matcher(query);Matcher m1=p1.matcher(query);
String colsOnly="";
while(m.find()){colsOnly+=(m.group().trim());}
while(m1.find()){colsOnly+=(m1.group().substring(0,m1.group().length()-4).trim());}
String[] cols = colsOnly.split(",");
你有没有尝试过
ResultSetMetaData rsmd = rs.getMetaData();
String name = rsmd.getColumnName(1);
String str = "select dept,empid as eid , ename as name,deptname,deptid as department_id from emp";
String[] arr = str.split("as");
StringTokenizer str1 = new StringTokenizer(str, " ,");
ArrayList<String> arrStr = new ArrayList<>();
while (str1.hasMoreTokens())
{
String strT = str1.nextToken().toString();
arrStr.add(strT);
}
for (int i = 0; i < arrStr.size(); i++)
{
if (arrStr.get(i).equals("as"))
{
System.out.println("Orignal\t" + arrStr.get(i - 1));
System.out.println("Alias\t" + arrStr.get(i + 1));
}
}
输出
原始帝国
别名 eid
原名
别名
原始部门
别名部门_id