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.