gpt4 book ai didi

java - 将字符串从数据库传递给 drawString()

转载 作者:行者123 更新时间:2023-11-30 11:38:01 27 4
gpt4 key购买 nike

我有一个数据库,所有值都存储在一个表中。我想使用 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();
}
}

最佳答案

在你的这些行中

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并坚持下去。

关于java - 将字符串从数据库传递给 drawString(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13787811/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com