将数据库中的字符串传递给drawString()

时间:2012-12-09 13:30:09

标签: java swing awt drawstring

我有一个数据库,其中包含存储在表中的所有值。我想使用drawString()方法从表中打印一个名称。我创建了一个结果集和arraylist。有没有办法在drawString方法中从数据库传递字符串??

请向我提供代码或代码链接,因为我是新手.Thankyou。

public class AB1 extends JPanel
{   public static void main(String a[]) throws Exception
{
    int ID,TC;
    String FROM, TO, ATS, DTS, SOURCE, DSTN, TS, ST;
    ResultSet rs;

    ArrayList<AB> ab = new ArrayList<AB>();

    //DATABASE CONNECTION

    String TrainNo = "287972";
    try
    {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
    Connection c = DriverManager.getConnection(url, "system", "mypcdatabase");

    // STORING TABLE IN RESULTSET

    int rows = 0;

    PreparedStatement st=c.prepareStatement("select * from MT_TRAIN_CONSIST where TRAIN_NUMBER = ?" );
    rs=st.executeQuery();

    PreparedStatement st1=c.prepareStatement("select * from MT_RAKE_TRAIN_LINK_MASTER where TRAIN_NUMBER = ?");
    rs = st1.executeQuery();

    PreparedStatement st2=c.prepareStatement("select * from MT_SLIP_TRAIN_INFO  where TRAIN_NUMBER = ?");
    rs = st2.executeQuery();

    while(rs.next())
    {

    ID = rs.getInt("ID_TRAIN_DEF");
    FROM = rs.getString("COACH_FROM");
    TO = rs.getString("COACH_TO");
    TC = rs.getInt("NUMBER_OF_COACHES");
    ATS = rs.getString("ATTACH_TRANSFER_STATION");
    DTS = rs.getString("DETACH_TRANSFER_STATION");
    SOURCE = rs.getString("TRAIN_SRC");
    DSTN = rs.getString("TRAIN_DSTN");
    TS = rs.getString("TRANSFER_STATION");
    ST = rs.getString("slip_type");

    AB obj = new AB();

    obj.id = ID;
    obj.tc = TC;
    obj.from = FROM;
    obj.to = TO;
    obj.ats = ATS;
    obj.dts = DTS;
    obj.src = SOURCE;
    obj.dstn = DSTN;
    obj.ts = TS;
    obj.st = ST;
    ab.add(obj);

    rows++;
    }
}

catch(Exception ex)
{
    ex.printStackTrace();
}

getImg(new FileOutputStream("C:\\A&B" + TrainNo + ".jpg"), ab);
}

public static void getImg(OutputStream out, ArrayList<AB> ab) throws IOException
{    int imgWidth = 1024, imgHeight = 768;

BufferedImage image = new BufferedImage(imgWidth,imgHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gx = image.createGraphics();
gx.setColor(Color.BLUE);

gx.setColor(Color.BLACK);
gx.drawLine(60,350,970,350);
gx.drawLine(970,350,960,340);
gx.drawLine(970, 350, 960, 360);
gx.drawString("", 30 , 330);
gx.drawString("", 960 , 330);

ImageIO.write(image, "PNG", out);
out.close();        
}
}       

2 个答案:

答案 0 :(得分:1)

在你的这些行中

PreparedStatement st = c.prepareStatement("select * from MT_TRAIN_CONSIST where TRAIN_NUMBER = ?");
rs=st.executeQuery();

您忘记为提供任何值,例如

PreparedStatement st = c.prepareStatement("select * from MT_TRAIN_CONSIST where TRAIN_NUMBER = ?");
st.setString(1, TrainNo);
rs=st.executeQuery();

请学习Java Naming Conventions并坚持下去。

答案 1 :(得分:0)

@ gagandeep bali 这条线是正确的  String TrainNo =“287972”; 传递此值代替