Home arrow static arrow Java Programming [Archive] - ResultSet methods dont work...?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - ResultSet methods dont work...?
This topic has 60 replies on 5 pages.    « Previous | 1 | 2 | 3 | 4 | 5 | Next »

Posts:13,769
Registered: 00-11-29
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:10 AM (reply 15 of 60)



 
the end of the code tags is [/code]
 

Posts:19,725
Registered: 9/26/01
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:14 AM (reply 16 of 60)



 
the end of the code tags is [/code]

Hmm, I'd wonder what that "Preview" button is for...
Especially after having goofed up every single post.
 

Posts:13,769
Registered: 00-11-29
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:15 AM (reply 17 of 60)



 
Since we are on a new page I will repeat. You have posted two pieces of code. One is a method. The other is snippet. You don't explain how these two things are related. I'm not even sure they are.
 

Posts:343
Registered: 7/13/04
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:20 AM (reply 18 of 60)



 
Access {
} is the class
i call it using:
Access sql = new Access("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","nthompson","123");

then the Access class looks like this:
public class Access {  private static Connection dbConn = null;  public static Statement gStmt = nullpublic Access(String provider, String database, String username, String password ) throws Exception {  if (dbConn == null) {     try {       Class.forName(provider).newInstance();       dbConn = DriverManager.getConnection(database+"?user="+username+"&password="+password);     }     catch (Exception error) {       throw new Exception("Can't connect to the database: " + database);     }   }}


then the ins method is just a method a little farther down:

  public static ResultSet ins(String sql) throws Exception {  Statement stmt = null;  int rows = 0;  if (dbConn == null)    throw new Exception("No database connection");  try {     gStmt = dbConn.createStatement();    rows = gStmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);     key = gStmt.getGeneratedKeys();   } catch (Exception error) {     System.out.println(error);   }//   stmt.close();   return key;}


now back to where the main class is located is where i call that last snippet of coding:
                try {                        array = ReadData.ReadMessage(in);  // read each message and store in byte array: array.                               rs = sql.ins("INSERT INTO FrameData (" +                                                "FrameNumber, MessageId, PlatformId, TaskId, ParentId, TimeOfDay" +                                                        ") VALUES (" +                                                        frame + "," +                                                        mid + "," +                                                        pid + "," +                                                        tid + "," +                                                        paid + "," +                                                        day + ")");                                  int id = rs.getInt("ID");                                  System.out.println(id);                                 sql.gStmt.close();                                }                        }catch (EOFException error) {                                error.printStackTrace();                                return; // all done reading file                        } 


I didn't want to post all of this cause i know its a lot. does that help in understanding the tie between all the code?
 

Posts:21,718
Registered: 98-02-20
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:28 AM (reply 19 of 60)



 
I still don't like the code much.

Does this compile? That "snippet" from the main has a try/catch block that deals with java.sql.ResultSet, but there's no java.sql.SQLException caught. The getInt() method can throw java.sql.SQLException, and it appears inside your try block.

Please refer to my previous comment about using the "while (rs.next())" idiom. You posted more code, but it's still incorrect. I think I understand what you're doing. I don't think your implementation is correct.

%
 

Posts:21,718
Registered: 98-02-20
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:32 AM (reply 20 of 60)



 

Cross-posting eejit:

http://forum.java.sun.com/thread.jsp?thread=543274&forum=48&message=2636852
http://forum.java.sun.com/thread.jsp?forum=31&thread=543283&start=15&range=15&hilite=false&q=

%
 

Posts:13,769
Registered: 00-11-29
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:33 AM (reply 21 of 60)



 
To reiterate what the mod just said.

1. You are catching EOFException. Why? Is it even thrown by anything you are calling?
2. In ins (bad method name BTW) you catch Exception and squash it while you throw Exception from your method. Why? What is the point of that? I saw something similar in some code the other day at work. I'd love to understand the logic that produces that code.
3. I don't see where you call rs.next().
4. You don't show where you catch SQLException.
 

Posts:343
Registered: 7/13/04
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:40 AM (reply 22 of 60)



 

Cross-posting eejit:

http://forum.java.sun.com/thread.jsp?thread=543274&foru
=48&message=2636852
http://forum.java.sun.com/thread.jsp?forum=31&thread=54
283&start=15&range=15&hilite=false&q=

%

i'm really sorry about double posting, i posted it then realized i posted it in the wrong forum.
 

Posts:21,718
Registered: 98-02-20
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:42 AM (reply 23 of 60)



 
Next time that happens don't repost your question. Provide a link to the first one you submitted. There is no "wrong" forum; some are just more pertinent than others.

See my comments in your other thread for more detail.

%
 

Posts:19,725
Registered: 9/26/01
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:42 AM (reply 24 of 60)



 
i'm really sorry about double posting, i posted it
then realized i posted it in the wrong forum.

And simply posting a URL in the one, pointing it to the other rather than duplicating it, didn't cross your mind either.
 

Posts:21,718
Registered: 98-02-20
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:44 AM (reply 25 of 60)



 
I don't know where you read that declaring methods to throw Exception was a good idea, but I'd forget it fast if I were you.

Geez, why not go all the way and declare everything to throw java.lang.RuntimeException? "I could throw anything at any time - be alert!"

%
 

Posts:13,769
Registered: 00-11-29
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:47 AM (reply 26 of 60)



 
I don't know where you read that declaring methods to
throw Exception was a good idea, but I'd forget it
fast if I were you.

It's basically pointless because he's squashing all Exceptions anyway. So he's basically forcing any caller to write code to handle an undefined exception that will never actually be thrown.
 

Posts:19,725
Registered: 9/26/01
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:49 AM (reply 27 of 60)



 
Geez, why not go all the way and declare everything to
throw java.lang.RuntimeException? "I could throw
anything at any time - be alert!"
Obviously you meant java.lang.Throwable, but yeah!
 

Posts:343
Registered: 7/13/04
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:50 AM (reply 28 of 60)



 
To reiterate what the mod just said.

1. You are catching EOFException. Why? Is it even
thrown by anything you are calling?
the reason i throw an EOFExeption is because i read a file in the line just below the try { statement

2. In ins (bad method name BTW) you catch Exception
i'm sure its a bad method name becasue i've been barely working as a computer programmer for 3 weeks now and never went to any schooling for it. i'm really trying my best to learn all the techniques and norms for things. i've been able to learn just what i've been able to teach myself online (the java tutorials), and in these forums.

and squash it while you throw Exception from your
method. Why? What is the point of that? I saw
i have no idea! hehehe

something similar in some code the other day at work.
I'd love to understand the logic that produces that
code.
i'm sure you would love to understand it I'D EVEN LIKE TO UNDERSTAND IT! ;)

3. I don't see where you call rs.next().
i do the rs.next method but it has the EXACT same effect as when i call any other method in the ResultSet class

4. You don't show where you catch SQLException.
i didn't realize such a thing existed
 

Posts:21,718
Registered: 98-02-20
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:50 AM (reply 29 of 60)



 
That's true in the ins() method, but in the constructor he catches Exception and then rethrows it.

%
 
This topic has 60 replies on 5 pages.    « Previous | 1 | 2 | 3 | 4 | 5 | Next »