Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-02-2008, 03:45 AM
AJG AJG is offline
Member
 
Join Date: Apr 2008
Posts: 7
AJG is on a distinguished road
[SOLVED] Getting Data from a database
Hi, im new to java and i have been asked to create a tutorial, im not looking for mass help, its just the problem is that i have a database called JAVA and inside it is text (under the table name 'context' and column name 'content_text'.

this text i wish to insert into the tutorial, however i don't know how to display text in my tutorial, do i need to use a JTextArea or JPanel or something different and if so how do i do this, i have managed to set up my database correctly and also tested that the information is called correctly by using the 'System.out.println' function using dos. thank you in advance if you are able to help.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-02-2008, 05:42 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,477
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
I think the easiest way is use a JTextArea. In most classes you want to display multiple text lines. So it's easy.

On a JPanel you can't display text. It's a Swing Container, which is used to place and work-on with other controls.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-02-2008, 01:18 PM
AJG AJG is offline
Member
 
Join Date: Apr 2008
Posts: 7
AJG is on a distinguished road
Quote:
Originally Posted by Eranga View Post
I think the easiest way is use a JTextArea. In most classes you want to display multiple text lines. So it's easy.

On a JPanel you can't display text. It's a Swing Container, which is used to place and work-on with other controls.
Ok, but how do i get the text from the database to be displayed in the JTextArea?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-02-2008, 01:26 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,477
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
So you don't know how to work with database in Java? Better to read some articles, at the time I don't have a simple example with me.

Following article covers a lot in JDBC with Java.

JDBC - Java Database Connectivity
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-02-2008, 01:56 PM
AJG AJG is offline
Member
 
Join Date: Apr 2008
Posts: 7
AJG is on a distinguished road
Quote:
Originally Posted by Eranga View Post
So you don't know how to work with database in Java? Better to read some articles, at the time I don't have a simple example with me.

Following article covers a lot in JDBC with Java.

JDBC - Java Database Connectivity
OK thanks, i have a grasp of databases, like how to get it connected to the Java file, its just the output of the text from the database into the JTextArea.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-02-2008, 02:10 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,477
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
If you know how to connect a database, use a query to select an entry of the database.

Code:
try { String url = "jdbc_connection_to_DB"; Connection conn = DriverManager.getConnection(url,"",""); Statement stmt = conn.createStatement(); ResultSet rs; rs = stmt.executeQuery("SELECT content_text FROM content"); while ( rs.next() ) { String text= rs.getString("content_text"); System.out.println(text); } conn.close(); } catch (Exception e) { System.err.println("Got an exception! "); System.err.println(e.getMessage()); }
In this way you can get the text in the column.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-02-2008, 02:31 PM
AJG AJG is offline
Member
 
Join Date: Apr 2008
Posts: 7
AJG is on a distinguished road
yeah thats similar to what i have already got:

Code:
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String dataSourceName="java"; String dbURL = "jdbc:odbc:" + dataSourceName; Connection con = DriverManager.getConnection(dbURL,"",""); Statement s = con.createStatement(); s.execute("SELECT Content FROM content_table"); ResultSet rs = s.getResultSet(); if(rs != null); { rs.next(); { firstrow = rs.getString(1); JTextArea text1 = new JTextArea(firstrow); } s.close(); con.close(); } } catch (Exception e) { System.out.println("Error: " + e); }
However its the text in bold that i wish to use to insert the text into the JTextArea however this compiles but doesn't work, also firstrow is declared as a string at the top of this class
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-03-2008, 04:30 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,477
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
You have to use

text1.setText(firstrow);
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-04-2008, 01:52 PM
AJG AJG is offline
Member
 
Join Date: Apr 2008
Posts: 7
AJG is on a distinguished road
Quote:
Originally Posted by Eranga View Post
You have to use

text1.setText(firstrow);
Thanks Eranga! the text from the database is displayed in the text area!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help returning data from database dyn03 Database 0 03-11-2008 05:55 PM
Modifying data in database table using PreparedStatement Java Tip Java Tips 0 02-09-2008 09:22 PM
Plz help ... retreiving data from an access database table.... austinsmiles New To Java 1 02-01-2008 02:21 PM
Upload excel data to access database ravikumar.achi New To Java 0 11-12-2007 02:52 PM
How to query data from database using SSL mano New To Java 0 08-02-2007 06:30 PM


All times are GMT +3. The time now is 05:33 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org