通过JAVA 想把SQL 中表的记录打印出 该如何做?谢谢大家 通过JDBC 连接好SQL后,想把查询到的表的记录打印出,不需特别的表,只要把数据打印出来 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是MYSQL的,SQL差不多,就是驱动换一下!希望对你有所帮助!import java.sql.*;public class mySQLdbBean { Connection Conn; ResultSet rs=null; boolean succeed=true; public mySQLdbBean(){ String strURL = "jdbc:mysql://localhost:3306/test"; try{ Class.forName("org.gjt.mm.mysql.Driver"); Conn= DriverManager.getConnection(strURL,"name","pwd"); } catch (Exception E) { succeed=false; } } public boolean Connected(){ return succeed; } public boolean executeSQL(String sqlString){ Statement stmt=null; boolean blnOK=true; try { stmt = Conn.createStatement(); stmt.executeUpdate(sqlString); } catch(SQLException ex){ blnOK=false; } return blnOK; } public ResultSet getResultSet(String sqlString){ Statement stmt=null; try { stmt = Conn.createStatement(); rs=stmt.executeQuery(sqlString); } catch(SQLException ex){ } return rs; }}这样rs.getString(0);得到第一个值;依此类推!字段类型不同方法也不同如getInt(); 把SQL查询到的记录,通过JTABLE 显示出以后,通过打印机输出?我现在不知如何处理打印输出这一部分 package test;import javax.swing.*;import java.awt.print.*;import java.awt.Graphics2D;import java.awt.Graphics;import java.util.Enumeration;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: </p> * @author * @version 1.0 */public class printProcess implements Printable{ //private printContents aPrintContents; private PageFormat aPageFormat; private boolean marginReset; private boolean directionReset; private double lMargin; private double rMargin; private double tMargin; private double bMargin; public printProcess() { //aPrintContents=new printContents(); aPageFormat=new PageFormat(); marginReset=false; directionReset=false; lMargin=0; rMargin=0; tMargin=0; bMargin=0; } public int print(Graphics g,PageFormat pf,int page) throws PrinterException { if(page>=1) return Printable.NO_SUCH_PAGE; Graphics2D g2=(Graphics2D)g; g2.translate(pf.getImageableX(),pf.getImageableY()); //get printer capabilities by dot double xPrinter=pf.getWidth(); double yPrinter=pf.getHeight(); double xRatio=xPrinter/21; //21 is the width of the paper double yRatio=yPrinter/29.7;//29.7 is the height of the paper //Enumeration contentEnum=this.aPrintContents.getContentList().elements(); //while(contentEnum.hasMoreElements()) //{ // printContent aPrintContent=(printContent)contentEnum.nextElement(); // g2.drawString(aPrintContent.getContent(),(int)(aPrintContent.getXPosition()*xRatio),(int)(aPrintContent.getYPosition()*yRatio)); //} g2.drawString("whatyayayaya",(int)(3*xRatio),(int)(3*yRatio)); for(int i=0;i<500;i++) g2.drawLine(0,0,i,0); for(int j=0;j<500;j++) g2.drawLine(0,0,0,j); return Printable.PAGE_EXISTS; } //public void setObjects(printContents thePrintContents) //{ // this.aPrintContents=thePrintContents; //} public void setLandscapeOrPortrait(int orient) { this.aPageFormat.setOrientation(orient); this.directionReset=true; } public void setMargin(double leftMargin ,double rightMargin,double topMargin, double bottomMargin) { this.lMargin=leftMargin; this.rMargin=rightMargin; this.tMargin=topMargin; this.bMargin=bottomMargin; this.marginReset=true; } //scheduled by user public void print() { //tell whether there exists sth 2 print //if(this.aPrintContents==null) // return; try { java.awt.print.PrinterJob job =PrinterJob.getPrinterJob(); PageFormat pf; //whether PageFormat is customed set or not pf=job.defaultPage(); if(this.marginReset) { Paper paper=new Paper(); double xRatio=pf.getWidth()/21; double yRatio=pf.getHeight()/29.7; paper.setImageableArea(this.lMargin*xRatio, this.rMargin*yRatio, pf.getWidth()-this.rMargin*xRatio-this.lMargin*xRatio, pf.getHeight()-this.bMargin*yRatio-this.tMargin*yRatio); pf.setPaper(paper); this.marginReset=false; } if(this.directionReset) { pf.setOrientation(this.aPageFormat.getOrientation()); this.directionReset=false; } //pf.setPaper(this.aPageFormat.getPaper()); job.setPrintable(this,pf); if(job.printDialog()) { job.print(); } } catch(Exception e) { e.printStackTrace(); } }} 参考jdk1.4新特性Merlin 的魔力:在 JDK 1.4 中打印http://www-900.ibm.com/developerWorks/cn/java/j-merlin/part10/index.shtml 数字格式化的一些问题 代码查错 read()和read(byte[])区别 Java有没有能操作.tar文件的API?(急,在线等) vc如何掉用java程序(急) 什么原因不能显示有图标的button? 很急的问题,请你帮帮忙 如何从String转换到一个InputStream? 各位侠义心肠的java大小牛们 用你们只字片言为孩子指点迷途吧 :) 一个难以调出的问题。在线等。 小弟刚玩java,问一菜菜问题。 我是新的也刚学JAVA的一个菜鸟,急寻师兄师弟共同上路求得真!
import java.sql.*;public class mySQLdbBean {
Connection Conn;
ResultSet rs=null;
boolean succeed=true; public mySQLdbBean(){
String strURL = "jdbc:mysql://localhost:3306/test";
try{
Class.forName("org.gjt.mm.mysql.Driver");
Conn= DriverManager.getConnection(strURL,"name","pwd");
}
catch (Exception E) {
succeed=false;
}
} public boolean Connected(){
return succeed;
} public boolean executeSQL(String sqlString){
Statement stmt=null;
boolean blnOK=true;
try {
stmt = Conn.createStatement();
stmt.executeUpdate(sqlString);
}
catch(SQLException ex){
blnOK=false;
}
return blnOK;
} public ResultSet getResultSet(String sqlString){
Statement stmt=null;
try {
stmt = Conn.createStatement();
rs=stmt.executeQuery(sqlString);
}
catch(SQLException ex){
}
return rs;
}
}这样rs.getString(0);得到第一个值;依此类推!字段类型不同方法也不同如getInt();
import java.awt.print.*;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.util.Enumeration;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author
* @version 1.0
*/public class printProcess implements Printable
{
//private printContents aPrintContents;
private PageFormat aPageFormat;
private boolean marginReset;
private boolean directionReset;
private double lMargin;
private double rMargin;
private double tMargin;
private double bMargin;
public printProcess()
{
//aPrintContents=new printContents();
aPageFormat=new PageFormat();
marginReset=false;
directionReset=false;
lMargin=0;
rMargin=0;
tMargin=0;
bMargin=0; } public int print(Graphics g,PageFormat pf,int page) throws PrinterException
{
if(page>=1) return Printable.NO_SUCH_PAGE;
Graphics2D g2=(Graphics2D)g; g2.translate(pf.getImageableX(),pf.getImageableY()); //get printer capabilities by dot
double xPrinter=pf.getWidth();
double yPrinter=pf.getHeight(); double xRatio=xPrinter/21; //21 is the width of the paper
double yRatio=yPrinter/29.7;//29.7 is the height of the paper
//Enumeration contentEnum=this.aPrintContents.getContentList().elements();
//while(contentEnum.hasMoreElements())
//{
// printContent aPrintContent=(printContent)contentEnum.nextElement();
// g2.drawString(aPrintContent.getContent(),(int)(aPrintContent.getXPosition()*xRatio),(int)(aPrintContent.getYPosition()*yRatio));
//}
g2.drawString("whatyayayaya",(int)(3*xRatio),(int)(3*yRatio)); for(int i=0;i<500;i++)
g2.drawLine(0,0,i,0);
for(int j=0;j<500;j++)
g2.drawLine(0,0,0,j); return Printable.PAGE_EXISTS;
} //public void setObjects(printContents thePrintContents)
//{
// this.aPrintContents=thePrintContents;
//} public void setLandscapeOrPortrait(int orient)
{
this.aPageFormat.setOrientation(orient);
this.directionReset=true;
} public void setMargin(double leftMargin ,double rightMargin,double topMargin, double bottomMargin)
{
this.lMargin=leftMargin;
this.rMargin=rightMargin;
this.tMargin=topMargin;
this.bMargin=bottomMargin;
this.marginReset=true;
} //scheduled by user
public void print()
{
//tell whether there exists sth 2 print
//if(this.aPrintContents==null)
// return; try
{
java.awt.print.PrinterJob job =PrinterJob.getPrinterJob();
PageFormat pf; //whether PageFormat is customed set or not
pf=job.defaultPage();
if(this.marginReset)
{
Paper paper=new Paper();
double xRatio=pf.getWidth()/21;
double yRatio=pf.getHeight()/29.7;
paper.setImageableArea(this.lMargin*xRatio,
this.rMargin*yRatio,
pf.getWidth()-this.rMargin*xRatio-this.lMargin*xRatio,
pf.getHeight()-this.bMargin*yRatio-this.tMargin*yRatio);
pf.setPaper(paper);
this.marginReset=false;
}
if(this.directionReset)
{
pf.setOrientation(this.aPageFormat.getOrientation());
this.directionReset=false;
} //pf.setPaper(this.aPageFormat.getPaper());
job.setPrintable(this,pf); if(job.printDialog())
{
job.print();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Merlin 的魔力:在 JDK 1.4 中打印
http://www-900.ibm.com/developerWorks/cn/java/j-merlin/part10/index.shtml