Results 1 to 9 of 9
Thread: [SOLVED] database connection
- 05-30-2009, 11:46 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 18
- Rep Power
- 0
[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,
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.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(); } }
thank you for any support,
- 05-31-2009, 02:53 AM #2
Member
- Join Date
- Mar 2009
- Posts
- 25
- Rep Power
- 0
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.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(); } }Last edited by hawaiian robots; 05-31-2009 at 03:02 AM.
- 05-31-2009, 10:01 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 18
- Rep Power
- 0
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 againLast edited by casid; 05-31-2009 at 10:16 PM. Reason: found out the solution
- 06-01-2009, 06:31 AM #4
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:
- 06-01-2009, 12:56 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 18
- Rep Power
- 0
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
thank you for your help.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");
}
- 06-01-2009, 01:20 PM #6
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
RamyaRamya:cool:
- 06-01-2009, 02:09 PM #7
Member
- Join Date
- Nov 2008
- Posts
- 18
- Rep Power
- 0
thank you very much
- 06-03-2009, 11:07 PM #8
Member
- Join Date
- Nov 2008
- Posts
- 18
- Rep Power
- 0
thank you RamyaSivakanth,
- 06-05-2009, 12:42 PM #9
Similar Threads
-
[SOLVED] web.xml config for database connection
By simo_mon in forum Java ServletReplies: 7Last Post: 02-10-2009, 09:45 AM -
database connection in jsf
By Srikala in forum JavaServer Faces (JSF)Replies: 0Last Post: 10-06-2008, 06:53 AM -
Database Connection
By CompleteBeginner in forum New To JavaReplies: 2Last Post: 05-24-2008, 02:30 PM -
Database Connection
By vipinkumarsolanki in forum Advanced JavaReplies: 2Last Post: 11-26-2007, 06:36 AM -
Database connection
By oaklander in forum New To JavaReplies: 2Last Post: 11-12-2007, 11:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks