Solved

java program cannot retrieve records from oracle DB

Posted on 2006-10-20
14
215 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 74
topping1 challenge 7 71
replace a word with other 1 40
arguments to jar 5 7
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

943 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

5 Experts available now in Live!

Get 1:1 Help Now