2

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

请给我一个代码或代码链接,因为我是新手。谢谢。

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();        
}
}       
4

2 回答 2

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 命名约定并遵守它们。

于 2012-12-09T14:28:06.907 回答
0

@ gagandeep bali 这条线是正确的 String TrainNo = "287972"; 此值代替?

于 2012-12-09T14:32:46.197 回答