用Eclipse,查询Oracle当前用户,出现Oracle语法错误
/**打开到数据库的连接*/publicvoidopenConnect(){try{/**连接数据库*/conn=DriverManager.getConnection(...
/**打开到数据库的连接*/
public void openConnect() {
try {
/**连接数据库*/
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
/**进行打印,测试连接对象是否存在*/
System.out.println("连接对象:" + conn);
}
/**创建数据库操作对象*/
public void createStatment() {
try {
/**Statement接口需要通过Connection接口进行实例化操作*/
stmt = conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**进行打印,测试连接对象是否存在*/
System.out.println("操作对象:" +stmt);
}
/**执行SQL语句*/
public void executeSQL() {
try {
//sql_query = "SELECT username FROM dba_users";
sql_query = "SHOW USER";
/**stmt作为数据库操作对象,执行sql语句*/
rs = stmt.executeQuery(sql_query);
System.out.println("SQL Execute");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**输出查询结果*/
public void getQueryResult() {
try {
/**判断结果是否为空*/
if(rs == null) {
System.out.println("Resultset is null. from getQueryResult");
} else {
System.out.println("Resultset is not null. from getQueryResult");
/**输出结果*/
while(rs.next()) {
System.out.println(rs.getString(1));
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("getQueryResult Error!");
}
}
public void test() {
this.loadOracleDriver();
this.openConnect();
this.createStatment();
this.executeSQL();
this.getQueryResult();
this.closeConnect();
}
执行test()方法后,
rs为空值
rs = stmt.executeQuery(sql_query); 出现SQLSyntaxErrorException语法错误
但是执行sql_query = "SELECT username FROM dba_users";时,却是正确的。
请问该怎么修改,才能显示出show user的结果? 展开
public void openConnect() {
try {
/**连接数据库*/
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
/**进行打印,测试连接对象是否存在*/
System.out.println("连接对象:" + conn);
}
/**创建数据库操作对象*/
public void createStatment() {
try {
/**Statement接口需要通过Connection接口进行实例化操作*/
stmt = conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**进行打印,测试连接对象是否存在*/
System.out.println("操作对象:" +stmt);
}
/**执行SQL语句*/
public void executeSQL() {
try {
//sql_query = "SELECT username FROM dba_users";
sql_query = "SHOW USER";
/**stmt作为数据库操作对象,执行sql语句*/
rs = stmt.executeQuery(sql_query);
System.out.println("SQL Execute");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**输出查询结果*/
public void getQueryResult() {
try {
/**判断结果是否为空*/
if(rs == null) {
System.out.println("Resultset is null. from getQueryResult");
} else {
System.out.println("Resultset is not null. from getQueryResult");
/**输出结果*/
while(rs.next()) {
System.out.println(rs.getString(1));
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("getQueryResult Error!");
}
}
public void test() {
this.loadOracleDriver();
this.openConnect();
this.createStatment();
this.executeSQL();
this.getQueryResult();
this.closeConnect();
}
执行test()方法后,
rs为空值
rs = stmt.executeQuery(sql_query); 出现SQLSyntaxErrorException语法错误
但是执行sql_query = "SELECT username FROM dba_users";时,却是正确的。
请问该怎么修改,才能显示出show user的结果? 展开
1个回答
展开全部
因为Show user 命令是oracle sql plus的特有命令,不是标准sql,所以不能在代码中使用。
追问
那在java中,该怎么查询Oracle的当前用户呢?
追答
连接oracle数据库字符串中有连接所用的用户名和密码
另外可试试以下命令
1、查看当前用户拥有的角色权限信息:select * from role_sys_privs;
2、查看所有用户:select * from all_users;
3、查看当前用户的详细信息:select * from user_users;
4、查看所有角色:select * from dba_roles;
5、查看当前用户的角色信息:select * from user_role_privs;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询