Solved

java program cannot retrieve records from oracle DB

Posted on 2006-10-20
14
218 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

630 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