AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases
View previous topic :: View next topic  
Author Message
kaiwing18@hotmail.com
Guest





PostPosted: Fri Jan 16, 2004 12:18 pm    Post subject: Please help ,I dont know what is the problem in my code? Reply with quote



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").newInstance();
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").newInstance();
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:2Cool
at db.main(db.java:12)



What is the problem ?



Thank you

Ricky.
Back to top
Markku Salminen
Guest





PostPosted: Fri Jan 16, 2004 4:39 pm    Post subject: Re: Please help ,I dont know what is the problem in my code? Reply with quote



Quote:
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").newInstance();
con = DriverManager.getConnection(
"jdbc:odbc:cnEVA ");
^

Is this space really in there?

Quote:
} catch (SQLException cnfe) {
error = "SQLException: Could not connect to database.";
throw new SQLException(error);

That's the place where exception has been thrown.

Quote:
Exception in thread "main" java.sql.SQLException: SQLException: Could
not connect to database.
at Books.connect(Books.java:2Cool
at db.main(db.java:12)



Back to top
kaiwing18@hotmail.com
Guest





PostPosted: Sat Jan 17, 2004 2:46 am    Post subject: Re: Please help ,I dont know what is the problem in my code? Reply with quote



Hi Markku,

Thank you very much for your help.Yes, you are correct. Ihave fixed the problem.

Ricky
Back to top
Andy Grove
Guest





PostPosted: Mon Jan 19, 2004 9:23 pm    Post subject: Re: Please help ,I dont know what is the problem in my code? Reply with quote

[email]kaiwing18 (AT) hotmail (DOT) com[/email] wrote in message news:<d82f3f5c.0401161846.47140939 (AT) posting (DOT) google.com>...
Quote:
Hi Markku,

Thank you very much for your help.Yes, you are correct. Ihave fixed the problem.

Ricky

If you want to learn more about JDBC coding you might want to try out
a code generator product like FireStorm
(http://www.codefutures.com/products/firestorm) to see how it
generates JDBC connection code and how it generates code for SQL
statements).

Andy.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.