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

04-02-2008, 03:45 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 7
|
|
|
[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.
|
|

04-02-2008, 05:42 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,477
|
|
|
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.
|
|

04-02-2008, 01:18 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 7
|
|
Originally Posted by 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.
Ok, but how do i get the text from the database to be displayed in the JTextArea?
|
|

04-02-2008, 01:26 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,477
|
|
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.
|
|

04-02-2008, 01:56 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 7
|
|
Originally Posted by 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
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.
|
|

04-02-2008, 02:10 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,477
|
|
If you know how to connect a database, use a query to select an entry of the database.
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.
|
|

04-02-2008, 02:31 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 7
|
|
yeah thats similar to what i have already got:
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
|
|

04-03-2008, 04:30 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,477
|
|
|
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.
|
|

04-04-2008, 01:52 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 7
|
|
Originally Posted by Eranga
You have to use
text1.setText(firstrow);
Thanks Eranga! the text from the database is displayed in the text area!
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|