using JCheckBox, JButton and JTextArea with JDBC

Hello.
Im wondering do any of you have links to web pages that include tutorials and source code on using JCheckBox, JButton and JTextArea with JDBC. would any of you who have experience with using JCheckBox, JButton, JTextArea and JDBC together be able to give me a few tips on how to select certain data from a table using JCheckBox, JButton and display the data in a JTextArea? examples of such data could be CD/DVD/Game data - i want users of my video library system to be able to view CD/DVD/Game information by name, age category, type and year. Users should be able to click on a check box (e.g. view by name, age category, type or year) and press a button. What would happen then is that data from the Product table would appear in the text area (which could be placed beneath the check box + button inside a frame).
Thank you.
[844 byte] By [warship] at [2007-11-20 9:33:48]
# 1 Re: using JCheckBox, JButton and JTextArea with JDBC
would any of you who have experience with using JCheckBox, JButton, JTextArea and JDBC together be able to give me a few tips on how to select certain data from a table using JCheckBox, JButton and display the data in a JTextArea?How to select data from a database table has nothing to do with how to use JCheckBox, JButton etc. I suggest you search for a JDBC tutorial and also a swing tutorial and read both.

A good place to start is JDBC Basics (http://java.sun.com/docs/books/tutorial/jdbc/index.html) & Using Swing (http://java.sun.com/docs/books/tutorial/uiswing/)
keang at 2007-11-10 2:14:50 >
# 2 Re: using JCheckBox, JButton and JTextArea with JDBC
hello.
thanks for the reply. how can I modify the following code so that JCheckBox can be used instead of JComboBox? i want users of my video library system to be able to view CD/DVD/Game information by name, age category, type and year. Users should be able to click on a check box (e.g. view by name, age category, type or year) and press a button. What would happen then is that data from the Product table would appear in the text area.

import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;

public class Exercise32_6 extends JApplet {
private JComboBox jcboTableName = new JComboBox();
private JTextArea jtaResult = new JTextArea();
private JButton jbtShowContents = new JButton("Show Contents");
private Statement stmt;

public void init() {
initializeDB();

jbtShowContents.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jbtShowContents_actionPerformed(e);
}
});

JPanel jPanel1 = new JPanel();
jPanel1.add(new JLabel("Table Name"));
jPanel1.add(jcboTableName);
jPanel1.add(jbtShowContents);

this.getContentPane().add(jPanel1, BorderLayout.NORTH);
this.getContentPane().add(new JScrollPane(jtaResult), BorderLayout.CENTER);
}

private void initializeDB() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
Connection connection = DriverManager.getConnection("jdbc:odbc:VideoLibrary");
System.out.println("Database connected");
stmt = connection.createStatement();
DatabaseMetaData dbMetaData = connection.getMetaData();
ResultSet rsTables = dbMetaData.getTables(null, null, null, new String[] {"TABLE"});
System.out.print("User tables: ");

while (rsTables.next()) {
jcboTableName.addItem(rsTables.getString("TABLE_NAME"));
}

}
catch (Exception ex) {
ex.printStackTrace();
}
}

private void jbtShowContents_actionPerformed(ActionEvent e) {
String tableName = (String)jcboTableName.getSelectedItem();

try {
String queryString = "select * from " + tableName;
ResultSet resultSet = stmt.executeQuery(queryString);
ResultSetMetaData rsMetaData = resultSet.getMetaData();

for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
jtaResult.append(rsMetaData.getColumnName(i) + " ");
}

jtaResult.append("\n");

while (resultSet.next()) {
for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
jtaResult.append(resultSet.getObject(i) + " ");
}

jtaResult.append("\n");
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {
Exercise32_6 applet = new Exercise32_6();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Exercise32_6");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(380, 180);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
warship at 2007-11-10 2:15:50 >
# 3 Re: using JCheckBox, JButton and JTextArea with JDBC
how can I modify the following code so that JCheckBox can be used instead of JComboBox?Well I'd start by reading the swing tutorial and working out how to use JCheckBox controls.

BTW when posting code please use code tags and fill your code with comments (your code should have comments anyway). Remember, you know what it does because you wrote it and presumably have tested it but most people here don't have the time or inclination to look through it to work out what its supposed to be doing.
keang at 2007-11-10 2:16:48 >
# 4 Re: using JCheckBox, JButton and JTextArea with JDBC
sorry here's the tagged version of the code

import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;

public class Exercise32_6 extends JApplet {
private JComboBox jcboTableName = new JComboBox();
private JTextArea jtaResult = new JTextArea();
private JButton jbtShowContents = new JButton("Show Contents");
private Statement stmt;

public void init() {
initializeDB();

jbtShowContents.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jbtShowContents_actionPerformed(e);
}
});

JPanel jPanel1 = new JPanel();
jPanel1.add(new JLabel("Table Name"));
jPanel1.add(jcboTableName);
jPanel1.add(jbtShowContents);

this.getContentPane().add(jPanel1, BorderLayout.NORTH);
this.getContentPane().add(new JScrollPane(jtaResult), BorderLayout.CENTER);
}

private void initializeDB() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
Connection connection = DriverManager.getConnection("jdbc:odbc:VideoLibrary");
System.out.println("Database connected");
stmt = connection.createStatement();
DatabaseMetaData dbMetaData = connection.getMetaData();
ResultSet rsTables = dbMetaData.getTables(null, null, null, new String[] {"TABLE"});
System.out.print("User tables: ");

while (rsTables.next()) {
jcboTableName.addItem(rsTables.getString("TABLE_NAME"));
}

}
catch (Exception ex) {
ex.printStackTrace();
}
}

private void jbtShowContents_actionPerformed(ActionEvent e) {
String tableName = (String)jcboTableName.getSelectedItem();

try {
String queryString = "select * from " + tableName;
ResultSet resultSet = stmt.executeQuery(queryString);
ResultSetMetaData rsMetaData = resultSet.getMetaData();

for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
jtaResult.append(rsMetaData.getColumnName(i) + " ");
}

jtaResult.append("\n");

while (resultSet.next()) {
for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
jtaResult.append(resultSet.getObject(i) + " ");
}

jtaResult.append("\n");
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {
Exercise32_6 applet = new Exercise32_6();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Exercise32_6");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(380, 180);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
warship at 2007-11-10 2:17:54 >
# 5 Re: using JCheckBox, JButton and JTextArea with JDBC
There are still no comments in the code which probably means either you're too lazy to put them in or it's not your code.

The way it works here is you put in some effort and we will try to help you. If you're not prepared to make any effort then why should we give up our free time (and valuable expertise) to help you.
keang at 2007-11-10 2:18:48 >