NetBeans 7.1中与Java的Oracle 11g连接

提问

我已成功将NetBeans 7.1中的Java与Oracle 11g XEdatabase连接.

Class.forName("oracle.jdbc.OracleDriver");
System.out.println("DRIVER LOADED!");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "acpsa") ;
System.out.println("CONNECTION ESTABLISHED!");

现在,我想访问一个表employee(fname,lname,ssn),检索所有记录并显示它们.

当我添加以下代码时:

Statement stmt;
stmt=(Statement)conn.createStatement();
String qq = "select fname,ssn from employee where lname='tank';";
ResultSet rs = (ResultSet)stmt.executeQuery(qq);
while(rs.next()){
    System.out.println(rs.getString("fname") + "\t" + rs.getString("ssn"));
}

我收到以下错误:

Error :java.sql.SQLSyntaxErrorException: ORA-00911: invalid character

这可能是由于我们无法在oracle中访问多个行.

如何访问Java中的employee表?

最佳答案

从查询中删除分号.

String qq = "select fname,ssn from employee where lname='tank'";

顺便说一下,所有这些强制转换(语句)和(结果集)都是不必要的.

评论