Results 1 to 9 of 9
  1. #1
    AJG
    AJG is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    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.

  3. #3
    AJG
    AJG is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    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?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    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

  5. #5
    AJG
    AJG is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    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.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    If you know how to connect a database, use a query to select an entry of the database.

    Java 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.

  7. #7
    AJG
    AJG is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    yeah thats similar to what i have already got:

    Java 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); 
              [B]JTextArea text1 = new JTextArea(firstrow);[/B]
                }
                    
    	     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

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  9. #9
    AJG
    AJG is offline Member
    Join Date
    Apr 2008
    Posts
    7
    Rep Power
    0

    Default

    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!

Similar Threads

  1. Upload excel data to access database
    By ravikumar.achi in forum New To Java
    Replies: 20
    Last Post: 01-19-2012, 11:30 AM
  2. Replies: 0
    Last Post: 03-11-2008, 04:55 PM
  3. Replies: 0
    Last Post: 02-09-2008, 08:22 PM
  4. Replies: 1
    Last Post: 02-01-2008, 01:21 PM
  5. How to query data from database using SSL
    By mano in forum New To Java
    Replies: 0
    Last Post: 08-02-2007, 05:30 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
  •