Welcome to Dream.In.Code
Become a Java Expert!

Join 149,520 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,400 people online right now. Registration is fast and FREE... Join Now!




MySql returning nothing

2 Pages V  1 2 >  
Reply to this topicStart new topic

MySql returning nothing

ap0c0lyps3
4 Jul, 2007 - 05:16 AM
Post #1

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
Hey, I'm having problems with this code

CODE
    private boolean isLogged() {
        ResultSet is = null;
        
    try {
            is = dtb.query("SELECT * FROM `active_users` WHERE ip = '"+this.IP+"'");
        } catch(Exception e) {
            System.err.println("Couldn't execute SQL query: "+e);
        }
        
        try {
            int count = 0;

            while(is.next())
                count++;
                
            UI.showMsg("Number of records "+count);
            is.close();
            if(count > 0) {
                if(debug)
                    System.out.println("Logged in");
                    return true;
            }
            else {
                if(debug)
                    System.out.println("Logged out");
                    return false;
            }
        } catch(Exception e) {
            if(debug)
                    System.err.println("Error checking for row: "+e);
        }
        return false;       //If it got here it means there is an error
    }


DB.java
CODE
/*
* DB.java
*
* Created on 10 June 2007, 01:38
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package icadmin.MySql;

import java.sql.*;
import icadmin.UI;

/**
*
*/
public class DB {
    Connection con = null;
    
    /** Creates a new instance of DB */
    public DB(String serv, String db, String user, String pass) {
        String url = "jdbc:mysql://"+serv+"/"+db+"?user="+user+"&password="+pass;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection(url);
        } catch (Exception e) {
            UI.showErr("Error: "+e);
        }
    }
    
    public ResultSet query(String que) {
        if(con == null) return null;
        
        Statement st = null;
        ResultSet rs = null;
        
        try {
            st = con.createStatement();
            rs = st.executeQuery(que);
        } catch (SQLException e) {
            System.err.println("Culdn't execute SQL command: "+e);
            return null;
        } finally {
            try {
                st.close();
            } catch(SQLException e) {}
        }
        que = null;
        return rs;
    }
    
    public boolean close() {
        try {
        if(con != null)
          con.close();
      } catch(SQLException e) {
          return false;
      }
      return true;
    }
    
}

function isLogged() never returns true even if there are entries in thre database.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: MySql Returning Nothing
4 Jul, 2007 - 08:29 AM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
Are you getting any exceptions? If not, is debug set to true?
User is online!Profile CardPM
+Quote Post

gyron
RE: MySql Returning Nothing
5 Jul, 2007 - 02:45 AM
Post #3

D.I.C Head
**

Joined: 9 Jan, 2007
Posts: 61


My Contributions
try this:
CODE

resultSet = statement.executeQuery(SELECT * FROM active_users WHERE ip = '"+this.IP+"'");


where statement = connection.createStatement();
and connection is your connection to the database
Note that i have removed the quotes in your select statement, assuming active_users to be a database table or query
User is offlineProfile CardPM
+Quote Post

Programmist
RE: MySql Returning Nothing
5 Jul, 2007 - 03:02 AM
Post #4

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(gyron @ 5 Jul, 2007 - 03:45 AM) *

try this:
CODE

resultSet = statement.executeQuery(SELECT * FROM active_users WHERE ip = '"+this.IP+"'");


where statement = connection.createStatement();
and connection is your connection to the database
Note that i have removed the quotes in your select statement, assuming active_users to be a database table or query


The problem with your example is twofold:

1. executeQuery takes a String as an argument, so you have to have quotes.

2. By hard coding the query, you limit flexibility.
User is online!Profile CardPM
+Quote Post

gyron
RE: MySql Returning Nothing
5 Jul, 2007 - 04:41 AM
Post #5

D.I.C Head
**

Joined: 9 Jan, 2007
Posts: 61


My Contributions
Actually i wanted to take out the single quotes around active_users and leave the select statement in quotes; sorry for the mistake.
User is offlineProfile CardPM
+Quote Post

ap0c0lyps3
RE: MySql Returning Nothing
5 Jul, 2007 - 06:34 AM
Post #6

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
Yoh it was that a simple mistake... embarassing. It doesn't have to be very flexible because all it does is check whether the user's IP is in the list.

It was right already
User is offlineProfile CardPM
+Quote Post

gyron
RE: MySql Returning Nothing
5 Jul, 2007 - 06:34 AM
Post #7

D.I.C Head
**

Joined: 9 Jan, 2007
Posts: 61


My Contributions
Am glad to have helped a neighbor smile.gif
User is offlineProfile CardPM
+Quote Post

ap0c0lyps3
RE: MySql Returning Nothing
7 Jul, 2007 - 02:41 AM
Post #8

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
I just saw now when debugging the program that it throws an SQLException at
CODE
while(is.next())

Please Help!!
User is offlineProfile CardPM
+Quote Post

ap0c0lyps3
RE: MySql Returning Nothing
7 Jul, 2007 - 02:50 AM
Post #9

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
This might just be the most embarrasing I ever have to post.... I forgot to include the MySql jar file into the program... Ya even more embarassing then above
User is offlineProfile CardPM
+Quote Post

ap0c0lyps3
RE: MySql Returning Nothing
7 Jul, 2007 - 02:55 AM
Post #10

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
And it still doesn't work omw
User is offlineProfile CardPM
+Quote Post

ap0c0lyps3
RE: MySql Returning Nothing
7 Jul, 2007 - 03:06 AM
Post #11

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
You people probably think I'm mad or something but it's working again.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: MySql Returning Nothing
7 Jul, 2007 - 05:53 PM
Post #12

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
The next time you feel like posting, wait 24 hours. If the problem still isn't resolved, then post.
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:19PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month