Results 1 to 9 of 9
  1. #1
    casid is offline Member
    Join Date
    Nov 2008
    Posts
    18
    Rep Power
    0

    Default [SOLVED] database connection

    Hi people,

    I have problem with getting value from textfield s text and use it in sql query ,and another problem is using data in textare field. could you pls tell me the problem here,

    Java Code:
    public void nameSearch(){
    		
        name=jTextField1.getText();
    
    	try {
    		
    		Class.forName(JDBC_DRIVER).newInstance();
    	    con= DriverManager.getConnection(DATABASE_URL,USERNAME,PASSWORD);
    	    stm=con.createStatement();
    	    rs=stm.executeQuery("select * from guest where firstname="+name+"");
     
    	    while(rs.next()){
    	    	
    	     	jTextArea1.setText(rs.getString("firstname"));
    	    }
    	stm.close();
    	con.close();
    	} catch (Exception e) {
    		// TODO: handle exception
    		e.getCause();
    	}
    		
    	}
    when I write sql code by manually, giving value by myself, program runs however that textarea gets only one data although there are more datas.



    thank you for any support,

  2. #2
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    Java Code:
    public void nameSearch(){
    		
        name=jTextField1.getText();
    
    	try {
    		
    		Class.forName(JDBC_DRIVER).newInstance();
    	    con= DriverManager.getConnection(DATABASE_URL,USERNAME,PASSWORD);
    	    stm=con.createStatement();
    	    rs=stm.executeQuery("select * from guest where firstname="+name+"");
                // clear the text area.
                jTextArea1.setText("");
    	    while(rs.next()){
    	    	// add the data to the text area.
    	     	jTextArea1.append(rs.getString("firstname") + "\n");
    	    }
    	stm.close();
    	con.close();
    	} catch (Exception e) {
    		// TODO: handle exception
    		e.getCause();
    	}
    		
    	}
    The problem that you where setting the text area text, which erases the text that was there before it, so what you needed to do is append the text to it.
    Last edited by hawaiian robots; 05-31-2009 at 03:02 AM.

  3. #3
    casid is offline Member
    Join Date
    Nov 2008
    Posts
    18
    Rep Power
    0

    Default

    do you have any idea how to get textfield s value for my sql query,?

    thank you hawaiin robots ,

    Ok I handled it, '"+name+"' is the solution. thanks again
    Last edited by casid; 05-31-2009 at 10:16 PM. Reason: found out the solution

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Hi,
    One small suggestion here.Anyway, you are going to set the value dynamically for the query.In order to avoid query formation errors, you
    can use PreparedStatement like this below.

    Ex:

    PreparedStatement ps = con.prepareStatement("select * from guest where firstname = ?");
    ps.setString(1,name);
    Ramya:cool:

  5. #5
    casid is offline Member
    Join Date
    Nov 2008
    Posts
    18
    Rep Power
    0

    Default

    thank you for your suggestion however that I havent used it, so how will I get the result data.?
    which of method return query result ? you know with resultset
    rs=stm.executeQuery("select * from guest where firstname="+name+"");
    // clear the text area.
    jTextArea1.setText("");
    while(rs.next()){
    // add the data to the text area.
    jTextArea1.append(rs.getString("firstname") + "\n");

    }
    thank you for your help.

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Hi,

    Go thru this piece of code for returning the ResultSet.
    PreparedStatement ps =
    con.prepareStatement("select * from guest where firstname = ?");
    ps.setString(1,name);
    ResultSet rs = ps.executeQuery();

    Also please go thru this link for better understanding of ResultSet.
    Using Prepared Statements (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics)

    -Regards
    Ramya
    Ramya:cool:

  7. #7
    casid is offline Member
    Join Date
    Nov 2008
    Posts
    18
    Rep Power
    0

    Default

    thank you very much

  8. #8
    casid is offline Member
    Join Date
    Nov 2008
    Posts
    18
    Rep Power
    0

    Default

    thank you RamyaSivakanth,

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

Similar Threads

  1. [SOLVED] web.xml config for database connection
    By simo_mon in forum Java Servlet
    Replies: 7
    Last Post: 02-10-2009, 09:45 AM
  2. database connection in jsf
    By Srikala in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 10-06-2008, 06:53 AM
  3. Database Connection
    By CompleteBeginner in forum New To Java
    Replies: 2
    Last Post: 05-24-2008, 02:30 PM
  4. Database Connection
    By vipinkumarsolanki in forum Advanced Java
    Replies: 2
    Last Post: 11-26-2007, 06:36 AM
  5. Database connection
    By oaklander in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 11:11 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
  •