How to display result of database query in JFrame?

How to display result of oracle database query in JFrame?

This is part of my code:

String username, password;

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

OracleConnection conn = DriverManager.getConnection(String url, String username, String password);

Statement s= conn.createStatement();

ResultSet q= s.executeQuery("SELECT A, B, C FROM TABLE X");

Forget what url, username & password are. Is there any problem with my code?

What should be next if I want to display result of the query in a table in JFrame?Thx !

[598 byte] By [CuriousDummya] at [2007-11-27 2:07:23]
# 1

Simplest method is to pack it all into a JLabel and add that to a JFrame.

Why don't you read the JDBC and Swing tutorials, it would help you immensly.

Edit: Or add a JTextArea to a JFrame and write each line as you get it.

But that is a little sarcastic, as the question you have asked doesn't really make much sense. How do you want to display? Simple lines of text, columns of data, each column in a labeled textfield one record at a time? etc, etc

As stated earlier, read the Tutorials.

masijade.a at 2007-7-12 1:55:27 > top of Java-index,Java Essentials,Java Programming...
# 2

masijade,first of all, thank you.

I read some examples on java.sun.com already but that cannot be applied to my case. Or maybe I just don't know how apply it to my case. I am really bad at programming.Sorry.

Btw, there are 7 syntax errors in this line:

OracleConnection conn = DriverManager.getConnection(url, username, password);

I got these:

Compiling 1 source file to .java

java:136: ')' expected^

java:136: not a statement

java:136: ';' expected

java:136: not a statement

java:136: ';' expected

java:136: not a statement

java:136: ';' expected

^

7 errors

BUILD FAILED (total time: 3 seconds)

I have declared url, username, password as String and assigned value to all of them. What's wrong with it?

CuriousDummya at 2007-7-12 1:55:27 > top of Java-index,Java Essentials,Java Programming...
# 3
Show a few lines before this line, as your problem is probably there.
masijade.a at 2007-7-12 1:55:27 > top of Java-index,Java Essentials,Java Programming...
# 4

import java.io.*;

import java.sql.*;

import oracle.jdbc.driver.*;

import oracle.sql.*;

public class JFrame extends javax.swing.JFrame {

/** Creates new form JFrame */

public JFrame() {

initComponents();

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new JFrame().setVisible(true);

String url,username, password;

url="xxxxxxxxxx";

username = "xxxxx";

password = "xxxxx";

// Connection

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

OracleConnection conn = DriverManager.getConnection(url, username, password);

CuriousDummya at 2007-7-12 1:55:27 > top of Java-index,Java Essentials,Java Programming...
# 5

public class JFrame extends javax.swing.JFrame {

You have your class named the same as it's parent, that's a bad idea. It's legal, but very prone to confusion. Give it a unique name to avoid referencing the wrong class.

About displaying the results, I'm assuming you'll have multiple records each with multiple fields right? A JTable would be well suited for displaying that. You can add each record to a row, with each field in it's own column.

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

hunter9000a at 2007-7-12 1:55:27 > top of Java-index,Java Essentials,Java Programming...
# 6

How to create JTable with unknown no. of rows? How to get no. of rows of a query?

I saw the demo of creating JTable on java.sun.com but the the table has a certain no. of rows which is not applicable to my case.

Suppose the result of query is a table with 3 attributes so there are 3 columns in the table.

R contains the result of the query.

Should it be something like this if I want to create JTable of the query?

How to make n rows of {R.getString(1),R.getString(2),R.getString(3)};?

public SimpleTableDemo() {

super(new GridLayout(1,0));

String[] columnNames = {"A",

"B",

"C",

};

while (R.next())

{

// content of a row

Object[][] data = {R.getString(1),R.getString(2),R.getString(3)};

}

I can't run it because I still can't debug my code which is said before.

Thx!

CuriousDummya at 2007-7-12 1:55:27 > top of Java-index,Java Essentials,Java Programming...
# 7
In the future, Swing related questions should be posted in the Swing forum.Here is a simple example using a JTable: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=5123381
camickra at 2007-7-12 1:55:28 > top of Java-index,Java Essentials,Java Programming...
# 8
That example is exactly what I'm looking for! Thank you so much!2 more questions, what is the meaning of try{} and catch{}? Why try{} enclose the main body of the code?Message was edited by: CuriousDummy
CuriousDummya at 2007-7-12 1:55:28 > top of Java-index,Java Essentials,Java Programming...