Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-02-2008, 02:45 AM
AJG AJG is offline
Member
 
Join Date: Apr 2008
Posts: 7
Rep Power: 0
AJG is on a distinguished road
Default [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
  #2 (permalink)  
Old 04-02-2008, 04:42 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,525
Rep Power: 9
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
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.
Resources:
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.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
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, 12:18 PM
AJG AJG is offline
Member
 
Join Date: Apr 2008
Posts: 7
Rep Power: 0
AJG is on a distinguished road
Default
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, 12:26 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,525
Rep Power: 9
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
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.
Resources:
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.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
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, 12:56 PM
AJG AJG is offline
Member
 
Join Date: Apr 2008
Posts: 7
Rep Power: 0
AJG is on a distinguished road
Default
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, 01:10 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,525
Rep Power: 9
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
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.
Resources:
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.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
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, 01:31 PM
AJG AJG is offline
Member
 
Join Date: Apr 2008
Posts: 7
Rep Power: 0
AJG is on a distinguished road
Default
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, 03:30 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,525
Rep Power: 9
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
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.
Resources:
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.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
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, 12:52 PM
AJG AJG is offline
Member
 
Join Date: Apr 2008
Posts: 7
Rep Power: 0
AJG is on a distinguished road
Default
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
Reply

Bookmarks

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

BB 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 04:55 PM
Modifying data in database table using PreparedStatement Java Tip Java Tips 0 02-09-2008 08:22 PM
Plz help ... retreiving data from an access database table.... austinsmiles New To Java 1 02-01-2008 01:21 PM
Upload excel data to access database ravikumar.achi New To Java 0 11-12-2007 01:52 PM
How to query data from database using SSL mano New To Java 0 08-02-2007 05:30 PM


All times are GMT +2. The time now is 10:03 PM.



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