hi guys i am trying to create a button that sends all the data returned from my ms access database into the fields of my jframe to the printer for a hardcopy, i have created a button to exit the frame, but now i am having difficulties creating a print button, any help or advice will be appreciated..

the coding is below it work with no problems
import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.math.*;
import javax.swing.border.*;
import java.awt.event.*;



public class DataOut extends JFrame implements ActionListener{



private JButton btnexit;


public DataOut()
{
Vector columnNames = new Vector();
Vector data = new Vector();


try
{
//  Connect to the Database


//connection object created using DriverManager class
//carpark is the name of the database


Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connect =DriverManager.getConnection("jdbc:odbc:carpark");


//  Read data from a table


String sql = "Select * from member";
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery( sql );
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();


//  Get column names


for (int i = 1; i <= columns; i++)
{
columnNames.addElement( md.getColumnName(i) );
}


//  Get row data


while (rs.next())
{
Vector row = new Vector(columns);


for (int i = 1; i <= columns; i++)
{
row.addElement( rs.getObject(i) );
}


data.addElement( row );
}


rs.close();
stmt.close();
}
catch(Exception e)
{
System.out.println( e );
}


//  Create table with database data


JTable table = new JTable(data, columnNames);
btnexit = new JButton("Exit");
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );


JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
buttonPanel.add(btnexit);
btnexit.addActionListener(this);



this.setSize(450,260);
this.setLocation(500,200);
this.show();


}



public static void main(String[] args)
{
DataOut frame = new DataOut();


}



public void actionPerformed(ActionEvent e){


if(e.getSource()==btnexit)
{
this.hide();
}
}
}

------------------------------------------------------------------------------------------------------

sorry if the code layout is'nt good am still a beginner....tc

Recommended Answers

All 2 Replies

ok i have created a class that sends the data base data to text file so that i can print off the text file...but i get errors when i run it.....any help pls............


====================================================

import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.math.*;
import javax.swing.border.*;
import java.awt.event.*;

Public class indata {

public indata(){

public static void main(String args[]){

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connect =DriverManager.getConnection("jdbc:odbc:carpark");


String sql = "SELECT * FROM member";
Statement stmt = connect.createStatement();


ResultSet resultset = stmt.executeQuery( sql );
ResultSetMetaData str_buf = resultsset.getMetaData();

StringBuffer str_buf = new StringBuffer();
String fileName = "info.txt";
FileWriter fw = new FileWriter(fileName);

String data_row = "";
fw.write("STUDENT_ID,NAME,PHONE,ADDRESS,RESULT");

while (resultset.next()) {
data_row = "\n";
data_row += resultset.getLong("STUDENT_ID");
data_row += ",\"" + resultset.getString("NAME").trim()+ "\"";
data_row += ",\"" +resultset.getString("PHONE").trim() + "\"";
data_row += ",\"" +resultset.getString("ADDRESS").trim() + "\"";
data_row += ",\"" +resultset.getString("RESULT").trim() + "\"";

fw.write(data_row);
}

fw.close();


}
}

}


===================================================
when i run it it creates the txt file but its empty... errors are below:
tc
===================================================
C:\Users\sars\Desktop>javac indata.java
indata.java:11: class, interface, or enum expected
Public class indata {
^
indata.java:15: illegal start of expression
public static void main(String args[]){
^
indata.java:54: class, interface, or enum expected
}→
^
3 errors

The compiler tells you exactly where the errors are. Can't you read the messages and go at the lines the message indicates to fix it?

C:\Users\sars\Desktop>javac indata.java
indata.java:11: class, interface, or enum expected
Public class indata {
^

As you can see the arrow pints at the 'P'. Perhaps there should be a lowercase 'p'.
And don't forget to close: stmt , resultset

And does this look OK to you?

ResultSetMetaData str_buf = resultsset.getMetaData();

StringBuffer str_buf = new StringBuffer();

Two different object with the same instance name: str_buf

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.