Solved

java program cannot retrieve records from oracle DB

Posted on 2006-10-20
14
214 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
  • 9
  • 4
14 Comments
 
LVL 9

Expert Comment

by:owenli27
Comment Utility
try this:

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

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

Author Comment

by:shilpi84
Comment Utility
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
Comment Utility
Print the exceptions to see if any error has occurred

e.printStackTrace()
0
 

Author Comment

by:shilpi84
Comment Utility
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
Comment Utility
OK, but first printStackTrace the exceptions in your first code to find out the problem
0
 

Author Comment

by:shilpi84
Comment Utility
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
Comment Utility
please omit my last post
i'm reposting in stackTrace
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 500 total points
Comment Utility
> 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
Comment Utility
And
select fname,ssn from employee
To
select fname,ssn from empl
0
 

Author Comment

by:shilpi84
Comment Utility
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
Comment Utility
yes i did all that and its working ... thanks ...all of you
0
 

Author Comment

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

Author Comment

by:shilpi84
Comment Utility
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
Comment Utility
i'm closing this question now.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now