Solved

java program cannot retrieve records from oracle DB

Posted on 2006-10-20
14
216 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 90
micro services spring boot application error 3 194
javap not working 8 46
Glassfish admin console not working 1 13
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

809 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