?
Solved

how to print a sql table.

Posted on 2005-03-03
21
Medium Priority
?
270 Views
Last Modified: 2008-02-01
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
Comment
Question by:hohingday
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 6
  • +1
21 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 13448646
0
 
LVL 15

Expert Comment

by:Javatm
ID: 13452225
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
 

Author Comment

by:hohingday
ID: 13452306
is there any easy way to be able to EASILY setup printing without using any third party tools?
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 15

Expert Comment

by:Javatm
ID: 13452374
Sorry that one is not free JasperReports is free :
http://jasperreports.sourceforge.net/
0
 
LVL 35

Expert Comment

by:girionis
ID: 13452404
> 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
 
LVL 15

Expert Comment

by:Javatm
ID: 13452482
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
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 300 total points
ID: 13452539
Here I think this is much proper for printing in JTable :
http://forum.java.sun.com/thread.jspa?threadID=240928&tstart=135
0
 

Author Comment

by:hohingday
ID: 13452676
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
 
LVL 15

Expert Comment

by:Javatm
ID: 13460258
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
 

Author Comment

by:hohingday
ID: 13460404
what you posted has not answered what I want, the term "printing" implies formatting.
0
 
LVL 35

Expert Comment

by:girionis
ID: 13460454
0
 
LVL 35

Expert Comment

by:girionis
ID: 13460472
0
 

Expert Comment

by:tegz
ID: 13460766
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
 

Expert Comment

by:tegz
ID: 13460776
sorry jumping in the qs, this is what i want to do too
0
 
LVL 35

Accepted Solution

by:
girionis earned 900 total points
ID: 13460921
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
 

Author Comment

by:hohingday
ID: 13461200
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
 
LVL 35

Expert Comment

by:girionis
ID: 13461278
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
 
LVL 15

Expert Comment

by:Javatm
ID: 13463761
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
 
LVL 15

Expert Comment

by:Javatm
ID: 13463764
> the term "printing" implies formatting.

Well printing ? is a concrete word formatting is another ;)
0
 

Author Comment

by:hohingday
ID: 13466027
error messgae: lpt1 access is denied when using FileWriter

im running windows xp, sp2 with printer connected via usb, any ideas?
0
 

Author Comment

by:hohingday
ID: 13466049
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question