473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Please help ,I dont know what is the problem in my code?

Hi ,

I have a problem relate to java and database. Could anyone answer me
?Please see the following code.
import java.sql.*;
public class Result {

public static void main(String args[]) {
Connection con = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newI nstance();
System.out.println("JDBC driver loaded");

con = DriverManager.getConnection
("jdbc:odbc:cnEVA");
System.out.println("Database connection established");

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT * FROM t_Asset");
while (rs.next()) {
String title = rs.getString("f_Asset_number");
String price = rs.getString("f_Amount");

String s1 = rs.getString("f_Value_date");
String s2 = rs.getString("f_Original_dept");
String s3 = rs.getString("f_Original_loc");
System.out.println( title + " " + price+" "+s2+" "+ s1);
}
} catch (ClassNotFoundException cnfe) {
System.out.println("ClassNotFoundException: Could not locate
driver");
} catch (SQLException cnfe) {
System.out.println("SQLException: Could not connect to
database");
} catch (Exception e) {
System.out.println
("An unknown error occurred while connecting to
database");
} finally {
try {
if (con != null) {
con.close();
}
} catch(SQLException sqle) {
System.out.println("Unable to close database connection.");
}
}
}
}
There is complete ok to show the result i wanted when i run it.
But when i want to separate the object functions like the following
two class files:

import java.sql.*;
import java.util.*;

public class Books {

public String error;
public Connection con=null;

public Books() {

}

public void connect() throws ClassNotFoundException,
SQLException,
Exception {

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newI nstance();
con = DriverManager.getConnection(
"jdbc:odbc:cnEVA ");
} catch (ClassNotFoundException cnfe) {
error = "ClassNotFoundException: Could not locate DB driver.";
throw new ClassNotFoundException(error);
} catch (SQLException cnfe) {
error = "SQLException: Could not connect to database.";
throw new SQLException(error);
} catch (Exception e) {
error = "Exception: An unknown error occurred while connecting "
+
"to database.";
throw new Exception(error);
}
}
public ResultSet viewBooks() throws SQLException, Exception {
ResultSet rs = null;

try {
String queryString = ("SELECT * FROM t_Asset");
Statement stmt = con.createStatement();
rs = stmt.executeQuery(queryString);
} catch (SQLException sqle) {
error = "SQLException: Could not execute the query.";
throw new SQLException(error);
} catch (Exception e) {
error = "An exception occured while retrieving books.";
throw new Exception(error);
}
return rs;
}
}
//Then i use the following file to show the result :
import java.sql.*;
import java.util.*;

public class db{

public static void main(String[] argc) throws
ClassNotFoundException,
SQLException,
Exception{
Books book=new Books();

book.connect();
ResultSet rs =book.viewBooks();

while (rs.next()) {
String title = rs.getString("f_Asset_number");
String price = rs.getString("f_Amount");
System.out.print(title+price);
}
}
}
when i type javac to compile the above two programs ,they are ok. then
i type
java db. the following error.
Exception in thread "main" java.sql.SQLException: SQLException: Could
not connect to database.
at Books.connect(Books.java:28)
at db.main(db.java:12)

What is the problem ?

Thank you

Ricky.
Jul 17 '05 #1
1 2575
On 16 Jan 2004 04:20:06 -0800, <ka*******@hotmail.com> wrote:
when i type javac to compile the above two programs ,they are ok. then
i type
java db. the following error.
Exception in thread "main" java.sql.SQLException: SQLException: Could
not connect to database.
at Books.connect(Books.java:28)
at db.main(db.java:12)

What is the problem ? Thank you

Ricky.

In your main method you forgot to create a Statement like you did in the
first example. I think your error message is misleading. I haven't tested
your code but I think the connection is successful and the problem is when
you try to get the Resultset without a Statement. I would recommend
putting the connection code by itself in its own try block so that you
will know for sure whether the problem is the connection or some other
part.
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: amine | last post by:
I really need help on this please. I am writing an application for an Ipaq and I am trying to enable the application to connect to a DAV server which can is basically an HTTP server that acts like...
9
by: Martin | last post by:
Due to the the large numbers of MS trained programmers unemployed we are looking at using MS Development environments. VB programmers are currently 10 a penny. However I find C a bit more...
1
by: David Goodyear | last post by:
At the moment im experimenting with ideas in C++ and would really like to solve the following, please please help. Sorry i dont even know what the subject is this would come under? :( Sorry if...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
3
by: suzy | last post by:
Hello, I have created a template for my website using the masterpages template technique. It's working fine, except when I try to print the value of a querystring parameter in my HTML code, I...
11
by: K E Senthil Kumar | last post by:
Hi, I have created an application which uses a SQL server database. I created a mainform which retreives all the customers. This takes about 3sec. I have created a splash screen too. I use a...
5
by: tony | last post by:
I'm using PHP 5 on Win-98 command line (ie no web server involved) I'm processing a large csv file and when I loop through it I can process around 275 records per second. However at around...
5
by: settyv | last post by:
Hi, Below is the Javascript function that am trying to call from asp:Button control. <script language="javascript"> function ValidateDate(fromDate,toDate) { var fromDate=new Date();
5
nabh4u
by: nabh4u | last post by:
hi, i have a program where every thing is working properly. i have a vector with some values. i use iterators and delete a specific value in the vector. here the loop runs infinitely only for some...
11
by: shror | last post by:
Hi every body, Please I need your help solving my php mail() function problem that the code is appearing in the view source and I dont know whats the problem where I am using another page tto test...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.