Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle 11g connectivity with java in Netbeans 7.1

I have successfully connected my Oracle 11g XEdatabase with java in Netbeans 7.1.

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!");

Now I want to access a table employee(fname,lname,ssn), retrieving all records and displaying them.

When I add this code:

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"));
}

I get the following error:

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

This might be due to the fact the we can't access multiple rows in oracle.

How can I access the employee table in Java?

like image 479
Naveen Avatar asked Nov 20 '25 09:11

Naveen


1 Answers

Remove the semicolon from the query.

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

By the way, all those casts (Statement) and (ResultSet) are unnecessary.

like image 99
BalusC Avatar answered Nov 21 '25 22:11

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!