Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

how to print a sql table.

if i have this sql query, select * from employees
how can i output all the results to a printer like a report.
use something like a BufferedStreamWriter?
0
hohingday
Asked:
hohingday
  • 7
  • 6
  • 6
  • +1
2 Solutions
 
girionisCommented:
0
 
JavatmCommented:
Printing report in java is very hard and not to easy specially if your new to it. You can use
such alternative through using free reporting tool :
http://www.jasperassistant.com/
0
 
hohingdayAuthor Commented:
is there any easy way to be able to EASILY setup printing without using any third party tools?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JavatmCommented:
Sorry that one is not free JasperReports is free :
http://jasperreports.sourceforge.net/
0
 
girionisCommented:
> is there any easy way to be able to EASILY setup printing without using any third party tools?

The links I posted should explain how you can do it. And they only use the default API that comes with the JDK.
0
 
JavatmCommented:
I have a printing class that prints using a JTextPane if you could display the contents of your
table in JTextPane I can provide you the class.
0
 
JavatmCommented:
Here I think this is much proper for printing in JTable :
http://forum.java.sun.com/thread.jspa?threadID=240928&tstart=135
0
 
hohingdayAuthor Commented:
so say i got this query rs = stmt.executeQuery("SELECT * FROM employees");
all i want is for it to be printed out simply in a table format
eg
name            | dob             |    address
hohingday      | 11-11-70    | 82 random address, manc, etc
hohingday      | 11-11-70    | 82 random address, manc, etc
hohingday      | 11-11-70    | 82 random address, manc, etc
hohingday      | 11-11-70    | 82 random address, manc, etc


i dont want to use any tools to do it, can any1 show me a example of this?
i thought it wud have been possible by using StreamWriter and adding "|" to organise the table....
0
 
JavatmCommented:
The link above shows you the detail on how to print it but to format that would be another question which
should be posted in another thread, since it was not included in your original question.
0
 
hohingdayAuthor Commented:
what you posted has not answered what I want, the term "printing" implies formatting.
0
 
girionisCommented:
0
 
girionisCommented:
0
 
tegzCommented:
im looking at the http://www.rgagnon.com/javadetails/java-0051.html example
and there seems to be a few problems with it.

1) how would i know when is the end of a page, there will be possibility of ending up with truncated text?
2) how would i organise spaccing so the print outs are tabular like....
3) if say one name was long then i may end up with uneven tables, whats the best way to prevent this?
eg.
name            | dob             |    address
hohingday      | 11-11-70    | 82 random address, manc, etc
hohingdaysveryverylong name      | 11-11-70    | 82 random address, manc, etc
hohingday      | 11-11-70    | 82 random address, manc, etc

0
 
tegzCommented:
sorry jumping in the qs, this is what i want to do too
0
 
girionisCommented:
1) You will probably indicate the end of the page when you call the close() method on the stream.
2) use the \t character (it is the tab character)
3) Yes there is this possibility. You could prevent it if you use multiple \t characters, if you know beforehand the length of the column name. Otherwise you will either loose some text or it will be printed at the next line.
0
 
hohingdayAuthor Commented:
nice to see my qs getting hijacked :P
actually point 3) is something i want to ask, say if i know the max length of a column is 25, is there a way to extend all entries to 25 characters, so that it would be neat?
0
 
girionisCommented:
You could use as many tabs as it takes to fill up the 25 character length. But you would have to find the length of the line first (say it is 10 characters), then subtract it from the total (25) and divide it by 4 (I think 4 characters is the length of the \t but I am not 100% sure, better check) to see how many tabs you will need.
0
 
JavatmCommented:
I have a codes created before for textpane if your data is on JTable we can try to cast it or better yet use JTextPane
and we can easily print what you want ;)
0
 
JavatmCommented:
> the term "printing" implies formatting.

Well printing ? is a concrete word formatting is another ;)
0
 
hohingdayAuthor Commented:
error messgae: lpt1 access is denied when using FileWriter

im running windows xp, sp2 with printer connected via usb, any ideas?
0
 
hohingdayAuthor Commented:
if i change my printer settings from printing to USB001 to LPT1 then it queues but still fails
if i leave it on USB001 i get the error message i printed b4.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 6
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now