Results 1 to 7 of 7
  1. #1
    style is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Checking if IP already exists - MySQL

    Hello guys! I started coding in java just a month ago and I'm still super confused...
    I have made a simple db and I made a register form for it.anyways I would like to check if the IP adress is already existing in the db and here my problem starts...
    I have the following code:

    Java Code:
                 String driverName = "com.mysql.jdbc.Driver";
                 String dbURL = "jdbc:mysql://mydburl.net:3306/java";
                 String userName = "ava";
                 String userPwd = "1234";
                 Connection dbConn1;
             try {
                Class.forName(driverName);
                dbConn1 = DriverManager.getConnection(dbURL, userName, userPwd);
                Statement select = (Statement) dbConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,	ResultSet.CONCUR_READ_ONLY);
                String query="SELECT ip "+
                            "FROM Users "+
                            "where ip = '"+ ip +"'";
                                 ResultSet result = select.executeQuery(query);
                while (result.next()) {
                                String ip1 = result.getString("ip1"); 
                              }
                                result.beforeFirst();
                                    if(result.next()){
                                        lblError.setText("IP already exists");
                                    }else{
                                        lblError.setText("IP already exists"); 
                                    }
                  select.close();
                  dbConn1.close();
            }catch (Exception e) {
                lblError.setText(e.getLocalizedMessage());
    Isn't it supposed to print that the IP already exist in the db?

    Can someone help me with this?

    Any reply is highly appreciated!

    THANKS in advance!
    Last edited by style; 02-07-2012 at 07:45 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Checking if IP already exists - MySQL

    Does the IP exist in the database? Why are you looping over the ResultSet twice? FWIW, I'd highly recommend using a PreparedStatement

  3. #3
    style is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Checking if IP already exists - MySQL

    Hi doWhile thanks for your reply! The IP already exists..Can you give me a code sample,since I have no idea what PreparedStatement is....


    PS - I fixed the above while code - it was a typo
    Last edited by style; 02-07-2012 at 07:46 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Checking if IP already exists - MySQL

    Quote Originally Posted by style View Post
    I have no idea what PreparedStatement is....
    Sounds like a great place to start a google search.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    ContoNick is offline Member
    Join Date
    Dec 2011
    Location
    Croatia,Zagreb
    Posts
    24
    Rep Power
    0

    Default Re: Checking if IP already exists - MySQL

    check here
    ResultSet (Java 2 Platform SE v1.4.2)

    getString method

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Checking if IP already exists - MySQL

    Quote Originally Posted by ContoNick View Post
    Please don't post links to the API for old, EOL versions of Java. Bookmark these:
    Java Platform SE 6
    Java Platform SE 7

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Checking if IP already exists - MySQL

    1. As suggested use a PreparedSTatement.
    2. Since all you want to know is if that IP already exists, then if rs.next() is true you know the IP is in the database. No need to read the value or anything. Indeed you could do:
    SELECT 1 FROM <your-table> WHERE IP = <the IP to test>

Similar Threads

  1. Determining which choice exists
    By kjkrum in forum XML
    Replies: 1
    Last Post: 12-16-2011, 12:27 AM
  2. How do I check if a database exists ...Any help?
    By nmvictor in forum New To Java
    Replies: 5
    Last Post: 05-09-2010, 04:21 PM
  3. Checking if file exists in a directory
    By achilles in forum New To Java
    Replies: 7
    Last Post: 08-10-2009, 02:43 AM
  4. How to check whether file is exists or not
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 10:13 AM
  5. Check if a web page exists or not
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •