By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.