JDBC
hello.
thanks for the reply.how can i modify the modified code below so that users of my video library system would be able to view CD/DVD/Game information by name, age category, type and year? the text area is not displaying in a proper size - i have to use the scroll pane to view the data in the text area. i should be able to view the data in the text area without a scrollpane because there is not much data in the database.how can i make the text area bigger?
thanks.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
publicclass ViewProductDetailsextends JFrame{
JPanel pnlBox, pnlBody, pnlFooter;
JCheckBox name;
JCheckBox ageCategory;
JCheckBox type;
JCheckBox year;
JButton returnToProductMenu;
JTextArea jta;
Container contentpane;
Connection db;
Statement statement;
publicvoid makeConnection(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){
System.out.println("Problem loading the driver");
}
}
publicvoid setHostURL(){
String url ="jdbc:odbc:VideoLibrary";
closeDB();
try{
db = DriverManager.getConnection(url,"","");
statement = db.createStatement();
DatabaseMetaData dbmd = db.getMetaData();
ResultSet rs = dbmd.getTables(null, null, null,new String[]{"TABLE"});
}
catch(Exception e){
System.out.println("Could not initialise the database");
e.printStackTrace();
}
}
publicvoid selectProductOne(){
try{
ResultSet rs1 = statement.executeQuery("SELECT * FROM Product ORDER BY name");
ResultSetMetaData rsmd1 = rs1.getMetaData();
for(int i = 1; i <= rsmd1.getColumnCount(); i++){
jta.append(rsmd1.getColumnName(i) +"");
}
jta.append("\n");
while(rs1.next()){
for(int i = 1; i <= rsmd1.getColumnCount(); i++){
jta.append(rs1.getObject(i) +"");
}
jta.append("\n");
}
}
catch(SQLException ea){
ea.printStackTrace();
}
}
publicvoid selectProductTwo(){
try{
ResultSet rs2 = statement.executeQuery("SELECT * FROM Product ORDER BY ageCategory");
ResultSetMetaData rsmd2 = rs2.getMetaData();
for(int i = 1; i <= rsmd2.getColumnCount(); i++){
jta.append(rsmd2.getColumnName(i) +"");
}
jta.append("\n");
while(rs2.next()){
for(int i = 1; i <= rsmd2.getColumnCount(); i++){
jta.append(rs2.getObject(i) +"");
}
jta.append("\n");
}
}
catch(SQLException eb){
eb.printStackTrace();
}
}
publicvoid selectProductThree(){
try{
ResultSet rs3 = statement.executeQuery("SELECT * FROM Product ORDER BY type");
ResultSetMetaData rsmd3 = rs3.getMetaData();
for(int i = 1; i <= rsmd3.getColumnCount(); i++){
jta.append(rsmd3.getColumnName(i) +"");
}
jta.append("\n");
while(rs3.next()){
for(int i = 1; i <= rsmd3.getColumnCount(); i++){
jta.append(rs3.getObject(i) +"");
}
jta.append("\n");
}
}
catch(SQLException ec){
ec.printStackTrace();
}
}
publicvoid selectProductFour(){
try{
ResultSet rs4 = statement.executeQuery("SELECT * FROM Product ORDER BY year");
ResultSetMetaData rsmd4 = rs4.getMetaData();
for(int i = 1; i <= rsmd4.getColumnCount(); i++){
jta.append(rsmd4.getColumnName(i) +"");
}
jta.append("\n");
while(rs4.next()){
for(int i = 1; i <= rsmd4.getColumnCount(); i++){
jta.append(rs4.getObject(i) +"");
}
jta.append("\n");
}
}
catch(SQLException ed){
ed.printStackTrace();
}
}
publicvoid closeDB(){
try{
if(statement !=null){
statement.close();
}
if(db !=null){
db.close();
}
}
catch(Exception e){
System.out.println("Could not close the current connection");
e.printStackTrace();
}
}
public ViewProductDetails(){
super("View Product Details");
contentpane = getContentPane();
contentpane.setLayout(new BorderLayout());
pnlBox =new JPanel();
pnlBody =new JPanel();
pnlFooter =new JPanel();
jta =new JTextArea();
jta.setFont(new Font("Serif", Font.PLAIN, 12));
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jta.setEditable(false);
name =new JCheckBox("Name");
ageCategory =new JCheckBox("Age Category");
type =new JCheckBox("Type");
year =new JCheckBox("Year");
pnlBox.add(name);
pnlBox.add(ageCategory);
pnlBox.add(type);
pnlBox.add(year);
JScrollPane jsp =new JScrollPane(jta);
pnlBody.add(jsp, BorderLayout.CENTER);
returnToProductMenu =new JButton("Return To Product Menu");
pnlFooter.add(returnToProductMenu);
contentpane.add(pnlBox,BorderLayout.NORTH);
contentpane.add(pnlBody,BorderLayout.CENTER);
contentpane.add(pnlFooter,BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
name.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
makeConnection();
setHostURL();
selectProductOne();
closeDB();
}
});
ageCategory.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
makeConnection();
setHostURL();
selectProductTwo();
closeDB();
}
});
type.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
makeConnection();
setHostURL();
selectProductThree();
closeDB();
}
});
year.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
makeConnection();
setHostURL();
selectProductFour();
closeDB();
}
});
returnToProductMenu.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
setVisible(false);
}
});
}
publicstaticvoid main(String[] args){
new ViewProductDetails();
}
}

