Solved

java program cannot retrieve records from oracle DB

Posted on 2006-10-20
14
217 Views
Last Modified: 2011-08-18
i made this prg. in java .
it inserts records into the databse but doesnt retrieve them...
i checked using SQL-PLUS
the records were created ...
but the records don't get displayed
what could be wrong?


___________________________________________________
import java.sql.*;

class A1
{
public static void main(String args[])
{
try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


Connection c=DriverManager.getConnection("jdbc:odbc:pranjal","scott","tiger");

System.out.println("got connection");
Statement stmt=c.createStatement();
String sq="insert into empl values('d2','73141')";
stmt.executeUpdate(sq);
sq="select fname,ssn from employee";

ResultSet rs=stmt.executeQuery(sq);
System.out.println("test2");
while(rs.next())
{
System.out.println("hi");
System.out.println(rs.getString("fname") +" " +rs.getString("ssn"));
}
rs.close();
stmt.close();
c.close();
}
catch(ClassNotFoundException e1)
{}
catch(SQLException e2)
{}
catch(Exception e3)
{}
}
}
____________________________________________________
0
Comment
Question by:shilpi84
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
14 Comments
 
LVL 9

Expert Comment

by:owenli27
ID: 17776086
try this:

change this line:   ResultSet rs=stmt.executeQuery( sq );

to:
stmt = c.prepareStatement(sq);
ResultSet rs = stmt.executeQuery();
0
 

Author Comment

by:shilpi84
ID: 17776125
ok... this is my latest code... and it works!

import java.sql.*;

class A1
{
public static void main(String args[])
{
try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


Connection c=DriverManager.getConnection("jdbc:odbc:shilpi","scott","tiger");

System.out.println("got connection");

Statement stmt=c.createStatement();

String sq="select fname,ssn from empl";

//stmt.executeUpdate(sq);


ResultSet rs=stmt.executeQuery(sq);
System.out.println("hi");

while(rs.next())
{
System.out.println(rs.getString("fname") +" " +rs.getString("ssn"));
}
rs.close();
stmt.close();
c.close();
}
catch(ClassNotFoundException e1)
{}
catch(SQLException e2)
{}
catch(Exception e3)
{}
}
}
0
 
LVL 14

Accepted Solution

by:
hoomanv earned 500 total points
ID: 17776136
Print the exceptions to see if any error has occurred

e.printStackTrace()
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:shilpi84
ID: 17776178
hoomanv it's working now...
but i have a similar problem in JSP ..
the same thing is not working in JSP
...
if you could please check it out.
here's the link:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_22031824.html
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 500 total points
ID: 17776190
OK, but first printStackTrace the exceptions in your first code to find out the problem
0
 

Author Comment

by:shilpi84
ID: 17776234
ok ... i put e.printStackTrace() in every catch. this is what i got
_____________________________________________________
C:\>javac A1.java

C:\>java A1
got connection
java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-00942: tab
le or view does not exist
        at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
        at A1.main(A1.java:21)

C:\>







0
 

Author Comment

by:shilpi84
ID: 17776243
please omit my last post
i'm reposting in stackTrace
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 500 total points
ID: 17776248
> table or view does not exist

Thats why you changed
jdbc:odbc:pranjal
to
jdbc:odbc:shilpi
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 500 total points
ID: 17776280
And
select fname,ssn from employee
To
select fname,ssn from empl
0
 

Author Comment

by:shilpi84
ID: 17776287
no hoomanv ... i by mistake input some chars right after the empl table name...
i made this mistake just this time...

String sq="insert into empl values('d2','73141')";
stmt.executeUpdate(sq);
sq="select fname,ssn from employee";
...
 i was using different table names

i recompiled the original program and its now running
0
 

Author Comment

by:shilpi84
ID: 17776297
yes i did all that and its working ... thanks ...all of you
0
 

Author Comment

by:shilpi84
ID: 17776304
actually i'd ported programs from one PC to another.
0
 

Author Comment

by:shilpi84
ID: 17776313
i still dont get ehy the same thing is not working in JSP:
similar JSP question:
         http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_22031824.html
0
 

Author Comment

by:shilpi84
ID: 17776323
i'm closing this question now.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question