Thread: Java and sql

Results 1 to 4 of 4
  1. #1 Java and sql 
    Extreme Donator


    Join Date
    Dec 2011
    Posts
    323
    Thanks given
    92
    Thanks received
    46
    Rep Power
    81
    Hello. I'm trying to edit a database field using java. And I want to retrieve the information in the database, to check if it matches the key stored locally on the java program.
    And if it matches, update the information on database, otherwise it should not
    How would I go about doing that using prepared statements?
    Reply With Quote  
     

  2. #2  
    Extreme Donator


    Join Date
    Dec 2011
    Posts
    323
    Thanks given
    92
    Thanks received
    46
    Rep Power
    81
    Quote Originally Posted by LoveGuru View Post
    PreparedStatement statement = connection.prepareStatement("SELECT * FROM table_name WHERE field='" + javaField + "'");
    Ok this is a good start, thanks!

    Im thinking more like this:
    PreparedStatement statement = connection.prepareStatement("SELECT * FROM table_name WHERE field=key'");

    How would I store the sql field in a java variable, or how can i compare them?

    if(key == javaKey){
    statement = connection.prepareStatement("UPDATE * FROM table_name WHERE field=key");
    }
    I can clearly see that I am doing something wrong here...
    Reply With Quote  
     

  3. #3  
    Extreme Donator


    Join Date
    Dec 2011
    Posts
    323
    Thanks given
    92
    Thanks received
    46
    Rep Power
    81
    Quote Originally Posted by LoveGuru View Post
    What are you trying to do?
    Retrieve key field from SQL EDIT: Well it doesnt need to retrieve it, but it needs to check if it matches, as stated below.
    Check if the key field matches the keyfield stored locally on the java program
    If the key fields matches, update the database with new information
    Reply With Quote  
     

  4. #4  
    Extreme Donator


    Join Date
    Dec 2011
    Posts
    323
    Thanks given
    92
    Thanks received
    46
    Rep Power
    81
    Quote Originally Posted by LoveGuru View Post
    Well can't really tell you the most efficient way unless you tell me what exactly it's used for.

    This code will work, but it can simply use the WHERE command in some situations and it'll be enough, again it all depends on the usage

    Code:
    	int javaKey = 1;
    	PreparedStatement statement = connection.prepareStatement("SELECT * FROM table_name WHERE some_field='something'");
    	ResultSet result = statement.executeQuery();
    	
    	if (result.hasNext()) { //if you're looking for more than 1 result or multiple elements in the table that should return, then you will have to loop until result doesn't have a next
    		//this means some of the table content has returned from our "statement"
    		int key = result.getInt("keyName"); //would suggest using the int parameter instead of the string as it's quicker, but this is just to be more user-friendly
    		if (key == javaKey) {
    			//do something
    		}
    	}
    Thanks I'll try this!

    Edit: There is no "hasNext() method. Do you mean next()?
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Downloading java and seting paths
    By Brucie in forum Tutorials
    Replies: 2
    Last Post: 03-29-2009, 11:22 PM
  2. Replies: 4
    Last Post: 03-11-2009, 11:23 PM
  3. silab items for item.java and item.cfg
    By Frosty Teh Snowman in forum Configuration
    Replies: 81
    Last Post: 08-04-2008, 04:38 AM
  4. Need silab webclient item.java and pickup command
    By FuturePkz in forum RS2 Server
    Replies: 0
    Last Post: 06-24-2008, 11:55 PM
  5. Dumped worldmap XML and SQL Files.
    By Zachera in forum Downloads
    Replies: 16
    Last Post: 05-25-2008, 07:50 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •